FIRArrayTransformTests.mm 9.3 KB

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