FSTTransactionTests.mm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. #include <libkern/OSAtomic.h>
  19. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  20. @interface FSTTransactionTests : FSTIntegrationTestCase
  21. @end
  22. @implementation FSTTransactionTests
  23. - (void)testGetDocuments {
  24. FIRFirestore *firestore = [self firestore];
  25. FIRDocumentReference *doc = [[firestore collectionWithPath:@"spaces"] documentWithAutoID];
  26. [self writeDocumentRef:doc data:@{@"foo" : @1, @"desc" : @"Stuff", @"owner" : @"Jonny"}];
  27. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  28. [firestore
  29. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  30. [transaction getDocument:doc error:error];
  31. XCTAssertNil(*error);
  32. return @YES;
  33. }
  34. completion:^(id _Nullable result, NSError *_Nullable error) {
  35. XCTAssertNil(result);
  36. // We currently require every document read to also be written.
  37. // TODO(b/34879758): Fix this check once we drop that requirement.
  38. XCTAssertNotNil(error);
  39. [expectation fulfill];
  40. }];
  41. [self awaitExpectations];
  42. }
  43. - (void)testDeleteDocument {
  44. FIRFirestore *firestore = [self firestore];
  45. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  46. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  47. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  48. XCTAssertEqualObjects(@"bar", snapshot[@"foo"]);
  49. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  50. [firestore
  51. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  52. [transaction deleteDocument:doc];
  53. return @YES;
  54. }
  55. completion:^(id _Nullable result, NSError *_Nullable error) {
  56. XCTAssertEqualObjects(@YES, result);
  57. XCTAssertNil(error);
  58. [expectation fulfill];
  59. }];
  60. [self awaitExpectations];
  61. snapshot = [self readDocumentForRef:doc];
  62. XCTAssertFalse(snapshot.exists);
  63. }
  64. - (void)testGetNonexistentDocumentThenCreate {
  65. FIRFirestore *firestore = [self firestore];
  66. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  67. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  68. [firestore
  69. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  70. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  71. XCTAssertNil(*error);
  72. XCTAssertNotNil(snapshot);
  73. XCTAssertFalse(snapshot.exists);
  74. [transaction setData:@{@"foo" : @"bar"} forDocument:doc];
  75. return @YES;
  76. }
  77. completion:^(id _Nullable result, NSError *_Nullable error) {
  78. XCTAssertEqualObjects(@YES, result);
  79. XCTAssertNil(error);
  80. [expectation fulfill];
  81. }];
  82. [self awaitExpectations];
  83. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  84. XCTAssertTrue(snapshot.exists);
  85. XCTAssertEqualObjects(@"bar", snapshot[@"foo"]);
  86. }
  87. - (void)testGetNonexistentDocumentThenFailPatch {
  88. FIRFirestore *firestore = [self firestore];
  89. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  90. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  91. [firestore
  92. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  93. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  94. XCTAssertNil(*error);
  95. XCTAssertFalse(snapshot.exists);
  96. [transaction updateData:@{@"foo" : @"bar"} forDocument:doc];
  97. return @YES;
  98. }
  99. completion:^(id _Nullable result, NSError *_Nullable error) {
  100. XCTAssertNil(result);
  101. XCTAssertNotNil(error);
  102. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  103. // TODO(dimond): This is probably the wrong error code, but it's what we use today. We
  104. // should update the code once the underlying error was fixed.
  105. XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
  106. [expectation fulfill];
  107. }];
  108. [self awaitExpectations];
  109. }
  110. - (void)testDeleteDocumentAndPatch {
  111. FIRFirestore *firestore = [self firestore];
  112. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  113. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  114. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  115. [firestore
  116. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
  117. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  118. XCTAssertNil(*error);
  119. XCTAssertTrue(snapshot.exists);
  120. [transaction deleteDocument:doc];
  121. // Since we deleted the doc, the update will fail
  122. [transaction updateData:@{@"foo" : @"bar"} forDocument:doc];
  123. return @YES;
  124. }
  125. completion:^(id _Nullable result, NSError *_Nullable error) {
  126. XCTAssertNil(result);
  127. XCTAssertNotNil(error);
  128. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  129. // TODO(dimond): This is probably the wrong error code, but it's what we use today. We
  130. // should update the code once the underlying error was fixed.
  131. XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
  132. [expectation fulfill];
  133. }];
  134. [self awaitExpectations];
  135. }
  136. - (void)testDeleteDocumentAndSet {
  137. FIRFirestore *firestore = [self firestore];
  138. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  139. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  140. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  141. [firestore
  142. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
  143. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  144. XCTAssertNil(*error);
  145. XCTAssertTrue(snapshot.exists);
  146. [transaction deleteDocument:doc];
  147. // TODO(dimond): In theory this should work, but it's complex to make it work, so instead we
  148. // just let the transaction fail and verify it's unsupported for now
  149. [transaction setData:@{@"foo" : @"new-bar"} forDocument:doc];
  150. return @YES;
  151. }
  152. completion:^(id _Nullable result, NSError *_Nullable error) {
  153. XCTAssertNil(result);
  154. XCTAssertNotNil(error);
  155. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  156. // TODO(dimond): This is probably the wrong error code, but it's what we use today. We
  157. // should update the code once the underlying error was fixed.
  158. XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
  159. [expectation fulfill];
  160. }];
  161. [self awaitExpectations];
  162. }
  163. - (void)testWriteDocumentTwice {
  164. FIRFirestore *firestore = [self firestore];
  165. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  166. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  167. [firestore
  168. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
  169. [transaction setData:@{@"a" : @"b"} forDocument:doc];
  170. [transaction setData:@{@"c" : @"d"} forDocument:doc];
  171. return @YES;
  172. }
  173. completion:^(id _Nullable result, NSError *_Nullable error) {
  174. XCTAssertEqualObjects(@YES, result);
  175. XCTAssertNil(error);
  176. [expectation fulfill];
  177. }];
  178. [self awaitExpectations];
  179. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  180. XCTAssertEqualObjects(snapshot.data, @{@"c" : @"d"});
  181. }
  182. - (void)testSetDocumentWithMerge {
  183. FIRFirestore *firestore = [self firestore];
  184. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  185. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  186. [firestore
  187. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  188. [transaction setData:@{@"a" : @"b", @"nested" : @{@"a" : @"b"}} forDocument:doc];
  189. [transaction setData:@{@"c" : @"d", @"nested" : @{@"c" : @"d"}} forDocument:doc merge:YES];
  190. return @YES;
  191. }
  192. completion:^(id _Nullable result, NSError *_Nullable error) {
  193. XCTAssertEqualObjects(@YES, result);
  194. XCTAssertNil(error);
  195. [expectation fulfill];
  196. }];
  197. [self awaitExpectations];
  198. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  199. XCTAssertEqualObjects(snapshot.data,
  200. (@{@"a" : @"b", @"c" : @"d", @"nested" : @{@"a" : @"b", @"c" : @"d"}}));
  201. }
  202. - (void)testCannotUpdateNonExistentDocument {
  203. FIRFirestore *firestore = [self firestore];
  204. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  205. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  206. [firestore
  207. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  208. [transaction updateData:@{@"foo" : @"bar"} forDocument:doc];
  209. return nil;
  210. }
  211. completion:^(id _Nullable result, NSError *_Nullable error) {
  212. XCTAssertNotNil(error);
  213. [expectation fulfill];
  214. }];
  215. [self awaitExpectations];
  216. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  217. XCTAssertFalse(result.exists);
  218. }
  219. - (void)testIncrementTransactionally {
  220. // A barrier to make sure every transaction reaches the same spot.
  221. dispatch_semaphore_t writeBarrier = dispatch_semaphore_create(0);
  222. __block volatile int32_t started = 0;
  223. FIRFirestore *firestore = [self firestore];
  224. FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  225. [self writeDocumentRef:doc data:@{@"count" : @(5.0)}];
  226. // Make 3 transactions that will all increment.
  227. int total = 3;
  228. for (int i = 0; i < total; i++) {
  229. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  230. [firestore
  231. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  232. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  233. XCTAssertNil(*error);
  234. int32_t nowStarted = OSAtomicIncrement32(&started);
  235. // Once all of the transactions have read, allow the first write.
  236. if (nowStarted == total) {
  237. dispatch_semaphore_signal(writeBarrier);
  238. }
  239. dispatch_semaphore_wait(writeBarrier, DISPATCH_TIME_FOREVER);
  240. // Refill the barrier so that the other transactions and retries succeed.
  241. dispatch_semaphore_signal(writeBarrier);
  242. double newCount = ((NSNumber *)snapshot[@"count"]).doubleValue + 1.0;
  243. [transaction setData:@{@"count" : @(newCount)} forDocument:doc];
  244. return @YES;
  245. }
  246. completion:^(id _Nullable result, NSError *_Nullable error) {
  247. [expectation fulfill];
  248. }];
  249. }
  250. [self awaitExpectations];
  251. // Now all transaction should be completed, so check the result.
  252. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  253. XCTAssertEqualObjects(@(5.0 + total), snapshot[@"count"]);
  254. }
  255. - (void)testUpdateTransactionally {
  256. // A barrier to make sure every transaction reaches the same spot.
  257. dispatch_semaphore_t writeBarrier = dispatch_semaphore_create(0);
  258. __block volatile int32_t started = 0;
  259. FIRFirestore *firestore = [self firestore];
  260. FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  261. [self writeDocumentRef:doc data:@{@"count" : @(5.0), @"other" : @"yes"}];
  262. // Make 3 transactions that will all increment.
  263. int total = 3;
  264. for (int i = 0; i < total; i++) {
  265. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  266. [firestore
  267. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  268. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  269. XCTAssertNil(*error);
  270. int32_t nowStarted = OSAtomicIncrement32(&started);
  271. // Once all of the transactions have read, allow the first write.
  272. if (nowStarted == total) {
  273. dispatch_semaphore_signal(writeBarrier);
  274. }
  275. dispatch_semaphore_wait(writeBarrier, DISPATCH_TIME_FOREVER);
  276. // Refill the barrier so that the other transactions and retries succeed.
  277. dispatch_semaphore_signal(writeBarrier);
  278. double newCount = ((NSNumber *)snapshot[@"count"]).doubleValue + 1.0;
  279. [transaction updateData:@{@"count" : @(newCount)} forDocument:doc];
  280. return @YES;
  281. }
  282. completion:^(id _Nullable result, NSError *_Nullable error) {
  283. [expectation fulfill];
  284. }];
  285. }
  286. [self awaitExpectations];
  287. // Now all transaction should be completed, so check the result.
  288. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  289. XCTAssertEqualObjects(@(5.0 + total), snapshot[@"count"]);
  290. XCTAssertEqualObjects(@"yes", snapshot[@"other"]);
  291. }
  292. - (void)testHandleReadingOneDocAndWritingAnother {
  293. FIRFirestore *firestore = [self firestore];
  294. FIRDocumentReference *doc1 = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  295. FIRDocumentReference *doc2 = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  296. [self writeDocumentRef:doc1 data:@{@"count" : @(15.0)}];
  297. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  298. [firestore
  299. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  300. // Get the first doc.
  301. [transaction getDocument:doc1 error:error];
  302. XCTAssertNil(*error);
  303. // Do a write outside of the transaction. The first time the
  304. // transaction is tried, this will bump the version, which
  305. // will cause the write to doc2 to fail. The second time, it
  306. // will be a no-op and not bump the version.
  307. dispatch_semaphore_t writeSemaphore = dispatch_semaphore_create(0);
  308. [doc1 setData:@{
  309. @"count" : @(1234)
  310. }
  311. completion:^(NSError *_Nullable error) {
  312. dispatch_semaphore_signal(writeSemaphore);
  313. }];
  314. // We can block on it, because transactions run on a background queue.
  315. dispatch_semaphore_wait(writeSemaphore, DISPATCH_TIME_FOREVER);
  316. // Now try to update the other doc from within the transaction.
  317. // This should fail once, because we read 15 earlier.
  318. [transaction setData:@{@"count" : @(16)} forDocument:doc2];
  319. return nil;
  320. }
  321. completion:^(id _Nullable result, NSError *_Nullable error) {
  322. // We currently require every document read to also be written.
  323. // TODO(b/34879758): Add this check back once we drop that.
  324. // NSError *error = nil;
  325. // FIRDocument *snapshot = [transaction getDocument:doc1 error:&error];
  326. // XCTAssertNil(error);
  327. // XCTAssertEquals(0, tries);
  328. // XCTAssertEqualObjects(@(1234), snapshot[@"count"]);
  329. // snapshot = [transaction getDocument:doc2 error:&error];
  330. // XCTAssertNil(error);
  331. // XCTAssertEqualObjects(@(16), snapshot[@"count"]);
  332. XCTAssertNotNil(error);
  333. [expectation fulfill];
  334. }];
  335. [self awaitExpectations];
  336. }
  337. - (void)testReadingADocTwiceWithDifferentVersions {
  338. FIRFirestore *firestore = [self firestore];
  339. FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  340. [self writeDocumentRef:doc data:@{@"count" : @(15.0)}];
  341. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  342. [firestore
  343. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  344. // Get the doc once.
  345. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  346. XCTAssertNil(*error);
  347. XCTAssertEqualObjects(@(15), snapshot[@"count"]);
  348. // Do a write outside of the transaction.
  349. dispatch_semaphore_t writeSemaphore = dispatch_semaphore_create(0);
  350. [doc setData:@{
  351. @"count" : @(1234)
  352. }
  353. completion:^(NSError *_Nullable error) {
  354. dispatch_semaphore_signal(writeSemaphore);
  355. }];
  356. // We can block on it, because transactions run on a background queue.
  357. dispatch_semaphore_wait(writeSemaphore, DISPATCH_TIME_FOREVER);
  358. // Get the doc again in the transaction with the new version.
  359. snapshot = [transaction getDocument:doc error:error];
  360. // The get itself will fail, because we already read an earlier version of this document.
  361. // TODO(klimt): Perhaps we shouldn't fail reads for this, but should wait and fail the
  362. // whole transaction? It's an edge-case anyway, as developers shouldn't be reading the same
  363. // do multiple times. But they need to handle read errors anyway.
  364. XCTAssertNotNil(*error);
  365. return nil;
  366. }
  367. completion:^(id _Nullable result, NSError *_Nullable error) {
  368. [expectation fulfill];
  369. }];
  370. [self awaitExpectations];
  371. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  372. XCTAssertEqualObjects(@(1234.0), snapshot[@"count"]);
  373. }
  374. - (void)testCannotHaveAGetWithoutMutations {
  375. FIRFirestore *firestore = [self firestore];
  376. FIRDocumentReference *doc = [[firestore collectionWithPath:@"foo"] documentWithAutoID];
  377. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  378. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  379. [firestore
  380. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  381. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  382. XCTAssertTrue(snapshot.exists);
  383. XCTAssertNil(*error);
  384. return nil;
  385. }
  386. completion:^(id _Nullable result, NSError *_Nullable error) {
  387. // We currently require every document read to also be written.
  388. // TODO(b/34879758): Fix this check once we drop that requirement.
  389. XCTAssertNotNil(error);
  390. [expectation fulfill];
  391. }];
  392. [self awaitExpectations];
  393. }
  394. - (void)testSuccessWithNoTransactionOperations {
  395. FIRFirestore *firestore = [self firestore];
  396. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  397. [firestore
  398. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  399. return @"yes";
  400. }
  401. completion:^(id _Nullable result, NSError *_Nullable error) {
  402. XCTAssertEqualObjects(@"yes", result);
  403. XCTAssertNil(error);
  404. [expectation fulfill];
  405. }];
  406. [self awaitExpectations];
  407. }
  408. - (void)testCancellationOnError {
  409. FIRFirestore *firestore = [self firestore];
  410. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  411. __block volatile int32_t count = 0;
  412. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  413. [firestore
  414. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  415. OSAtomicIncrement32(&count);
  416. [transaction setData:@{@"foo" : @"bar"} forDocument:doc];
  417. if (error) {
  418. *error = [NSError errorWithDomain:NSCocoaErrorDomain code:35 userInfo:@{}];
  419. }
  420. return nil;
  421. }
  422. completion:^(id _Nullable result, NSError *_Nullable error) {
  423. XCTAssertNil(result);
  424. XCTAssertNotNil(error);
  425. XCTAssertEqual(35, error.code);
  426. [expectation fulfill];
  427. }];
  428. [self awaitExpectations];
  429. XCTAssertEqual(1, (int)count);
  430. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  431. XCTAssertFalse(snapshot.exists);
  432. }
  433. - (void)testUpdateFieldsWithDotsTransactionally {
  434. FIRDocumentReference *doc = [self documentRef];
  435. XCTestExpectation *expectation =
  436. [self expectationWithDescription:@"testUpdateFieldsWithDotsTransactionally"];
  437. [doc.firestore
  438. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  439. XCTAssertNil(*error);
  440. [transaction setData:@{@"a.b" : @"old", @"c.d" : @"old"} forDocument:doc];
  441. [transaction updateData:@{
  442. [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"
  443. }
  444. forDocument:doc];
  445. return nil;
  446. }
  447. completion:^(id result, NSError *error) {
  448. XCTAssertNil(error);
  449. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  450. XCTAssertNil(error);
  451. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  452. }];
  453. [expectation fulfill];
  454. }];
  455. [self awaitExpectations];
  456. }
  457. - (void)testUpdateNestedFieldsTransactionally {
  458. FIRDocumentReference *doc = [self documentRef];
  459. XCTestExpectation *expectation =
  460. [self expectationWithDescription:@"testUpdateNestedFieldsTransactionally"];
  461. [doc.firestore
  462. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  463. XCTAssertNil(*error);
  464. [transaction setData:@{
  465. @"a" : @{@"b" : @"old"},
  466. @"c" : @{@"d" : @"old"},
  467. @"e" : @{@"f" : @"old"}
  468. }
  469. forDocument:doc];
  470. [transaction updateData:@{
  471. @"a.b" : @"new",
  472. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  473. }
  474. forDocument:doc];
  475. return nil;
  476. }
  477. completion:^(id result, NSError *error) {
  478. XCTAssertNil(error);
  479. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  480. XCTAssertNil(error);
  481. XCTAssertEqualObjects(snapshot.data, (@{
  482. @"a" : @{@"b" : @"new"},
  483. @"c" : @{@"d" : @"new"},
  484. @"e" : @{@"f" : @"old"}
  485. }));
  486. }];
  487. [expectation fulfill];
  488. }];
  489. [self awaitExpectations];
  490. }
  491. @end