FIRWriteBatchTests.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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:@{
  71. @"c" : @"d",
  72. @"nested" : @{@"c" : @"d"}
  73. }
  74. forDocument:doc
  75. options:[FIRSetOptions merge]];
  76. [batch commitWithCompletion:^(NSError *error) {
  77. XCTAssertNil(error);
  78. [batchExpectation fulfill];
  79. }];
  80. [self awaitExpectations];
  81. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  82. XCTAssertTrue(snapshot.exists);
  83. XCTAssertEqualObjects(
  84. snapshot.data, (
  85. @{ @"a" : @"b",
  86. @"c" : @"d",
  87. @"nested" : @{@"a" : @"b", @"c" : @"d"} }));
  88. }
  89. - (void)testUpdateDocuments {
  90. FIRDocumentReference *doc = [self documentRef];
  91. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  92. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  93. FIRWriteBatch *batch = [doc.firestore batch];
  94. [batch updateData:@{ @"baz" : @42 } forDocument:doc];
  95. [batch commitWithCompletion:^(NSError *error) {
  96. XCTAssertNil(error);
  97. [batchExpectation fulfill];
  98. }];
  99. [self awaitExpectations];
  100. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  101. XCTAssertTrue(snapshot.exists);
  102. XCTAssertEqualObjects(snapshot.data, (@{ @"foo" : @"bar", @"baz" : @42 }));
  103. }
  104. - (void)testCannotUpdateNonexistentDocuments {
  105. FIRDocumentReference *doc = [self documentRef];
  106. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  107. FIRWriteBatch *batch = [doc.firestore batch];
  108. [batch updateData:@{ @"baz" : @42 } forDocument:doc];
  109. [batch commitWithCompletion:^(NSError *error) {
  110. XCTAssertNotNil(error);
  111. [batchExpectation fulfill];
  112. }];
  113. [self awaitExpectations];
  114. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  115. XCTAssertFalse(result.exists);
  116. }
  117. - (void)testDeleteDocuments {
  118. FIRDocumentReference *doc = [self documentRef];
  119. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  120. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  121. XCTAssertTrue(snapshot.exists);
  122. XCTestExpectation *batchExpectation = [self expectationWithDescription:@"batch written"];
  123. FIRWriteBatch *batch = [doc.firestore batch];
  124. [batch deleteDocument:doc];
  125. [batch commitWithCompletion:^(NSError *error) {
  126. XCTAssertNil(error);
  127. [batchExpectation fulfill];
  128. }];
  129. [self awaitExpectations];
  130. snapshot = [self readDocumentForRef:doc];
  131. XCTAssertFalse(snapshot.exists);
  132. }
  133. - (void)testBatchesCommitAtomicallyRaisingCorrectEvents {
  134. FIRCollectionReference *collection = [self collectionRef];
  135. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  136. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  137. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  138. [collection addSnapshotListenerWithOptions:[[FIRQueryListenOptions options]
  139. includeQueryMetadataChanges:YES]
  140. listener:accumulator.valueEventHandler];
  141. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  142. XCTAssertEqual(initialSnap.count, 0);
  143. // Atomically write two documents.
  144. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  145. FIRWriteBatch *batch = [collection.firestore batch];
  146. [batch setData:@{ @"a" : @1 } forDocument:docA];
  147. [batch setData:@{ @"b" : @2 } forDocument:docB];
  148. [batch commitWithCompletion:^(NSError *_Nullable error) {
  149. XCTAssertNil(error);
  150. [expectation fulfill];
  151. }];
  152. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  153. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  154. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap), (@[ @{ @"a" : @1 }, @{ @"b" : @2 } ]));
  155. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  156. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  157. XCTAssertEqualObjects(FIRQuerySnapshotGetData(serverSnap), (@[ @{ @"a" : @1 }, @{ @"b" : @2 } ]));
  158. }
  159. - (void)testBatchesFailAtomicallyRaisingCorrectEvents {
  160. FIRCollectionReference *collection = [self collectionRef];
  161. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  162. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  163. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  164. [collection addSnapshotListenerWithOptions:[[FIRQueryListenOptions options]
  165. includeQueryMetadataChanges:YES]
  166. listener:accumulator.valueEventHandler];
  167. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  168. XCTAssertEqual(initialSnap.count, 0);
  169. // Atomically write 1 document and update a nonexistent document.
  170. XCTestExpectation *expectation = [self expectationWithDescription:@"batch failed"];
  171. FIRWriteBatch *batch = [collection.firestore batch];
  172. [batch setData:@{ @"a" : @1 } forDocument:docA];
  173. [batch updateData:@{ @"b" : @2 } forDocument:docB];
  174. [batch commitWithCompletion:^(NSError *_Nullable error) {
  175. XCTAssertNotNil(error);
  176. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  177. XCTAssertEqual(error.code, FIRFirestoreErrorCodeNotFound);
  178. [expectation fulfill];
  179. }];
  180. // Local event with the set document.
  181. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  182. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  183. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap), (@[ @{ @"a" : @1 } ]));
  184. // Server event with the set reverted.
  185. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  186. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  187. XCTAssertEqual(serverSnap.count, 0);
  188. }
  189. - (void)testWriteTheSameServerTimestampAcrossWrites {
  190. FIRCollectionReference *collection = [self collectionRef];
  191. FIRDocumentReference *docA = [collection documentWithPath:@"a"];
  192. FIRDocumentReference *docB = [collection documentWithPath:@"b"];
  193. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  194. [collection addSnapshotListenerWithOptions:[[FIRQueryListenOptions options]
  195. includeQueryMetadataChanges:YES]
  196. listener:accumulator.valueEventHandler];
  197. FIRQuerySnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  198. XCTAssertEqual(initialSnap.count, 0);
  199. // Atomically write 2 documents with server timestamps.
  200. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  201. FIRWriteBatch *batch = [collection.firestore batch];
  202. [batch setData:@{@"when" : [FIRFieldValue fieldValueForServerTimestamp]} forDocument:docA];
  203. [batch setData:@{@"when" : [FIRFieldValue fieldValueForServerTimestamp]} forDocument:docB];
  204. [batch commitWithCompletion:^(NSError *_Nullable error) {
  205. XCTAssertNil(error);
  206. [expectation fulfill];
  207. }];
  208. FIRQuerySnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  209. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  210. XCTAssertEqualObjects(FIRQuerySnapshotGetData(localSnap),
  211. (@[ @{@"when" : [NSNull null]}, @{@"when" : [NSNull null]} ]));
  212. FIRQuerySnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  213. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  214. XCTAssertEqual(serverSnap.count, 2);
  215. NSDate *when = serverSnap.documents[0][@"when"];
  216. XCTAssertEqualObjects(FIRQuerySnapshotGetData(serverSnap),
  217. (@[ @{@"when" : when}, @{@"when" : when} ]));
  218. }
  219. - (void)testCanWriteTheSameDocumentMultipleTimes {
  220. FIRDocumentReference *doc = [self documentRef];
  221. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  222. [doc
  223. addSnapshotListenerWithOptions:[[FIRDocumentListenOptions options] includeMetadataChanges:YES]
  224. listener:accumulator.valueEventHandler];
  225. FIRDocumentSnapshot *initialSnap = [accumulator awaitEventWithName:@"initial event"];
  226. XCTAssertFalse(initialSnap.exists);
  227. XCTestExpectation *expectation = [self expectationWithDescription:@"batch written"];
  228. FIRWriteBatch *batch = [doc.firestore batch];
  229. [batch deleteDocument:doc];
  230. [batch setData:@{ @"a" : @1, @"b" : @1, @"when" : @"when" } forDocument:doc];
  231. [batch updateData:@{
  232. @"b" : @2,
  233. @"when" : [FIRFieldValue fieldValueForServerTimestamp]
  234. }
  235. forDocument:doc];
  236. [batch commitWithCompletion:^(NSError *_Nullable error) {
  237. XCTAssertNil(error);
  238. [expectation fulfill];
  239. }];
  240. FIRDocumentSnapshot *localSnap = [accumulator awaitEventWithName:@"local event"];
  241. XCTAssertTrue(localSnap.metadata.hasPendingWrites);
  242. XCTAssertEqualObjects(localSnap.data, (@{ @"a" : @1, @"b" : @2, @"when" : [NSNull null] }));
  243. FIRDocumentSnapshot *serverSnap = [accumulator awaitEventWithName:@"server event"];
  244. XCTAssertFalse(serverSnap.metadata.hasPendingWrites);
  245. NSDate *when = serverSnap[@"when"];
  246. XCTAssertEqualObjects(serverSnap.data, (@{ @"a" : @1, @"b" : @2, @"when" : when }));
  247. }
  248. - (void)testUpdateFieldsWithDots {
  249. FIRDocumentReference *doc = [self documentRef];
  250. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  251. FIRWriteBatch *batch = [doc.firestore batch];
  252. [batch setData:@{@"a.b" : @"old", @"c.d" : @"old"} forDocument:doc];
  253. [batch updateData:@{
  254. [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"
  255. }
  256. forDocument:doc];
  257. [batch commitWithCompletion:^(NSError *_Nullable error) {
  258. XCTAssertNil(error);
  259. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  260. XCTAssertNil(error);
  261. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  262. }];
  263. [expectation fulfill];
  264. }];
  265. [self awaitExpectations];
  266. }
  267. - (void)testUpdateNestedFields {
  268. FIRDocumentReference *doc = [self documentRef];
  269. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  270. FIRWriteBatch *batch = [doc.firestore batch];
  271. [batch setData:@{
  272. @"a" : @{@"b" : @"old"},
  273. @"c" : @{@"d" : @"old"},
  274. @"e" : @{@"f" : @"old"}
  275. }
  276. forDocument:doc];
  277. [batch updateData:@{
  278. @"a.b" : @"new",
  279. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  280. }
  281. forDocument:doc];
  282. [batch commitWithCompletion:^(NSError *_Nullable error) {
  283. XCTAssertNil(error);
  284. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  285. XCTAssertNil(error);
  286. XCTAssertEqualObjects(snapshot.data, (@{
  287. @"a" : @{@"b" : @"new"},
  288. @"c" : @{@"d" : @"new"},
  289. @"e" : @{@"f" : @"old"}
  290. }));
  291. }];
  292. [expectation fulfill];
  293. }];
  294. [self awaitExpectations];
  295. }
  296. @end