FIRArrayTransformTests.mm 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2018 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/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/API/FIRFieldValue+Internal.h"
  19. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  20. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  21. /**
  22. * Note: Transforms are tested pretty thoroughly in FIRServerTimestampTests (via set, update,
  23. * transactions, nested in documents, multiple transforms together, etc.) and so these tests
  24. * mostly focus on the array transform semantics.
  25. */
  26. @interface FIRArrayTransformTests : FSTIntegrationTestCase
  27. @end
  28. @implementation FIRArrayTransformTests {
  29. // A document reference to read and write to.
  30. FIRDocumentReference *_docRef;
  31. // Accumulator used to capture events during the test.
  32. FSTEventAccumulator<FIRDocumentSnapshot *> *_accumulator;
  33. // Listener registration for a listener maintained during the course of the test.
  34. id<FIRListenerRegistration> _listenerRegistration;
  35. }
  36. - (void)setUp {
  37. [super setUp];
  38. _docRef = [self documentRef];
  39. _accumulator = [FSTEventAccumulator accumulatorForTest:self];
  40. _listenerRegistration =
  41. [_docRef addSnapshotListenerWithIncludeMetadataChanges:YES
  42. listener:_accumulator.valueEventHandler];
  43. // Wait for initial nil snapshot to avoid potential races.
  44. FIRDocumentSnapshot *initialSnapshot = [_accumulator awaitEventWithName:@"initial event"];
  45. XCTAssertFalse(initialSnapshot.exists);
  46. }
  47. - (void)tearDown {
  48. [_listenerRegistration remove];
  49. [super tearDown];
  50. }
  51. #pragma mark - Test Helpers
  52. /** Writes some initial data and consumes the events generated. */
  53. - (void)writeInitialData:(NSDictionary<NSString *, id> *)data {
  54. [self writeDocumentRef:_docRef data:data];
  55. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, data);
  56. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, data);
  57. }
  58. #pragma mark - Test Cases
  59. - (void)testCreateDocumentWithArrayUnion {
  60. [self writeDocumentRef:_docRef
  61. data:@{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]}];
  62. id expected = @{@"array" : @[ @1, @2 ]};
  63. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  64. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  65. }
  66. - (void)testAppendToArrayViaUpdate {
  67. [self writeInitialData:@{@"array" : @[ @1, @3 ]}];
  68. [self updateDocumentRef:_docRef
  69. data:@{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @1, @4 ]]}];
  70. id expected = @{@"array" : @[ @1, @3, @2, @4 ]};
  71. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  72. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  73. }
  74. - (void)testAppendToArrayViaMergeSet {
  75. [self writeInitialData:@{@"array" : @[ @1, @3 ]}];
  76. [self mergeDocumentRef:_docRef
  77. data:@{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @1, @4 ]]}];
  78. id expected = @{@"array" : @[ @1, @3, @2, @4 ]};
  79. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  80. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  81. }
  82. - (void)testAppendObjectToArrayViaUpdate {
  83. [self writeInitialData:@{@"array" : @[ @{@"a" : @"hi"} ]}];
  84. [self updateDocumentRef:_docRef
  85. data:@{
  86. @"array" : [FIRFieldValue
  87. fieldValueForArrayUnion:@[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ]]
  88. }];
  89. id expected = @{@"array" : @[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ]};
  90. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  91. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  92. }
  93. - (void)testRemoveFromArrayViaUpdate {
  94. [self writeInitialData:@{@"array" : @[ @1, @3, @1, @3 ]}];
  95. [self updateDocumentRef:_docRef
  96. data:@{@"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @4 ]]}];
  97. id expected = @{@"array" : @[ @3, @3 ]};
  98. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  99. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  100. }
  101. - (void)testRemoveFromArrayViaMergeSet {
  102. [self writeInitialData:@{@"array" : @[ @1, @3, @1, @3 ]}];
  103. [self mergeDocumentRef:_docRef
  104. data:@{@"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @4 ]]}];
  105. id expected = @{@"array" : @[ @3, @3 ]};
  106. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  107. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  108. }
  109. - (void)testRemoveObjectFromArrayViaUpdate {
  110. [self writeInitialData:@{@"array" : @[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ]}];
  111. [self updateDocumentRef:_docRef
  112. data:@{
  113. @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @{@"a" : @"hi"} ]]
  114. }];
  115. id expected = @{@"array" : @[ @{@"a" : @"bye"} ]};
  116. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  117. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  118. }
  119. @end
  120. /**
  121. * Unlike the FIRArrayTransformTests above, these tests intentionally avoid having any ongoing
  122. * listeners so that we can test what gets stored in the offline cache based purely on the write
  123. * acknowledgement (without receiving an updated document via watch). As such they also rely on
  124. * persistence being enabled so documents remain in the cache after the write.
  125. */
  126. @interface FIRArrayTransformServerApplicationTests : FSTIntegrationTestCase
  127. @end
  128. @implementation FIRArrayTransformServerApplicationTests {
  129. // A document reference to read and write to.
  130. FIRDocumentReference *_docRef;
  131. }
  132. - (void)setUp {
  133. [super setUp];
  134. _docRef = [self documentRef];
  135. }
  136. /**
  137. * Helper that uses a temporary listener to read from cache (returning nil if no document seems
  138. * to be in cache). Can probably be replaced with get(source=cache) in the future.
  139. */
  140. - (FIRDocumentSnapshot *_Nullable)getFromCache {
  141. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  142. id<FIRListenerRegistration> listenerRegistration =
  143. [_docRef addSnapshotListener:accumulator.valueEventHandler];
  144. FIRDocumentSnapshot *snapshot = [accumulator awaitEventWithName:@"listenForOneEvent"];
  145. [listenerRegistration remove];
  146. if (snapshot.metadata.fromCache) {
  147. return snapshot;
  148. } else {
  149. return nil;
  150. }
  151. }
  152. - (void)testServerApplicationOfSetWithNoCachedBaseDoc {
  153. [self writeDocumentRef:_docRef
  154. data:@{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]}];
  155. id expected = @{@"array" : @[ @1, @2 ]};
  156. XCTAssertEqualObjects([self getFromCache].data, expected);
  157. }
  158. - (void)testServerApplicationOfUpdateWithNoCachedBaseDoc {
  159. // Write an initial document out-of-band so it's not in our cache
  160. [self writeDocumentRef:[[self firestore] documentWithPath:_docRef.path]
  161. data:@{@"array" : @[ @42 ]}];
  162. [self updateDocumentRef:_docRef
  163. data:@{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]}];
  164. // Nothing should be cached since it was an update and we had no base doc.
  165. XCTAssertNil([self getFromCache]);
  166. }
  167. - (void)testServerApplicationOfMergeSetWithNoCachedBaseDoc {
  168. // Write an initial document out-of-band so it's not in our cache
  169. [self writeDocumentRef:[[self firestore] documentWithPath:_docRef.path]
  170. data:@{@"array" : @[ @42 ]}];
  171. [self mergeDocumentRef:_docRef
  172. data:@{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]}];
  173. // Document will be cached but we'll be missing 42.
  174. id expected = @{@"array" : @[ @1, @2 ]};
  175. XCTAssertEqualObjects([self getFromCache].data, expected);
  176. }
  177. - (void)testServerApplicationOfArrayUnionUpdateWithCachedBaseDoc {
  178. // Cache a document with an array.
  179. [self writeDocumentRef:_docRef data:@{@"array" : @[ @42 ]}];
  180. [self updateDocumentRef:_docRef
  181. data:@{@"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]}];
  182. // Should have merged the update with the cached doc.
  183. id expected = @{@"array" : @[ @42, @1, @2 ]};
  184. XCTAssertEqualObjects([self getFromCache].data, expected);
  185. }
  186. - (void)testServerApplicationOfArrayRemoveUpdateWithCachedBaseDoc {
  187. // Cache a document with an array.
  188. [self writeDocumentRef:_docRef data:@{@"array" : @[ @42, @1, @2 ]}];
  189. [self updateDocumentRef:_docRef
  190. data:@{@"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @2 ]]}];
  191. // Should have merged the update with the cached doc.
  192. id expected = @{@"array" : @[ @42 ]};
  193. XCTAssertEqualObjects([self getFromCache].data, expected);
  194. }
  195. @end