FSTTransactionTests.mm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #include <libkern/OSAtomic.h>
  19. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  20. @interface FSTTransactionTests : FSTIntegrationTestCase
  21. @end
  22. @implementation FSTTransactionTests
  23. - (void)testGetDocuments {
  24. FIRFirestore *firestore = [self firestore];
  25. FIRDocumentReference *doc = [[firestore collectionWithPath:@"spaces"] documentWithAutoID];
  26. [self writeDocumentRef:doc data:@{@"foo" : @1, @"desc" : @"Stuff", @"owner" : @"Jonny"}];
  27. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  28. [firestore
  29. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  30. [transaction getDocument:doc error:error];
  31. XCTAssertNil(*error);
  32. return @YES;
  33. }
  34. completion:^(id _Nullable result, NSError *_Nullable error) {
  35. XCTAssertNil(result);
  36. // We currently require every document read to also be written.
  37. // TODO(b/34879758): Fix this check once we drop that requirement.
  38. XCTAssertNotNil(error);
  39. [expectation fulfill];
  40. }];
  41. [self awaitExpectations];
  42. }
  43. - (void)testDeleteDocument {
  44. FIRFirestore *firestore = [self firestore];
  45. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  46. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  47. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  48. XCTAssertEqualObjects(@"bar", snapshot[@"foo"]);
  49. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  50. [firestore
  51. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  52. [transaction deleteDocument:doc];
  53. return @YES;
  54. }
  55. completion:^(id _Nullable result, NSError *_Nullable error) {
  56. XCTAssertEqualObjects(@YES, result);
  57. XCTAssertNil(error);
  58. [expectation fulfill];
  59. }];
  60. [self awaitExpectations];
  61. snapshot = [self readDocumentForRef:doc];
  62. XCTAssertFalse(snapshot.exists);
  63. }
  64. - (void)testGetNonexistentDocumentThenCreate {
  65. FIRFirestore *firestore = [self firestore];
  66. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  67. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  68. [firestore
  69. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  70. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  71. XCTAssertNil(*error);
  72. XCTAssertNotNil(snapshot);
  73. XCTAssertFalse(snapshot.exists);
  74. [transaction setData:@{@"foo" : @"bar"} forDocument:doc];
  75. return @YES;
  76. }
  77. completion:^(id _Nullable result, NSError *_Nullable error) {
  78. XCTAssertEqualObjects(@YES, result);
  79. XCTAssertNil(error);
  80. [expectation fulfill];
  81. }];
  82. [self awaitExpectations];
  83. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  84. XCTAssertTrue(snapshot.exists);
  85. XCTAssertEqualObjects(@"bar", snapshot[@"foo"]);
  86. }
  87. - (void)testGetNonexistentDocumentThenFailPatch {
  88. FIRFirestore *firestore = [self firestore];
  89. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  90. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  91. [firestore
  92. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  93. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  94. XCTAssertNil(*error);
  95. XCTAssertFalse(snapshot.exists);
  96. [transaction updateData:@{@"foo" : @"bar"} forDocument:doc];
  97. return @YES;
  98. }
  99. completion:^(id _Nullable result, NSError *_Nullable error) {
  100. XCTAssertNil(result);
  101. XCTAssertNotNil(error);
  102. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  103. // TODO(dimond): This is probably the wrong error code, but it's what we use today. We
  104. // should update the code once the underlying error was fixed.
  105. XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
  106. [expectation fulfill];
  107. }];
  108. [self awaitExpectations];
  109. }
  110. - (void)testDeleteDocumentAndPatch {
  111. FIRFirestore *firestore = [self firestore];
  112. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  113. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  114. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  115. [firestore
  116. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
  117. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  118. XCTAssertNil(*error);
  119. XCTAssertTrue(snapshot.exists);
  120. [transaction deleteDocument:doc];
  121. // Since we deleted the doc, the update will fail
  122. [transaction updateData:@{@"foo" : @"bar"} forDocument:doc];
  123. return @YES;
  124. }
  125. completion:^(id _Nullable result, NSError *_Nullable error) {
  126. XCTAssertNil(result);
  127. XCTAssertNotNil(error);
  128. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  129. // TODO(dimond): This is probably the wrong error code, but it's what we use today. We
  130. // should update the code once the underlying error was fixed.
  131. XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
  132. [expectation fulfill];
  133. }];
  134. [self awaitExpectations];
  135. }
  136. - (void)testDeleteDocumentAndSet {
  137. FIRFirestore *firestore = [self firestore];
  138. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  139. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  140. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  141. [firestore
  142. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
  143. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  144. XCTAssertNil(*error);
  145. XCTAssertTrue(snapshot.exists);
  146. [transaction deleteDocument:doc];
  147. // TODO(dimond): In theory this should work, but it's complex to make it work, so instead we
  148. // just let the transaction fail and verify it's unsupported for now
  149. [transaction setData:@{@"foo" : @"new-bar"} forDocument:doc];
  150. return @YES;
  151. }
  152. completion:^(id _Nullable result, NSError *_Nullable error) {
  153. XCTAssertNil(result);
  154. XCTAssertNotNil(error);
  155. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  156. // TODO(dimond): This is probably the wrong error code, but it's what we use today. We
  157. // should update the code once the underlying error was fixed.
  158. XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
  159. [expectation fulfill];
  160. }];
  161. [self awaitExpectations];
  162. }
  163. - (void)testWriteDocumentTwice {
  164. FIRFirestore *firestore = [self firestore];
  165. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  166. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  167. [firestore
  168. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
  169. [transaction setData:@{@"a" : @"b"} forDocument:doc];
  170. [transaction setData:@{@"c" : @"d"} forDocument:doc];
  171. return @YES;
  172. }
  173. completion:^(id _Nullable result, NSError *_Nullable error) {
  174. XCTAssertEqualObjects(@YES, result);
  175. XCTAssertNil(error);
  176. [expectation fulfill];
  177. }];
  178. [self awaitExpectations];
  179. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  180. XCTAssertEqualObjects(snapshot.data, @{@"c" : @"d"});
  181. }
  182. - (void)testSetDocumentWithMerge {
  183. FIRFirestore *firestore = [self firestore];
  184. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  185. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  186. [firestore
  187. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  188. [transaction setData:@{@"a" : @"b", @"nested" : @{@"a" : @"b"}} forDocument:doc];
  189. [transaction setData:@{@"c" : @"d", @"nested" : @{@"c" : @"d"}} forDocument:doc merge:YES];
  190. return @YES;
  191. }
  192. completion:^(id _Nullable result, NSError *_Nullable error) {
  193. XCTAssertEqualObjects(@YES, result);
  194. XCTAssertNil(error);
  195. [expectation fulfill];
  196. }];
  197. [self awaitExpectations];
  198. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  199. XCTAssertEqualObjects(snapshot.data,
  200. (@{@"a" : @"b", @"c" : @"d", @"nested" : @{@"a" : @"b", @"c" : @"d"}}));
  201. }
  202. - (void)testCannotUpdateNonExistentDocument {
  203. FIRFirestore *firestore = [self firestore];
  204. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  205. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  206. [firestore
  207. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  208. [transaction updateData:@{@"foo" : @"bar"} forDocument:doc];
  209. return nil;
  210. }
  211. completion:^(id _Nullable result, NSError *_Nullable error) {
  212. XCTAssertNotNil(error);
  213. [expectation fulfill];
  214. }];
  215. [self awaitExpectations];
  216. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  217. XCTAssertFalse(result.exists);
  218. }
  219. - (void)testIncrementTransactionally {
  220. if ([FSTIntegrationTestCase isRunningAgainstEmulator]) return;
  221. // A barrier to make sure every transaction reaches the same spot.
  222. dispatch_semaphore_t writeBarrier = dispatch_semaphore_create(0);
  223. __block volatile int32_t started = 0;
  224. FIRFirestore *firestore = [self firestore];
  225. FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  226. [self writeDocumentRef:doc data:@{@"count" : @(5.0)}];
  227. // Make 3 transactions that will all increment.
  228. int total = 3;
  229. for (int i = 0; i < total; i++) {
  230. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  231. [firestore
  232. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  233. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  234. XCTAssertNil(*error);
  235. int32_t nowStarted = OSAtomicIncrement32(&started);
  236. // Once all of the transactions have read, allow the first write.
  237. if (nowStarted == total) {
  238. dispatch_semaphore_signal(writeBarrier);
  239. }
  240. dispatch_semaphore_wait(writeBarrier, DISPATCH_TIME_FOREVER);
  241. // Refill the barrier so that the other transactions and retries succeed.
  242. dispatch_semaphore_signal(writeBarrier);
  243. double newCount = ((NSNumber *)snapshot[@"count"]).doubleValue + 1.0;
  244. [transaction setData:@{@"count" : @(newCount)} forDocument:doc];
  245. return @YES;
  246. }
  247. completion:^(id _Nullable result, NSError *_Nullable error) {
  248. [expectation fulfill];
  249. }];
  250. }
  251. [self awaitExpectations];
  252. // Now all transaction should be completed, so check the result.
  253. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  254. XCTAssertEqualObjects(@(5.0 + total), snapshot[@"count"]);
  255. }
  256. - (void)testUpdateTransactionally {
  257. if ([FSTIntegrationTestCase isRunningAgainstEmulator]) return;
  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. // doc 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. if ([FSTIntegrationTestCase isRunningAgainstEmulator]) return;
  412. FIRFirestore *firestore = [self firestore];
  413. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  414. __block volatile int32_t count = 0;
  415. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  416. [firestore
  417. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  418. OSAtomicIncrement32(&count);
  419. [transaction setData:@{@"foo" : @"bar"} forDocument:doc];
  420. if (error) {
  421. *error = [NSError errorWithDomain:NSCocoaErrorDomain code:35 userInfo:@{}];
  422. }
  423. return nil;
  424. }
  425. completion:^(id _Nullable result, NSError *_Nullable error) {
  426. XCTAssertNil(result);
  427. XCTAssertNotNil(error);
  428. XCTAssertEqual(35, error.code);
  429. [expectation fulfill];
  430. }];
  431. [self awaitExpectations];
  432. XCTAssertEqual(1, (int)count);
  433. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  434. XCTAssertFalse(snapshot.exists);
  435. }
  436. - (void)testUpdateFieldsWithDotsTransactionally {
  437. if ([FSTIntegrationTestCase isRunningAgainstEmulator]) return;
  438. FIRDocumentReference *doc = [self documentRef];
  439. XCTestExpectation *expectation =
  440. [self expectationWithDescription:@"testUpdateFieldsWithDotsTransactionally"];
  441. [doc.firestore
  442. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  443. XCTAssertNil(*error);
  444. [transaction setData:@{@"a.b" : @"old", @"c.d" : @"old"} forDocument:doc];
  445. [transaction updateData:@{
  446. [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"
  447. }
  448. forDocument:doc];
  449. return nil;
  450. }
  451. completion:^(id result, NSError *error) {
  452. XCTAssertNil(error);
  453. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  454. XCTAssertNil(error);
  455. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  456. }];
  457. [expectation fulfill];
  458. }];
  459. [self awaitExpectations];
  460. }
  461. - (void)testUpdateNestedFieldsTransactionally {
  462. FIRDocumentReference *doc = [self documentRef];
  463. XCTestExpectation *expectation =
  464. [self expectationWithDescription:@"testUpdateNestedFieldsTransactionally"];
  465. [doc.firestore
  466. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  467. XCTAssertNil(*error);
  468. [transaction setData:@{
  469. @"a" : @{@"b" : @"old"},
  470. @"c" : @{@"d" : @"old"},
  471. @"e" : @{@"f" : @"old"}
  472. }
  473. forDocument:doc];
  474. [transaction updateData:@{
  475. @"a.b" : @"new",
  476. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  477. }
  478. forDocument:doc];
  479. return nil;
  480. }
  481. completion:^(id result, NSError *error) {
  482. XCTAssertNil(error);
  483. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  484. XCTAssertNil(error);
  485. XCTAssertEqualObjects(snapshot.data, (@{
  486. @"a" : @{@"b" : @"new"},
  487. @"c" : @{@"d" : @"new"},
  488. @"e" : @{@"f" : @"old"}
  489. }));
  490. }];
  491. [expectation fulfill];
  492. }];
  493. [self awaitExpectations];
  494. }
  495. @end