FIRQuerySnapshotTests.mm 5.8 KB

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