FIRArrayTransformTests.mm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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:@{
  62. @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]
  63. }];
  64. id expected = @{ @"array" : @[ @1, @2 ] };
  65. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  66. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  67. }
  68. - (void)testAppendToArrayViaUpdate {
  69. [self writeInitialData:@{ @"array" : @[ @1, @3 ] }];
  70. [self updateDocumentRef:_docRef
  71. data:@{
  72. @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @1, @4 ]]
  73. }];
  74. id expected = @{ @"array" : @[ @1, @3, @2, @4 ] };
  75. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  76. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  77. }
  78. - (void)testAppendToArrayViaMergeSet {
  79. [self writeInitialData:@{ @"array" : @[ @1, @3 ] }];
  80. [self mergeDocumentRef:_docRef
  81. data:@{
  82. @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @1, @4 ]]
  83. }];
  84. id expected = @{ @"array" : @[ @1, @3, @2, @4 ] };
  85. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  86. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  87. }
  88. - (void)testAppendObjectToArrayViaUpdate {
  89. [self writeInitialData:@{ @"array" : @[ @{@"a" : @"hi"} ] }];
  90. [self updateDocumentRef:_docRef
  91. data:@{
  92. @"array" : [FIRFieldValue
  93. fieldValueForArrayUnion:@[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ]]
  94. }];
  95. id expected = @{ @"array" : @[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ] };
  96. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  97. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  98. }
  99. - (void)testRemoveFromArrayViaUpdate {
  100. [self writeInitialData:@{ @"array" : @[ @1, @3, @1, @3 ] }];
  101. [self updateDocumentRef:_docRef
  102. data:@{
  103. @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @4 ]]
  104. }];
  105. id expected = @{ @"array" : @[ @3, @3 ] };
  106. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  107. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  108. }
  109. - (void)testRemoveFromArrayViaMergeSet {
  110. [self writeInitialData:@{ @"array" : @[ @1, @3, @1, @3 ] }];
  111. [self mergeDocumentRef:_docRef
  112. data:@{
  113. @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @4 ]]
  114. }];
  115. id expected = @{ @"array" : @[ @3, @3 ] };
  116. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  117. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  118. }
  119. - (void)testRemoveObjectFromArrayViaUpdate {
  120. [self writeInitialData:@{ @"array" : @[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ] }];
  121. [self updateDocumentRef:_docRef
  122. data:@{
  123. @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @{@"a" : @"hi"} ]]
  124. }];
  125. id expected = @{ @"array" : @[ @{@"a" : @"bye"} ] };
  126. XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  127. XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
  128. }
  129. @end
  130. /**
  131. * Unlike the FIRArrayTransformTests above, these tests intentionally avoid having any ongoing
  132. * listeners so that we can test what gets stored in the offline cache based purely on the write
  133. * acknowledgement (without receiving an updated document via watch). As such they also rely on
  134. * persistence being enabled so documents remain in the cache after the write.
  135. */
  136. @interface FIRArrayTransformServerApplicationTests : FSTIntegrationTestCase
  137. @end
  138. @implementation FIRArrayTransformServerApplicationTests {
  139. // A document reference to read and write to.
  140. FIRDocumentReference *_docRef;
  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:@{
  165. @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]
  166. }];
  167. id expected = @{ @"array" : @[ @1, @2 ] };
  168. XCTAssertEqualObjects([self getFromCache].data, expected);
  169. }
  170. - (void)testServerApplicationOfUpdateWithNoCachedBaseDoc {
  171. // Write an initial document out-of-band so it's not in our cache
  172. [self writeDocumentRef:[[self firestore] documentWithPath:_docRef.path]
  173. data:@{
  174. @"array" : @[ @42 ]
  175. }];
  176. [self updateDocumentRef:_docRef
  177. data:@{
  178. @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]
  179. }];
  180. // Nothing should be cached since it was an update and we had no base doc.
  181. XCTAssertNil([self getFromCache]);
  182. }
  183. - (void)testServerApplicationOfMergeSetWithNoCachedBaseDoc {
  184. // Write an initial document out-of-band so it's not in our cache
  185. [self writeDocumentRef:[[self firestore] documentWithPath:_docRef.path]
  186. data:@{
  187. @"array" : @[ @42 ]
  188. }];
  189. [self mergeDocumentRef:_docRef
  190. data:@{
  191. @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]
  192. }];
  193. // Document will be cached but we'll be missing 42.
  194. id expected = @{ @"array" : @[ @1, @2 ] };
  195. XCTAssertEqualObjects([self getFromCache].data, expected);
  196. }
  197. - (void)testServerApplicationOfArrayUnionUpdateWithCachedBaseDoc {
  198. // Cache a document with an array.
  199. [self writeDocumentRef:_docRef data:@{ @"array" : @[ @42 ] }];
  200. [self updateDocumentRef:_docRef
  201. data:@{
  202. @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]
  203. }];
  204. // Should have merged the update with the cached doc.
  205. id expected = @{ @"array" : @[ @42, @1, @2 ] };
  206. XCTAssertEqualObjects([self getFromCache].data, expected);
  207. }
  208. - (void)testServerApplicationOfArrayRemoveUpdateWithCachedBaseDoc {
  209. // Cache a document with an array.
  210. [self writeDocumentRef:_docRef data:@{ @"array" : @[ @42, @1, @2 ] }];
  211. [self updateDocumentRef:_docRef
  212. data:@{
  213. @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @2 ]]
  214. }];
  215. // Should have merged the update with the cached doc.
  216. id expected = @{ @"array" : @[ @42 ] };
  217. XCTAssertEqualObjects([self getFromCache].data, expected);
  218. }
  219. @end