FIRQuerySnapshotTests.mm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/FIRQuerySnapshot+Internal.h"
  25. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  26. #import "Firestore/Source/Model/FSTDocument.h"
  27. #import "Firestore/Source/Model/FSTDocumentSet.h"
  28. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  29. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  30. namespace util = firebase::firestore::util;
  31. using firebase::firestore::core::DocumentViewChange;
  32. using firebase::firestore::core::ViewSnapshot;
  33. using firebase::firestore::model::DocumentKeySet;
  34. NS_ASSUME_NONNULL_BEGIN
  35. @interface FIRDocumentChange ()
  36. // Expose initializer for testing.
  37. - (instancetype)initWithType:(FIRDocumentChangeType)type
  38. document:(FIRQueryDocumentSnapshot *)document
  39. oldIndex:(NSUInteger)oldIndex
  40. newIndex:(NSUInteger)newIndex;
  41. @end
  42. @interface FIRQuerySnapshotTests : XCTestCase
  43. @end
  44. @implementation FIRQuerySnapshotTests
  45. - (void)testEquals {
  46. FIRQuerySnapshot *foo = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  47. FIRQuerySnapshot *fooDup = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  48. FIRQuerySnapshot *differentPath =
  49. FSTTestQuerySnapshot("bar", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  50. FIRQuerySnapshot *differentDoc =
  51. FSTTestQuerySnapshot("foo", @{@"a" : @{@"b" : @1}}, @{}, true, false);
  52. FIRQuerySnapshot *noPendingWrites =
  53. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, false, false);
  54. FIRQuerySnapshot *fromCache =
  55. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, true);
  56. XCTAssertEqualObjects(foo, fooDup);
  57. XCTAssertNotEqualObjects(foo, differentPath);
  58. XCTAssertNotEqualObjects(foo, differentDoc);
  59. XCTAssertNotEqualObjects(foo, noPendingWrites);
  60. XCTAssertNotEqualObjects(foo, fromCache);
  61. XCTAssertEqual([foo hash], [fooDup hash]);
  62. XCTAssertNotEqual([foo hash], [differentPath hash]);
  63. XCTAssertNotEqual([foo hash], [differentDoc hash]);
  64. XCTAssertNotEqual([foo hash], [noPendingWrites hash]);
  65. XCTAssertNotEqual([foo hash], [fromCache hash]);
  66. }
  67. - (void)testIncludeMetadataChanges {
  68. FSTDocument *doc1Old = FSTTestDoc("foo/bar", 1, @{@"a" : @"b"}, FSTDocumentStateLocalMutations);
  69. FSTDocument *doc1New = FSTTestDoc("foo/bar", 1, @{@"a" : @"b"}, FSTDocumentStateSynced);
  70. FSTDocument *doc2Old = FSTTestDoc("foo/baz", 1, @{@"a" : @"b"}, FSTDocumentStateSynced);
  71. FSTDocument *doc2New = FSTTestDoc("foo/baz", 1, @{@"a" : @"c"}, FSTDocumentStateSynced);
  72. FSTDocumentSet *oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[ doc1Old, doc2Old ]);
  73. FSTDocumentSet *newDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[ doc2New, doc2New ]);
  74. std::vector<DocumentViewChange> documentChanges{
  75. DocumentViewChange{doc1New, DocumentViewChange::Type::kMetadata},
  76. DocumentViewChange{doc2New, DocumentViewChange::Type::kModified},
  77. };
  78. FIRFirestore *firestore = FSTTestFirestore();
  79. FSTQuery *query = FSTTestQuery("foo");
  80. ViewSnapshot viewSnapshot{query,
  81. newDocuments,
  82. oldDocuments,
  83. std::move(documentChanges),
  84. /*mutated_keys=*/DocumentKeySet{},
  85. /*from_cache=*/false,
  86. /*sync_state_changed=*/true,
  87. /*excludes_metadata_changes=*/false};
  88. FIRSnapshotMetadata *metadata = [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:NO
  89. fromCache:NO];
  90. FIRQuerySnapshot *snapshot = [FIRQuerySnapshot snapshotWithFirestore:firestore
  91. originalQuery:query
  92. snapshot:std::move(viewSnapshot)
  93. metadata:metadata];
  94. FIRQueryDocumentSnapshot *doc1Snap = [FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
  95. documentKey:doc1New.key
  96. document:doc1New
  97. fromCache:NO
  98. hasPendingWrites:NO];
  99. FIRQueryDocumentSnapshot *doc2Snap = [FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
  100. documentKey:doc2New.key
  101. document:doc2New
  102. fromCache:NO
  103. hasPendingWrites:NO];
  104. NSArray<FIRDocumentChange *> *changesWithoutMetadata = @[
  105. [[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeModified
  106. document:doc2Snap
  107. oldIndex:1
  108. newIndex:1],
  109. ];
  110. XCTAssertEqualObjects(snapshot.documentChanges, changesWithoutMetadata);
  111. NSArray<FIRDocumentChange *> *changesWithMetadata = @[
  112. [[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeModified
  113. document:doc1Snap
  114. oldIndex:0
  115. newIndex:0],
  116. [[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeModified
  117. document:doc2Snap
  118. oldIndex:1
  119. newIndex:1],
  120. ];
  121. XCTAssertEqualObjects([snapshot documentChangesWithIncludeMetadataChanges:YES],
  122. changesWithMetadata);
  123. }
  124. @end
  125. NS_ASSUME_NONNULL_END