FSTTransactionTests.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. #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:@{
  183. @"c" : @"d",
  184. @"nested" : @{@"c" : @"d"}
  185. }
  186. forDocument:doc
  187. options:[FIRSetOptions merge]];
  188. return @YES;
  189. }
  190. completion:^(id _Nullable result, NSError *_Nullable error) {
  191. XCTAssertEqualObjects(@YES, result);
  192. XCTAssertNil(error);
  193. [expectation fulfill];
  194. }];
  195. [self awaitExpectations];
  196. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  197. XCTAssertEqualObjects(
  198. snapshot.data, (
  199. @{ @"a" : @"b",
  200. @"c" : @"d",
  201. @"nested" : @{@"a" : @"b", @"c" : @"d"} }));
  202. }
  203. - (void)testCannotUpdateNonExistentDocument {
  204. FIRFirestore *firestore = [self firestore];
  205. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  206. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  207. [firestore 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 runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  231. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  232. XCTAssertNil(*error);
  233. int32_t nowStarted = OSAtomicIncrement32(&started);
  234. // Once all of the transactions have read, allow the first write.
  235. if (nowStarted == total) {
  236. dispatch_semaphore_signal(writeBarrier);
  237. }
  238. dispatch_semaphore_wait(writeBarrier, DISPATCH_TIME_FOREVER);
  239. // Refill the barrier so that the other transactions and retries succeed.
  240. dispatch_semaphore_signal(writeBarrier);
  241. double newCount = ((NSNumber *)snapshot[@"count"]).doubleValue + 1.0;
  242. [transaction setData:@{ @"count" : @(newCount) } forDocument:doc];
  243. return @YES;
  244. }
  245. completion:^(id _Nullable result, NSError *_Nullable error) {
  246. [expectation fulfill];
  247. }];
  248. }
  249. [self awaitExpectations];
  250. // Now all transaction should be completed, so check the result.
  251. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  252. XCTAssertEqualObjects(@(5.0 + total), snapshot[@"count"]);
  253. }
  254. - (void)testUpdateTransactionally {
  255. // A barrier to make sure every transaction reaches the same spot.
  256. dispatch_semaphore_t writeBarrier = dispatch_semaphore_create(0);
  257. __block volatile int32_t started = 0;
  258. FIRFirestore *firestore = [self firestore];
  259. FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  260. [self writeDocumentRef:doc data:@{ @"count" : @(5.0), @"other" : @"yes" }];
  261. // Make 3 transactions that will all increment.
  262. int total = 3;
  263. for (int i = 0; i < total; i++) {
  264. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  265. [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  266. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  267. XCTAssertNil(*error);
  268. int32_t nowStarted = OSAtomicIncrement32(&started);
  269. // Once all of the transactions have read, allow the first write.
  270. if (nowStarted == total) {
  271. dispatch_semaphore_signal(writeBarrier);
  272. }
  273. dispatch_semaphore_wait(writeBarrier, DISPATCH_TIME_FOREVER);
  274. // Refill the barrier so that the other transactions and retries succeed.
  275. dispatch_semaphore_signal(writeBarrier);
  276. double newCount = ((NSNumber *)snapshot[@"count"]).doubleValue + 1.0;
  277. [transaction updateData:@{ @"count" : @(newCount) } forDocument:doc];
  278. return @YES;
  279. }
  280. completion:^(id _Nullable result, NSError *_Nullable error) {
  281. [expectation fulfill];
  282. }];
  283. }
  284. [self awaitExpectations];
  285. // Now all transaction should be completed, so check the result.
  286. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  287. XCTAssertEqualObjects(@(5.0 + total), snapshot[@"count"]);
  288. XCTAssertEqualObjects(@"yes", snapshot[@"other"]);
  289. }
  290. // We currently require every document read to also be written.
  291. // TODO(b/34879758): Re-enable this test once we fix it.
  292. - (void)xtestHandleReadingOneDocAndWritingAnother {
  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 runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  299. // Get the first doc.
  300. [transaction getDocument:doc1 error:error];
  301. XCTAssertNil(*error);
  302. // Do a write outside of the transaction. The first time the
  303. // transaction is tried, this will bump the version, which
  304. // will cause the write to doc2 to fail. The second time, it
  305. // will be a no-op and not bump the version.
  306. dispatch_semaphore_t writeSemaphore = dispatch_semaphore_create(0);
  307. [doc1 setData:@{
  308. @"count" : @(1234)
  309. }
  310. completion:^(NSError *_Nullable error) {
  311. dispatch_semaphore_signal(writeSemaphore);
  312. }];
  313. // We can block on it, because transactions run on a background queue.
  314. dispatch_semaphore_wait(writeSemaphore, DISPATCH_TIME_FOREVER);
  315. // Now try to update the other doc from within the transaction.
  316. // This should fail once, because we read 15 earlier.
  317. [transaction setData:@{ @"count" : @(16) } forDocument:doc2];
  318. return nil;
  319. }
  320. completion:^(id _Nullable result, NSError *_Nullable error) {
  321. // We currently require every document read to also be written.
  322. // TODO(b/34879758): Add this check back once we drop that.
  323. // NSError *error = nil;
  324. // FIRDocument *snapshot = [transaction getDocument:doc1 error:&error];
  325. // XCTAssertNil(error);
  326. // XCTAssertEquals(0, tries);
  327. // XCTAssertEqualObjects(@(1234), snapshot[@"count"]);
  328. // snapshot = [transaction getDocument:doc2 error:&error];
  329. // XCTAssertNil(error);
  330. // XCTAssertEqualObjects(@(16), snapshot[@"count"]);
  331. XCTAssertNotNil(error);
  332. [expectation fulfill];
  333. }];
  334. [self awaitExpectations];
  335. }
  336. - (void)testReadingADocTwiceWithDifferentVersions {
  337. FIRFirestore *firestore = [self firestore];
  338. FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  339. [self writeDocumentRef:doc data:@{ @"count" : @(15.0) }];
  340. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  341. [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  342. // Get the doc once.
  343. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  344. XCTAssertNil(*error);
  345. XCTAssertEqualObjects(@(15), snapshot[@"count"]);
  346. // Do a write outside of the transaction.
  347. dispatch_semaphore_t writeSemaphore = dispatch_semaphore_create(0);
  348. [doc setData:@{
  349. @"count" : @(1234)
  350. }
  351. completion:^(NSError *_Nullable error) {
  352. dispatch_semaphore_signal(writeSemaphore);
  353. }];
  354. // We can block on it, because transactions run on a background queue.
  355. dispatch_semaphore_wait(writeSemaphore, DISPATCH_TIME_FOREVER);
  356. // Get the doc again in the transaction with the new version.
  357. snapshot = [transaction getDocument:doc error:error];
  358. // The get itself will fail, because we already read an earlier version of this document.
  359. // TODO(klimt): Perhaps we shouldn't fail reads for this, but should wait and fail the
  360. // whole transaction? It's an edge-case anyway, as developers shouldn't be reading the same
  361. // do multiple times. But they need to handle read errors anyway.
  362. XCTAssertNotNil(*error);
  363. return nil;
  364. }
  365. completion:^(id _Nullable result, NSError *_Nullable error) {
  366. [expectation fulfill];
  367. }];
  368. [self awaitExpectations];
  369. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  370. XCTAssertEqualObjects(@(1234.0), snapshot[@"count"]);
  371. }
  372. // We currently require every document read to also be written.
  373. // TODO(b/34879758): Add this test back once we fix that.
  374. - (void)xtestCannotHaveAGetWithoutMutations {
  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 runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  380. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  381. XCTAssertTrue(snapshot.exists);
  382. XCTAssertNil(*error);
  383. return nil;
  384. }
  385. completion:^(id _Nullable result, NSError *_Nullable error) {
  386. XCTAssertNotNil(error);
  387. [expectation fulfill];
  388. }];
  389. [self awaitExpectations];
  390. }
  391. - (void)testSuccessWithNoTransactionOperations {
  392. FIRFirestore *firestore = [self firestore];
  393. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  394. [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  395. return @"yes";
  396. }
  397. completion:^(id _Nullable result, NSError *_Nullable error) {
  398. XCTAssertEqualObjects(@"yes", result);
  399. XCTAssertNil(error);
  400. [expectation fulfill];
  401. }];
  402. [self awaitExpectations];
  403. }
  404. - (void)testCancellationOnError {
  405. FIRFirestore *firestore = [self firestore];
  406. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  407. __block volatile int32_t count = 0;
  408. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  409. [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  410. OSAtomicIncrement32(&count);
  411. [transaction setData:@{@"foo" : @"bar"} forDocument:doc];
  412. *error = [NSError errorWithDomain:NSCocoaErrorDomain code:35 userInfo:@{}];
  413. return nil;
  414. }
  415. completion:^(id _Nullable result, NSError *_Nullable error) {
  416. XCTAssertNil(result);
  417. XCTAssertNotNil(error);
  418. XCTAssertEqual(35, error.code);
  419. [expectation fulfill];
  420. }];
  421. [self awaitExpectations];
  422. XCTAssertEqual(1, (int)count);
  423. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  424. XCTAssertFalse(snapshot.exists);
  425. }
  426. - (void)testUpdateFieldsWithDotsTransactionally {
  427. FIRDocumentReference *doc = [self documentRef];
  428. XCTestExpectation *expectation =
  429. [self expectationWithDescription:@"testUpdateFieldsWithDotsTransactionally"];
  430. [doc.firestore
  431. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  432. XCTAssertNil(*error);
  433. [transaction setData:@{@"a.b" : @"old", @"c.d" : @"old"} forDocument:doc];
  434. [transaction updateData:@{
  435. [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"
  436. }
  437. forDocument:doc];
  438. return nil;
  439. }
  440. completion:^(id result, NSError *error) {
  441. XCTAssertNil(error);
  442. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  443. XCTAssertNil(error);
  444. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  445. }];
  446. [expectation fulfill];
  447. }];
  448. [self awaitExpectations];
  449. }
  450. - (void)testUpdateNestedFieldsTransactionally {
  451. FIRDocumentReference *doc = [self documentRef];
  452. XCTestExpectation *expectation =
  453. [self expectationWithDescription:@"testUpdateNestedFieldsTransactionally"];
  454. [doc.firestore
  455. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  456. XCTAssertNil(*error);
  457. [transaction setData:@{
  458. @"a" : @{@"b" : @"old"},
  459. @"c" : @{@"d" : @"old"},
  460. @"e" : @{@"f" : @"old"}
  461. }
  462. forDocument:doc];
  463. [transaction updateData:@{
  464. @"a.b" : @"new",
  465. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  466. }
  467. forDocument:doc];
  468. return nil;
  469. }
  470. completion:^(id result, NSError *error) {
  471. XCTAssertNil(error);
  472. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  473. XCTAssertNil(error);
  474. XCTAssertEqualObjects(snapshot.data, (@{
  475. @"a" : @{@"b" : @"new"},
  476. @"c" : @{@"d" : @"new"},
  477. @"e" : @{@"f" : @"old"}
  478. }));
  479. }];
  480. [expectation fulfill];
  481. }];
  482. [self awaitExpectations];
  483. }
  484. @end