FIRQuerySnapshotTests.mm 6.9 KB

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