FIRWriteBatchTests.mm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  19. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  20. @interface FIRWriteBatchTests : FSTIntegrationTestCase
  21. @end
  22. @implementation FIRWriteBatchTests
  23. - (void)testSupportEmptyBatches {
  24. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  25. [[[self firestore] batch] commitWithCompletion:^(NSError *error) {
  26. XCTAssertNil(error);
  27. [expectation fulfill];
  28. }];
  29. [self awaitExpectations];
  30. }
  31. - (void)testCommitWithoutCompletionHandler {
  32. FIRDocumentReference *doc = [self documentRef];
  33. FIRWriteBatch *batch1 = [doc.firestore batch];
  34. [batch1 setData:@{@"aa" : @"bb"} forDocument:doc];
  35. [batch1 commitWithCompletion:nil];
  36. FIRDocumentSnapshot *snapshot1 = [self readDocumentForRef:doc];
  37. XCTAssertTrue(snapshot1.exists);
  38. XCTAssertEqualObjects(snapshot1.data, @{@"aa" : @"bb"});
  39. FIRWriteBatch *batch2 = [doc.firestore batch];
  40. [batch2 setData:@{@"cc" : @"dd"} forDocument:doc];
  41. [batch2 commit];
  42. // TODO(b/70631617): There's currently a backend bug that prevents us from using a resume token
  43. // right away (against hexa at least). So we sleep. :-( :-( Anything over ~10ms seems to be
  44. // sufficient.
  45. [NSThread sleepForTimeInterval:0.2f];
  46. FIRDocumentSnapshot *snapshot2 = [self readDocumentForRef:doc];
  47. XCTAssertTrue(snapshot2.exists);
  48. XCTAssertEqualObjects(snapshot2.data, @{@"cc" : @"dd"});
  49. }
  50. - (void)testSetDocuments {
  51. FIRDocumentReference *doc = [self documentRef];
  52. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  53. FIRWriteBatch *batch = [doc.firestore batch];
  54. [batch setData:@{@"a" : @"b"} forDocument:doc];
  55. [batch setData:@{@"c" : @"d"} forDocument:doc];
  56. [batch commitWithCompletion:^(NSError *error) {
  57. XCTAssertNil(error);
  58. [batchExpectation fulfill];
  59. }];
  60. [self awaitExpectations];
  61. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  62. XCTAssertTrue(snapshot.exists);
  63. XCTAssertEqualObjects(snapshot.data, @{@"c" : @"d"});
  64. }
  65. - (void)testSetDocumentWithMerge {
  66. FIRDocumentReference *doc = [self documentRef];
  67. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  68. FIRWriteBatch *batch = [doc.firestore batch];
  69. [batch setData:@{ @"a" : @"b", @"nested" : @{@"a" : @"b"} } forDocument:doc];
  70. [batch setData:@{ @"c" : @"d", @"nested" : @{@"c" : @"d"} } forDocument:doc merge:YES];
  71. [batch commitWithCompletion:^(NSError *error) {
  72. XCTAssertNil(error);
  73. [batchExpectation fulfill];
  74. }];
  75. [self awaitExpectations];
  76. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  77. XCTAssertTrue(snapshot.exists);
  78. XCTAssertEqualObjects(
  79. snapshot.data, (
  80. @{ @"a" : @"b",
  81. @"c" : @"d",
  82. @"nested" : @{@"a" : @"b", @"c" : @"d"} }));
  83. }
  84. - (void)testUpdateDocuments {
  85. FIRDocumentReference *doc = [self documentRef];
  86. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  87. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  88. FIRWriteBatch *batch = [doc.firestore batch];
  89. [batch updateData:@{ @"baz" : @42 } forDocument:doc];
  90. [batch commitWithCompletion:^(NSError *error) {
  91. XCTAssertNil(error);
  92. [batchExpectation fulfill];
  93. }];
  94. [self awaitExpectations];
  95. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  96. XCTAssertTrue(snapshot.exists);
  97. XCTAssertEqualObjects(snapshot.data, (@{ @"foo" : @"bar", @"baz" : @42 }));
  98. }
  99. - (void)testCannotUpdateNonexistentDocuments {
  100. FIRDocumentReference *doc = [self documentRef];
  101. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  102. FIRWriteBatch *batch = [doc.firestore batch];
  103. [batch updateData:@{ @"baz" : @42 } forDocument:doc];
  104. [batch commitWithCompletion:^(NSError *error) {
  105. XCTAssertNotNil(error);
  106. [batchExpectation fulfill];
  107. }];
  108. [self awaitExpectations];
  109. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  110. XCTAssertFalse(result.exists);
  111. }
  112. - (void)testDeleteDocuments {
  113. FIRDocumentReference *doc = [self documentRef];
  114. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  115. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  116. XCTAssertTrue(snapshot.exists);
  117. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  118. FIRWriteBatch *batch = [doc.firestore batch];
  119. [batch deleteDocument:doc];
  120. [batch commitWithCompletion:^(NSError *error) {
  121. XCTAssertNil(error);
  122. [batchExpectation fulfill];
  123. }];
  124. [self awaitExpectations];
  125. snapshot = [self readDocumentForRef:doc];
  126. XCTAssertFalse(snapshot.exists);
  127. }
  128. - (void)testBatchesCommitAtomicallyRaisingCorrectEvents {
  129. FIRCollectionReference *collection = [self collectionRef];
  130. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  131. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  132. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  133. [collection addSnapshotListenerWithIncludeMetadataChanges:YES
  134. listener:accumulator.valueEventHandler];
  135. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  136. XCTAssertEqual(initialSnap.count, 0);
  137. // Atomically write two documents.
  138. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  139. FIRWriteBatch *batch = [collection.firestore batch];
  140. [batch setData:@{ @"a" : @1 } forDocument:docA];
  141. [batch setData:@{ @"b" : @2 } forDocument:docB];
  142. [batch commitWithCompletion:^(NSError *_Nullable error) {
  143. XCTAssertNil(error);
  144. [expectation fulfill];
  145. }];
  146. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  147. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  148. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap), (@[ @{ @"a" : @1 }, @{ @"b" : @2 } ]));
  149. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  150. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  151. XCTAssertEqualObjects(FIRQuerySnapshotGetData(serverSnap), (@[ @{ @"a" : @1 }, @{ @"b" : @2 } ]));
  152. }
  153. - (void)testBatchesFailAtomicallyRaisingCorrectEvents {
  154. FIRCollectionReference *collection = [self collectionRef];
  155. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  156. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  157. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  158. [collection addSnapshotListenerWithIncludeMetadataChanges:YES
  159. listener:accumulator.valueEventHandler];
  160. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  161. XCTAssertEqual(initialSnap.count, 0);
  162. // Atomically write 1 document and update a nonexistent document.
  163. XCTestExpectation *expectation = [self expectationWithDescription:@"batch failed"];
  164. FIRWriteBatch *batch = [collection.firestore batch];
  165. [batch setData:@{ @"a" : @1 } forDocument:docA];
  166. [batch updateData:@{ @"b" : @2 } forDocument:docB];
  167. [batch commitWithCompletion:^(NSError *_Nullable error) {
  168. XCTAssertNotNil(error);
  169. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  170. XCTAssertEqual(error.code, FIRFirestoreErrorCodeNotFound);
  171. [expectation fulfill];
  172. }];
  173. // Local event with the set document.
  174. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  175. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  176. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap), (@[ @{ @"a" : @1 } ]));
  177. // Server event with the set reverted.
  178. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  179. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  180. XCTAssertEqual(serverSnap.count, 0);
  181. }
  182. - (void)testWriteTheSameServerTimestampAcrossWrites {
  183. FIRCollectionReference *collection = [self collectionRef];
  184. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  185. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  186. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  187. [collection addSnapshotListenerWithIncludeMetadataChanges:YES
  188. listener:accumulator.valueEventHandler];
  189. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  190. XCTAssertEqual(initialSnap.count, 0);
  191. // Atomically write 2 documents with server timestamps.
  192. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  193. FIRWriteBatch *batch = [collection.firestore batch];
  194. [batch setData:@{@"when" : [FIRFieldValue fieldValueForServerTimestamp]} forDocument:docA];
  195. [batch setData:@{@"when" : [FIRFieldValue fieldValueForServerTimestamp]} forDocument:docB];
  196. [batch commitWithCompletion:^(NSError *_Nullable error) {
  197. XCTAssertNil(error);
  198. [expectation fulfill];
  199. }];
  200. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  201. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  202. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap),
  203. (@[ @{@"when" : [NSNull null]}, @{@"when" : [NSNull null]} ]));
  204. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  205. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  206. XCTAssertEqual(serverSnap.count, 2);
  207. NSDate *when = serverSnap.documents[0][@"when"];
  208. XCTAssertEqualObjects(FIRQuerySnapshotGetData(serverSnap),
  209. (@[ @{@"when" : when}, @{@"when" : when} ]));
  210. }
  211. - (void)testCanWriteTheSameDocumentMultipleTimes {
  212. FIRDocumentReference *doc = [self documentRef];
  213. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  214. [doc addSnapshotListenerWithIncludeMetadataChanges:YES listener:accumulator.valueEventHandler];
  215. FIRDocumentSnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  216. XCTAssertFalse(initialSnap.exists);
  217. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  218. FIRWriteBatch *batch = [doc.firestore batch];
  219. [batch deleteDocument:doc];
  220. [batch setData:@{ @"a" : @1, @"b" : @1, @"when" : @"when" } forDocument:doc];
  221. [batch updateData:@{
  222. @"b" : @2,
  223. @"when" : [FIRFieldValue fieldValueForServerTimestamp]
  224. }
  225. forDocument:doc];
  226. [batch commitWithCompletion:^(NSError *_Nullable error) {
  227. XCTAssertNil(error);
  228. [expectation fulfill];
  229. }];
  230. FIRDocumentSnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  231. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  232. XCTAssertEqualObjects(localSnap.data, (@{ @"a" : @1, @"b" : @2, @"when" : [NSNull null] }));
  233. FIRDocumentSnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  234. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  235. NSDate *when = serverSnap[@"when"];
  236. XCTAssertEqualObjects(serverSnap.data, (@{ @"a" : @1, @"b" : @2, @"when" : when }));
  237. }
  238. - (void)testUpdateFieldsWithDots {
  239. FIRDocumentReference *doc = [self documentRef];
  240. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  241. FIRWriteBatch *batch = [doc.firestore batch];
  242. [batch setData:@{@"a.b" : @"old", @"c.d" : @"old"} forDocument:doc];
  243. [batch updateData:@{
  244. [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"
  245. }
  246. forDocument:doc];
  247. [batch commitWithCompletion:^(NSError *_Nullable error) {
  248. XCTAssertNil(error);
  249. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  250. XCTAssertNil(error);
  251. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  252. }];
  253. [expectation fulfill];
  254. }];
  255. [self awaitExpectations];
  256. }
  257. - (void)testUpdateNestedFields {
  258. FIRDocumentReference *doc = [self documentRef];
  259. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  260. FIRWriteBatch *batch = [doc.firestore batch];
  261. [batch setData:@{
  262. @"a" : @{@"b" : @"old"},
  263. @"c" : @{@"d" : @"old"},
  264. @"e" : @{@"f" : @"old"}
  265. }
  266. forDocument:doc];
  267. [batch updateData:@{
  268. @"a.b" : @"new",
  269. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  270. }
  271. forDocument:doc];
  272. [batch commitWithCompletion:^(NSError *_Nullable error) {
  273. XCTAssertNil(error);
  274. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  275. XCTAssertNil(error);
  276. XCTAssertEqualObjects(snapshot.data, (@{
  277. @"a" : @{@"b" : @"new"},
  278. @"c" : @{@"d" : @"new"},
  279. @"e" : @{@"f" : @"old"}
  280. }));
  281. }];
  282. [expectation fulfill];
  283. }];
  284. [self awaitExpectations];
  285. }
  286. @end