FIRQuerySnapshotTests.mm 5.5 KB

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