FIRQuerySnapshotTests.mm 6.5 KB

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