FSTTransactionTests.mm 22 KB

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