FIRQuerySnapshotTests.mm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseFirestore/FIRQuerySnapshot.h>
  17. #import <XCTest/XCTest.h>
  18. #include <memory>
  19. #include <utility>
  20. #include <vector>
  21. #import "Firestore/Example/Tests/API/FSTAPIHelpers.h"
  22. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  23. #import "Firestore/Source/API/FIRDocumentChange+Internal.h"
  24. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  25. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  26. #import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
  27. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  28. #import "Firestore/Source/Model/FSTDocument.h"
  29. #include "Firestore/core/src/firebase/firestore/core/query.h"
  30. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  31. #include "Firestore/core/src/firebase/firestore/model/document.h"
  32. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  33. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  34. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  35. namespace util = firebase::firestore::util;
  36. namespace testutil = firebase::firestore::testutil;
  37. using firebase::firestore::api::DocumentChange;
  38. using firebase::firestore::api::DocumentSnapshot;
  39. using firebase::firestore::api::Firestore;
  40. using firebase::firestore::api::SnapshotMetadata;
  41. using firebase::firestore::core::DocumentViewChange;
  42. using firebase::firestore::core::ViewSnapshot;
  43. using firebase::firestore::model::DocumentComparator;
  44. using firebase::firestore::model::DocumentKeySet;
  45. using firebase::firestore::model::DocumentSet;
  46. using firebase::firestore::model::DocumentState;
  47. using testutil::Query;
  48. NS_ASSUME_NONNULL_BEGIN
  49. @interface FIRQuerySnapshotTests : XCTestCase
  50. @end
  51. @implementation FIRQuerySnapshotTests
  52. - (void)testEquals {
  53. FIRQuerySnapshot *foo = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  54. FIRQuerySnapshot *fooDup = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  55. FIRQuerySnapshot *differentPath =
  56. FSTTestQuerySnapshot("bar", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  57. FIRQuerySnapshot *differentDoc =
  58. FSTTestQuerySnapshot("foo", @{@"a" : @{@"b" : @1}}, @{}, true, false);
  59. FIRQuerySnapshot *noPendingWrites =
  60. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, false, false);
  61. FIRQuerySnapshot *fromCache =
  62. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, true);
  63. XCTAssertEqualObjects(foo, fooDup);
  64. XCTAssertNotEqualObjects(foo, differentPath);
  65. XCTAssertNotEqualObjects(foo, differentDoc);
  66. XCTAssertNotEqualObjects(foo, noPendingWrites);
  67. XCTAssertNotEqualObjects(foo, fromCache);
  68. XCTAssertEqual([foo hash], [fooDup hash]);
  69. XCTAssertNotEqual([foo hash], [differentPath hash]);
  70. XCTAssertNotEqual([foo hash], [differentDoc hash]);
  71. XCTAssertNotEqual([foo hash], [noPendingWrites hash]);
  72. XCTAssertNotEqual([foo hash], [fromCache hash]);
  73. }
  74. - (void)testIncludeMetadataChanges {
  75. FSTDocument *doc1Old = FSTTestDoc("foo/bar", 1, @{@"a" : @"b"}, DocumentState::kLocalMutations);
  76. FSTDocument *doc1New = FSTTestDoc("foo/bar", 1, @{@"a" : @"b"}, DocumentState::kSynced);
  77. FSTDocument *doc2Old = FSTTestDoc("foo/baz", 1, @{@"a" : @"b"}, DocumentState::kSynced);
  78. FSTDocument *doc2New = FSTTestDoc("foo/baz", 1, @{@"a" : @"c"}, DocumentState::kSynced);
  79. DocumentSet oldDocuments = FSTTestDocSet(DocumentComparator::ByKey(), @[ doc1Old, doc2Old ]);
  80. DocumentSet newDocuments = FSTTestDocSet(DocumentComparator::ByKey(), @[ doc2New, doc2New ]);
  81. std::vector<DocumentViewChange> documentChanges{
  82. DocumentViewChange(doc1New, DocumentViewChange::Type::kMetadata),
  83. DocumentViewChange(doc2New, DocumentViewChange::Type::kModified),
  84. };
  85. std::shared_ptr<Firestore> firestore = FSTTestFirestore().wrapped;
  86. core::Query query = Query("foo");
  87. ViewSnapshot viewSnapshot(query, newDocuments, oldDocuments, std::move(documentChanges),
  88. /*mutated_keys=*/DocumentKeySet(),
  89. /*from_cache=*/false,
  90. /*sync_state_changed=*/true,
  91. /*excludes_metadata_changes=*/false);
  92. SnapshotMetadata metadata(/*pending_writes=*/false, /*from_cache=*/false);
  93. FIRQuerySnapshot *snapshot = [[FIRQuerySnapshot alloc] initWithFirestore:firestore
  94. originalQuery:query
  95. snapshot:std::move(viewSnapshot)
  96. metadata:std::move(metadata)];
  97. DocumentSnapshot doc1Snap(firestore, doc1New.key, doc1New, SnapshotMetadata());
  98. DocumentSnapshot doc2Snap(firestore, doc2New.key, doc2New, SnapshotMetadata());
  99. NSArray<FIRDocumentChange *> *changesWithoutMetadata = @[
  100. [[FIRDocumentChange alloc]
  101. initWithDocumentChange:DocumentChange(DocumentChange::Type::Modified, doc2Snap,
  102. /*old_index=*/1, /*new_index=*/1)],
  103. ];
  104. XCTAssertEqualObjects(snapshot.documentChanges, changesWithoutMetadata);
  105. NSArray<FIRDocumentChange *> *changesWithMetadata = @[
  106. [[FIRDocumentChange alloc]
  107. initWithDocumentChange:DocumentChange(DocumentChange::Type::Modified, doc1Snap,
  108. /*old_index=*/0, /*new_index=*/0)],
  109. [[FIRDocumentChange alloc]
  110. initWithDocumentChange:DocumentChange(DocumentChange::Type::Modified, doc2Snap,
  111. /*old_index=*/1, /*new_index=*/1)],
  112. ];
  113. XCTAssertEqualObjects([snapshot documentChangesWithIncludeMetadataChanges:YES],
  114. changesWithMetadata);
  115. }
  116. @end
  117. NS_ASSUME_NONNULL_END