FIRQuerySnapshotTests.mm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/view_snapshot.h"
  30. #include "Firestore/core/src/firebase/firestore/model/document.h"
  31. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  32. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  33. namespace util = firebase::firestore::util;
  34. using firebase::firestore::api::DocumentChange;
  35. using firebase::firestore::api::DocumentSnapshot;
  36. using firebase::firestore::api::Firestore;
  37. using firebase::firestore::api::SnapshotMetadata;
  38. using firebase::firestore::core::DocumentViewChange;
  39. using firebase::firestore::core::ViewSnapshot;
  40. using firebase::firestore::model::DocumentComparator;
  41. using firebase::firestore::model::DocumentKeySet;
  42. using firebase::firestore::model::DocumentSet;
  43. using firebase::firestore::model::DocumentState;
  44. NS_ASSUME_NONNULL_BEGIN
  45. @interface FIRQuerySnapshotTests : XCTestCase
  46. @end
  47. @implementation FIRQuerySnapshotTests
  48. - (void)testEquals {
  49. FIRQuerySnapshot *foo = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  50. FIRQuerySnapshot *fooDup = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  51. FIRQuerySnapshot *differentPath =
  52. FSTTestQuerySnapshot("bar", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  53. FIRQuerySnapshot *differentDoc =
  54. FSTTestQuerySnapshot("foo", @{@"a" : @{@"b" : @1}}, @{}, true, false);
  55. FIRQuerySnapshot *noPendingWrites =
  56. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, false, false);
  57. FIRQuerySnapshot *fromCache =
  58. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, true);
  59. XCTAssertEqualObjects(foo, fooDup);
  60. XCTAssertNotEqualObjects(foo, differentPath);
  61. XCTAssertNotEqualObjects(foo, differentDoc);
  62. XCTAssertNotEqualObjects(foo, noPendingWrites);
  63. XCTAssertNotEqualObjects(foo, fromCache);
  64. XCTAssertEqual([foo hash], [fooDup hash]);
  65. XCTAssertNotEqual([foo hash], [differentPath hash]);
  66. XCTAssertNotEqual([foo hash], [differentDoc hash]);
  67. XCTAssertNotEqual([foo hash], [noPendingWrites hash]);
  68. XCTAssertNotEqual([foo hash], [fromCache hash]);
  69. }
  70. - (void)testIncludeMetadataChanges {
  71. FSTDocument *doc1Old = FSTTestDoc("foo/bar", 1, @{@"a" : @"b"}, DocumentState::kLocalMutations);
  72. FSTDocument *doc1New = FSTTestDoc("foo/bar", 1, @{@"a" : @"b"}, DocumentState::kSynced);
  73. FSTDocument *doc2Old = FSTTestDoc("foo/baz", 1, @{@"a" : @"b"}, DocumentState::kSynced);
  74. FSTDocument *doc2New = FSTTestDoc("foo/baz", 1, @{@"a" : @"c"}, DocumentState::kSynced);
  75. DocumentSet oldDocuments = FSTTestDocSet(DocumentComparator::ByKey(), @[ doc1Old, doc2Old ]);
  76. DocumentSet newDocuments = FSTTestDocSet(DocumentComparator::ByKey(), @[ doc2New, doc2New ]);
  77. std::vector<DocumentViewChange> documentChanges{
  78. DocumentViewChange(doc1New, DocumentViewChange::Type::kMetadata),
  79. DocumentViewChange(doc2New, DocumentViewChange::Type::kModified),
  80. };
  81. std::shared_ptr<Firestore> firestore = FSTTestFirestore().wrapped;
  82. FSTQuery *query = FSTTestQuery("foo");
  83. ViewSnapshot viewSnapshot(query, newDocuments, oldDocuments, std::move(documentChanges),
  84. /*mutated_keys=*/DocumentKeySet(),
  85. /*from_cache=*/false,
  86. /*sync_state_changed=*/true,
  87. /*excludes_metadata_changes=*/false);
  88. SnapshotMetadata metadata(/*pending_writes=*/false, /*from_cache=*/false);
  89. FIRQuerySnapshot *snapshot = [[FIRQuerySnapshot alloc] initWithFirestore:firestore
  90. originalQuery:query
  91. snapshot:std::move(viewSnapshot)
  92. metadata:std::move(metadata)];
  93. DocumentSnapshot doc1Snap(firestore, doc1New.key, doc1New, SnapshotMetadata());
  94. DocumentSnapshot doc2Snap(firestore, doc2New.key, doc2New, SnapshotMetadata());
  95. NSArray<FIRDocumentChange *> *changesWithoutMetadata = @[
  96. [[FIRDocumentChange alloc]
  97. initWithDocumentChange:DocumentChange(DocumentChange::Type::Modified, doc2Snap,
  98. /*old_index=*/1, /*new_index=*/1)],
  99. ];
  100. XCTAssertEqualObjects(snapshot.documentChanges, changesWithoutMetadata);
  101. NSArray<FIRDocumentChange *> *changesWithMetadata = @[
  102. [[FIRDocumentChange alloc]
  103. initWithDocumentChange:DocumentChange(DocumentChange::Type::Modified, doc1Snap,
  104. /*old_index=*/0, /*new_index=*/0)],
  105. [[FIRDocumentChange alloc]
  106. initWithDocumentChange:DocumentChange(DocumentChange::Type::Modified, doc2Snap,
  107. /*old_index=*/1, /*new_index=*/1)],
  108. ];
  109. XCTAssertEqualObjects([snapshot documentChangesWithIncludeMetadataChanges:YES],
  110. changesWithMetadata);
  111. }
  112. @end
  113. NS_ASSUME_NONNULL_END