FSTTransactionTests.mm 22 KB

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