FSTTransactionTests.mm 22 KB

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