FIRQuerySnapshotTests.mm 6.8 KB

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