FIRWriteBatchTests.m 13 KB

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