FSTTransactionTests.mm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  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
  52. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  53. [transaction deleteDocument:doc];
  54. return @YES;
  55. }
  56. completion:^(id _Nullable result, NSError *_Nullable error) {
  57. XCTAssertEqualObjects(@YES, result);
  58. XCTAssertNil(error);
  59. [expectation fulfill];
  60. }];
  61. [self awaitExpectations];
  62. snapshot = [self readDocumentForRef:doc];
  63. XCTAssertFalse(snapshot.exists);
  64. }
  65. - (void)testGetNonexistentDocumentThenCreate {
  66. FIRFirestore *firestore = [self firestore];
  67. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  68. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  69. [firestore
  70. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  71. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  72. XCTAssertNil(*error);
  73. XCTAssertNotNil(snapshot);
  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. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  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. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  129. [expectation fulfill];
  130. }];
  131. [self awaitExpectations];
  132. }
  133. - (void)testDeleteDocumentAndSet {
  134. FIRFirestore *firestore = [self firestore];
  135. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  136. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  137. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  138. [firestore
  139. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
  140. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  141. XCTAssertNil(*error);
  142. XCTAssertTrue(snapshot.exists);
  143. [transaction deleteDocument:doc];
  144. // TODO(dimond): In theory this should work, but it's complex to make it work, so instead we
  145. // just let the transaction fail and verify it's unsupported for now
  146. [transaction setData:@{@"foo" : @"new-bar"} forDocument:doc];
  147. return @YES;
  148. }
  149. completion:^(id _Nullable result, NSError *_Nullable error) {
  150. XCTAssertNil(result);
  151. XCTAssertNotNil(error);
  152. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  153. // This is the error surfaced by the backend.
  154. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  155. [expectation fulfill];
  156. }];
  157. [self awaitExpectations];
  158. }
  159. - (void)testWriteDocumentTwice {
  160. FIRFirestore *firestore = [self firestore];
  161. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  162. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  163. [firestore
  164. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
  165. [transaction setData:@{@"a" : @"b"} forDocument:doc];
  166. [transaction setData:@{@"c" : @"d"} forDocument:doc];
  167. return @YES;
  168. }
  169. completion:^(id _Nullable result, NSError *_Nullable error) {
  170. XCTAssertEqualObjects(@YES, result);
  171. XCTAssertNil(error);
  172. [expectation fulfill];
  173. }];
  174. [self awaitExpectations];
  175. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  176. XCTAssertEqualObjects(snapshot.data, @{@"c" : @"d"});
  177. }
  178. - (void)testSetDocumentWithMerge {
  179. FIRFirestore *firestore = [self firestore];
  180. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  181. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  182. [firestore
  183. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  184. [transaction setData:@{@"a" : @"b", @"nested" : @{@"a" : @"b"}} forDocument:doc];
  185. [transaction setData:@{@"c" : @"d", @"nested" : @{@"c" : @"d"}} forDocument:doc merge:YES];
  186. return @YES;
  187. }
  188. completion:^(id _Nullable result, NSError *_Nullable error) {
  189. XCTAssertEqualObjects(@YES, result);
  190. XCTAssertNil(error);
  191. [expectation fulfill];
  192. }];
  193. [self awaitExpectations];
  194. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  195. XCTAssertEqualObjects(snapshot.data,
  196. (@{@"a" : @"b", @"c" : @"d", @"nested" : @{@"a" : @"b", @"c" : @"d"}}));
  197. }
  198. - (void)testCannotUpdateNonExistentDocument {
  199. FIRFirestore *firestore = [self firestore];
  200. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  201. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  202. [firestore
  203. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  204. [transaction updateData:@{@"foo" : @"bar"} forDocument:doc];
  205. return nil;
  206. }
  207. completion:^(id _Nullable result, NSError *_Nullable error) {
  208. XCTAssertNotNil(error);
  209. // This is the error surfaced by the backend.
  210. XCTAssertEqual(error.code, FIRFirestoreErrorCodeNotFound);
  211. [expectation fulfill];
  212. }];
  213. [self awaitExpectations];
  214. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  215. XCTAssertFalse(result.exists);
  216. }
  217. - (void)testIncrementTransactionally {
  218. // A barrier to make sure every transaction reaches the same spot.
  219. dispatch_semaphore_t writeBarrier = dispatch_semaphore_create(0);
  220. __block volatile int32_t started = 0;
  221. FIRFirestore *firestore = [self firestore];
  222. FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  223. [self writeDocumentRef:doc data:@{@"count" : @(5.0)}];
  224. // Make 3 transactions that will all increment.
  225. int total = 3;
  226. for (int i = 0; i < total; i++) {
  227. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  228. [firestore
  229. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  230. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  231. XCTAssertNil(*error);
  232. int32_t nowStarted = OSAtomicIncrement32(&started);
  233. // Once all of the transactions have read, allow the first write.
  234. if (nowStarted == total) {
  235. dispatch_semaphore_signal(writeBarrier);
  236. }
  237. dispatch_semaphore_wait(writeBarrier, DISPATCH_TIME_FOREVER);
  238. // Refill the barrier so that the other transactions and retries succeed.
  239. dispatch_semaphore_signal(writeBarrier);
  240. double newCount = ((NSNumber *)snapshot[@"count"]).doubleValue + 1.0;
  241. [transaction setData:@{@"count" : @(newCount)} forDocument:doc];
  242. return @YES;
  243. }
  244. completion:^(id _Nullable result, NSError *_Nullable error) {
  245. [expectation fulfill];
  246. }];
  247. }
  248. [self awaitExpectations];
  249. // Now all transaction should be completed, so check the result.
  250. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  251. XCTAssertEqualObjects(@(5.0 + total), snapshot[@"count"]);
  252. }
  253. - (void)testUpdateTransactionally {
  254. // A barrier to make sure every transaction reaches the same spot.
  255. dispatch_semaphore_t writeBarrier = dispatch_semaphore_create(0);
  256. __block volatile int32_t started = 0;
  257. FIRFirestore *firestore = [self firestore];
  258. FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  259. [self writeDocumentRef:doc data:@{@"count" : @(5.0), @"other" : @"yes"}];
  260. // Make 3 transactions that will all increment.
  261. int total = 3;
  262. for (int i = 0; i < total; i++) {
  263. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  264. [firestore
  265. 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. - (void)testHandleReadingOneDocAndWritingAnother {
  291. FIRFirestore *firestore = [self firestore];
  292. FIRDocumentReference *doc1 = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  293. FIRDocumentReference *doc2 = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  294. [self writeDocumentRef:doc1 data:@{@"count" : @(15.0)}];
  295. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  296. [firestore
  297. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  298. // Get the first doc.
  299. [transaction getDocument:doc1 error:error];
  300. XCTAssertNil(*error);
  301. // Do a write outside of the transaction. The first time the
  302. // transaction is tried, this will bump the version, which
  303. // will cause the write to doc2 to fail. The second time, it
  304. // will be a no-op and not bump the version.
  305. dispatch_semaphore_t writeSemaphore = dispatch_semaphore_create(0);
  306. [doc1 setData:@{
  307. @"count" : @(1234)
  308. }
  309. completion:^(NSError *_Nullable error) {
  310. dispatch_semaphore_signal(writeSemaphore);
  311. }];
  312. // We can block on it, because transactions run on a background queue.
  313. dispatch_semaphore_wait(writeSemaphore, DISPATCH_TIME_FOREVER);
  314. // Now try to update the other doc from within the transaction.
  315. // This should fail once, because we read 15 earlier.
  316. [transaction setData:@{@"count" : @(16)} forDocument:doc2];
  317. return nil;
  318. }
  319. completion:^(id _Nullable result, NSError *_Nullable error) {
  320. // We currently require every document read to also be written.
  321. // TODO(b/34879758): Add this check back once we drop that.
  322. // NSError *error = nil;
  323. // FIRDocument *snapshot = [transaction getDocument:doc1 error:&error];
  324. // XCTAssertNil(error);
  325. // XCTAssertEquals(0, tries);
  326. // XCTAssertEqualObjects(@(1234), snapshot[@"count"]);
  327. // snapshot = [transaction getDocument:doc2 error:&error];
  328. // XCTAssertNil(error);
  329. // XCTAssertEqualObjects(@(16), snapshot[@"count"]);
  330. XCTAssertNotNil(error);
  331. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  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
  342. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  343. // Get the doc once.
  344. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  345. XCTAssertNil(*error);
  346. XCTAssertEqualObjects(@(15), snapshot[@"count"]);
  347. // Do a write outside of the transaction.
  348. dispatch_semaphore_t writeSemaphore = dispatch_semaphore_create(0);
  349. [doc setData:@{
  350. @"count" : @(1234)
  351. }
  352. completion:^(NSError *_Nullable error) {
  353. dispatch_semaphore_signal(writeSemaphore);
  354. }];
  355. // We can block on it, because transactions run on a background queue.
  356. dispatch_semaphore_wait(writeSemaphore, DISPATCH_TIME_FOREVER);
  357. // Get the doc again in the transaction with the new version.
  358. snapshot = [transaction getDocument:doc error:error];
  359. // The get itself will fail, because we already read an earlier version of this document.
  360. // TODO(klimt): Perhaps we shouldn't fail reads for this, but should wait and fail the
  361. // whole transaction? It's an edge-case anyway, as developers shouldn't be reading the same
  362. // doc multiple times. But they need to handle read errors anyway.
  363. XCTAssertNotNil(*error);
  364. return nil;
  365. }
  366. completion:^(id _Nullable result, NSError *_Nullable error) {
  367. [expectation fulfill];
  368. XCTAssertNotNil(error);
  369. XCTAssertEqual(error.code, FIRFirestoreErrorCodeAborted);
  370. }];
  371. [self awaitExpectations];
  372. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  373. XCTAssertEqualObjects(@(1234.0), snapshot[@"count"]);
  374. }
  375. - (void)testReadAndUpdateNonExistentDocumentWithExternalWrite {
  376. FIRFirestore *firestore = [self firestore];
  377. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  378. [firestore
  379. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  380. // Get and update a document that doesn't exist so that the transaction fails.
  381. FIRDocumentReference *doc =
  382. [[firestore collectionWithPath:@"nonexistent"] documentWithAutoID];
  383. [transaction getDocument:doc error:error];
  384. XCTAssertNil(*error);
  385. // Do a write outside of the transaction.
  386. dispatch_semaphore_t writeSemaphore = dispatch_semaphore_create(0);
  387. [doc setData:@{
  388. @"count" : @(1234)
  389. }
  390. completion:^(NSError *_Nullable error) {
  391. dispatch_semaphore_signal(writeSemaphore);
  392. }];
  393. // We can block on it, because transactions run on a background queue.
  394. dispatch_semaphore_wait(writeSemaphore, DISPATCH_TIME_FOREVER);
  395. // Now try to update the other doc from within the transaction.
  396. // This should fail, because the document didn't exist at the
  397. // start of the transaction.
  398. [transaction updateData:@{@"count" : @(16)} forDocument:doc];
  399. return nil;
  400. }
  401. completion:^(id _Nullable result, NSError *_Nullable error) {
  402. [expectation fulfill];
  403. XCTAssertNotNil(error);
  404. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  405. }];
  406. [self awaitExpectations];
  407. }
  408. - (void)testCannotHaveAGetWithoutMutations {
  409. FIRFirestore *firestore = [self firestore];
  410. FIRDocumentReference *doc = [[firestore collectionWithPath:@"foo"] documentWithAutoID];
  411. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  412. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  413. [firestore
  414. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  415. FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
  416. XCTAssertTrue(snapshot.exists);
  417. XCTAssertNil(*error);
  418. return nil;
  419. }
  420. completion:^(id _Nullable result, NSError *_Nullable error) {
  421. // We currently require every document read to also be written.
  422. // TODO(b/34879758): Fix this check once we drop that requirement.
  423. XCTAssertNotNil(error);
  424. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  425. [expectation fulfill];
  426. }];
  427. [self awaitExpectations];
  428. }
  429. - (void)testSuccessWithNoTransactionOperations {
  430. FIRFirestore *firestore = [self firestore];
  431. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  432. [firestore
  433. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  434. return @"yes";
  435. }
  436. completion:^(id _Nullable result, NSError *_Nullable error) {
  437. XCTAssertEqualObjects(@"yes", result);
  438. XCTAssertNil(error);
  439. [expectation fulfill];
  440. }];
  441. [self awaitExpectations];
  442. }
  443. - (void)testCancellationOnError {
  444. FIRFirestore *firestore = [self firestore];
  445. FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  446. __block volatile int32_t count = 0;
  447. XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  448. [firestore
  449. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  450. OSAtomicIncrement32(&count);
  451. [transaction setData:@{@"foo" : @"bar"} forDocument:doc];
  452. if (error) {
  453. *error = [NSError errorWithDomain:NSCocoaErrorDomain code:35 userInfo:@{}];
  454. }
  455. return nil;
  456. }
  457. completion:^(id _Nullable result, NSError *_Nullable error) {
  458. XCTAssertNil(result);
  459. XCTAssertNotNil(error);
  460. XCTAssertEqual(35, error.code);
  461. [expectation fulfill];
  462. }];
  463. [self awaitExpectations];
  464. XCTAssertEqual(1, (int)count);
  465. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  466. XCTAssertFalse(snapshot.exists);
  467. }
  468. - (void)testUpdateFieldsWithDotsTransactionally {
  469. FIRDocumentReference *doc = [self documentRef];
  470. XCTestExpectation *expectation =
  471. [self expectationWithDescription:@"testUpdateFieldsWithDotsTransactionally"];
  472. [doc.firestore
  473. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  474. XCTAssertNil(*error);
  475. [transaction setData:@{@"a.b" : @"old", @"c.d" : @"old"} forDocument:doc];
  476. [transaction updateData:@{
  477. [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"
  478. }
  479. forDocument:doc];
  480. return nil;
  481. }
  482. completion:^(id result, NSError *error) {
  483. XCTAssertNil(error);
  484. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  485. XCTAssertNil(error);
  486. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  487. }];
  488. [expectation fulfill];
  489. }];
  490. [self awaitExpectations];
  491. }
  492. - (void)testUpdateNestedFieldsTransactionally {
  493. FIRDocumentReference *doc = [self documentRef];
  494. XCTestExpectation *expectation =
  495. [self expectationWithDescription:@"testUpdateNestedFieldsTransactionally"];
  496. [doc.firestore
  497. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  498. XCTAssertNil(*error);
  499. [transaction setData:@{
  500. @"a" : @{@"b" : @"old"},
  501. @"c" : @{@"d" : @"old"},
  502. @"e" : @{@"f" : @"old"}
  503. }
  504. forDocument:doc];
  505. [transaction updateData:@{
  506. @"a.b" : @"new",
  507. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  508. }
  509. forDocument:doc];
  510. return nil;
  511. }
  512. completion:^(id result, NSError *error) {
  513. XCTAssertNil(error);
  514. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  515. XCTAssertNil(error);
  516. XCTAssertEqualObjects(snapshot.data, (@{
  517. @"a" : @{@"b" : @"new"},
  518. @"c" : @{@"d" : @"new"},
  519. @"e" : @{@"f" : @"old"}
  520. }));
  521. }];
  522. [expectation fulfill];
  523. }];
  524. [self awaitExpectations];
  525. }
  526. @end