FIRQuerySnapshotTests.mm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #import "Firestore/Example/Tests/API/FSTAPIHelpers.h"
  19. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  20. #import "Firestore/Source/API/FIRDocumentChange+Internal.h"
  21. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  22. #import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
  23. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  24. #import "Firestore/Source/Core/FSTViewSnapshot.h"
  25. #import "Firestore/Source/Model/FSTDocument.h"
  26. #import "Firestore/Source/Model/FSTDocumentSet.h"
  27. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  28. namespace util = firebase::firestore::util;
  29. using firebase::firestore::core::DocumentViewChangeType;
  30. NS_ASSUME_NONNULL_BEGIN
  31. @interface FIRDocumentChange ()
  32. // Expose initializer for testing.
  33. - (instancetype)initWithType:(FIRDocumentChangeType)type
  34. document:(FIRQueryDocumentSnapshot *)document
  35. oldIndex:(NSUInteger)oldIndex
  36. newIndex:(NSUInteger)newIndex;
  37. @end
  38. @interface FIRQuerySnapshotTests : XCTestCase
  39. @end
  40. @implementation FIRQuerySnapshotTests
  41. - (void)testEquals {
  42. FIRQuerySnapshot *foo = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, YES, NO);
  43. FIRQuerySnapshot *fooDup = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, YES, NO);
  44. FIRQuerySnapshot *differentPath =
  45. FSTTestQuerySnapshot("bar", @{}, @{@"a" : @{@"a" : @1}}, YES, NO);
  46. FIRQuerySnapshot *differentDoc =
  47. FSTTestQuerySnapshot("foo", @{@"a" : @{@"b" : @1}}, @{}, YES, NO);
  48. FIRQuerySnapshot *noPendingWrites =
  49. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, NO, NO);
  50. FIRQuerySnapshot *fromCache = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, YES, YES);
  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. FSTDocumentSet *oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[ doc1Old, doc2Old ]);
  68. FSTDocumentSet *newDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[ doc2New, doc2New ]);
  69. NSArray<FSTDocumentViewChange *> *documentChanges = @[
  70. [FSTDocumentViewChange changeWithDocument:doc1New type:DocumentViewChangeType::kMetadata],
  71. [FSTDocumentViewChange changeWithDocument:doc2New type:DocumentViewChangeType::kModified],
  72. ];
  73. FIRFirestore *firestore = FSTTestFirestore();
  74. FSTQuery *query = FSTTestQuery("foo");
  75. FSTViewSnapshot *viewSnapshot = [[FSTViewSnapshot alloc] initWithQuery:query
  76. documents:newDocuments
  77. oldDocuments:oldDocuments
  78. documentChanges:documentChanges
  79. fromCache:NO
  80. mutatedKeys:DocumentKeySet {}
  81. syncStateChanged:YES
  82. excludesMetadataChanges:NO];
  83. FIRSnapshotMetadata *metadata = [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:NO
  84. fromCache:NO];
  85. FIRQuerySnapshot *snapshot = [FIRQuerySnapshot snapshotWithFirestore:firestore
  86. originalQuery:query
  87. snapshot:viewSnapshot
  88. metadata:metadata];
  89. FIRQueryDocumentSnapshot *doc1Snap = [FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
  90. documentKey:doc1New.key
  91. document:doc1New
  92. fromCache:NO
  93. hasPendingWrites:NO];
  94. FIRQueryDocumentSnapshot *doc2Snap = [FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
  95. documentKey:doc2New.key
  96. document:doc2New
  97. fromCache:NO
  98. hasPendingWrites:NO];
  99. NSArray<FIRDocumentChange *> *changesWithoutMetadata = @[
  100. [[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeModified
  101. document:doc2Snap
  102. oldIndex:1
  103. newIndex:1],
  104. ];
  105. XCTAssertEqualObjects(snapshot.documentChanges, changesWithoutMetadata);
  106. NSArray<FIRDocumentChange *> *changesWithMetadata = @[
  107. [[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeModified
  108. document:doc1Snap
  109. oldIndex:0
  110. newIndex:0],
  111. [[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeModified
  112. document:doc2Snap
  113. oldIndex:1
  114. newIndex:1],
  115. ];
  116. XCTAssertEqualObjects([snapshot documentChangesWithIncludeMetadataChanges:YES],
  117. changesWithMetadata);
  118. }
  119. @end
  120. NS_ASSUME_NONNULL_END