FIRQuerySnapshotTests.mm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = FSTTestQuerySnapshot("bar", @{},
  44. @{ @"a" : @{@"a" : @1} }, YES, NO);
  45. FIRQuerySnapshot *differentDoc = FSTTestQuerySnapshot("foo",
  46. @{ @"a" : @{@"b" : @1} }, @{}, YES, NO);
  47. FIRQuerySnapshot *noPendingWrites = FSTTestQuerySnapshot("foo", @{},
  48. @{ @"a" : @{@"a" : @1} }, NO, NO);
  49. FIRQuerySnapshot *fromCache = FSTTestQuerySnapshot("foo", @{},
  50. @{ @"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"}, YES);
  64. FSTDocument *doc1New = FSTTestDoc("foo/bar", 1, @{@"a" : @"b"}, NO);
  65. FSTDocument *doc2Old = FSTTestDoc("foo/baz", 1, @{@"a" : @"b"}, NO);
  66. FSTDocument *doc2New = FSTTestDoc("foo/baz", 1, @{@"a" : @"c"}, NO);
  67. FSTDocumentSet *oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[ doc1Old, doc2Old ]);
  68. FSTDocumentSet *newDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[ doc2New, doc2New ]);
  69. NSArray<FSTDocumentViewChange *> *documentChanges = @[
  70. [FSTDocumentViewChange changeWithDocument:doc1New type:FSTDocumentViewChangeTypeMetadata],
  71. [FSTDocumentViewChange changeWithDocument:doc2New type:FSTDocumentViewChangeTypeModified],
  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. hasPendingWrites:NO
  81. syncStateChanged:YES];
  82. FIRSnapshotMetadata *metadata =
  83. [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:NO 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. FIRQueryDocumentSnapshot *doc2Snap = [FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
  93. documentKey:doc2New.key
  94. document:doc2New
  95. fromCache:NO];
  96. NSArray<FIRDocumentChange *> *changesWithoutMetadata = @[
  97. [[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeModified
  98. document:doc2Snap
  99. oldIndex:1
  100. newIndex:1],
  101. ];
  102. XCTAssertEqualObjects(snapshot.documentChanges, changesWithoutMetadata);
  103. NSArray<FIRDocumentChange *> *changesWithMetadata = @[
  104. [[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeModified
  105. document:doc1Snap
  106. oldIndex:0
  107. newIndex:0],
  108. [[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeModified
  109. document:doc2Snap
  110. oldIndex:1
  111. newIndex:1],
  112. ];
  113. XCTAssertEqualObjects([snapshot documentChangesWithIncludeMetadataChanges:YES],
  114. changesWithMetadata);
  115. }
  116. @end
  117. NS_ASSUME_NONNULL_END