aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Example/Tests/Integration/FSTTransactionTests.m
blob: a0b5bbe3252f5491bad65d5cbd81d197fa926d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

@import Firestore;

#import <XCTest/XCTest.h>
#include <libkern/OSAtomic.h>

#import "FSTIntegrationTestCase.h"

@interface FSTTransactionTests : FSTIntegrationTestCase
@end

@implementation FSTTransactionTests

// We currently require every document read to also be written.
// TODO(b/34879758): Re-enable this test once we fix it.
- (void)xtestGetDocuments {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"spaces"] documentWithAutoID];
  [self writeDocumentRef:doc data:@{ @"foo" : @1, @"desc" : @"Stuff", @"owner" : @"Jonny" }];

  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    [transaction getDocument:doc error:error];
    XCTAssertNil(*error);
    return @YES;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertNil(result);
        // We currently require every document read to also be written.
        // TODO(b/34879758): Fix this check once we drop that requirement.
        XCTAssertNotNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];
}

- (void)testDeleteDocument {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  XCTAssertEqualObjects(@"bar", snapshot[@"foo"]);

  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    [transaction deleteDocument:doc];
    return @YES;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertEqualObjects(@YES, result);
        XCTAssertNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];

  snapshot = [self readDocumentForRef:doc];
  XCTAssertFalse(snapshot.exists);
}

- (void)testGetNonexistentDocumentThenCreate {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];

  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
    XCTAssertNil(*error);
    XCTAssertFalse(snapshot.exists);
    [transaction setData:@{@"foo" : @"bar"} forDocument:doc];
    return @YES;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertEqualObjects(@YES, result);
        XCTAssertNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];

  FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  XCTAssertTrue(snapshot.exists);
  XCTAssertEqualObjects(@"bar", snapshot[@"foo"]);
}

- (void)testGetNonexistentDocumentThenFailPatch {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];

  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
    XCTAssertNil(*error);
    XCTAssertFalse(snapshot.exists);
    [transaction updateData:@{@"foo" : @"bar"} forDocument:doc];
    return @YES;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertNil(result);
        XCTAssertNotNil(error);
        XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
        // TODO(dimond): This is probably the wrong error code, but it's what we use today. We
        // should update the code once the underlying error was fixed.
        XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
        [expectation fulfill];
      }];
  [self awaitExpectations];
}

- (void)testDeleteDocumentAndPatch {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];

  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
    FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
    XCTAssertNil(*error);
    XCTAssertTrue(snapshot.exists);
    [transaction deleteDocument:doc];
    // Since we deleted the doc, the update will fail
    [transaction updateData:@{@"foo" : @"bar"} forDocument:doc];
    return @YES;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertNil(result);
        XCTAssertNotNil(error);
        XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
        // TODO(dimond): This is probably the wrong error code, but it's what we use today. We
        // should update the code once the underlying error was fixed.
        XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
        [expectation fulfill];
      }];
  [self awaitExpectations];
}

- (void)testDeleteDocumentAndSet {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];

  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
    FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
    XCTAssertNil(*error);
    XCTAssertTrue(snapshot.exists);
    [transaction deleteDocument:doc];
    // TODO(dimond): In theory this should work, but it's complex to make it work, so instead we
    // just let the transaction fail and verify it's unsupported for now
    [transaction setData:@{@"foo" : @"new-bar"} forDocument:doc];
    return @YES;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertNil(result);
        XCTAssertNotNil(error);
        XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
        // TODO(dimond): This is probably the wrong error code, but it's what we use today. We
        // should update the code once the underlying error was fixed.
        XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
        [expectation fulfill];
      }];
  [self awaitExpectations];
}

- (void)testWriteDocumentTwice {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];

  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **error) {
    [transaction setData:@{@"a" : @"b"} forDocument:doc];
    [transaction setData:@{@"c" : @"d"} forDocument:doc];
    return @YES;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertEqualObjects(@YES, result);
        XCTAssertNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];

  FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  XCTAssertEqualObjects(snapshot.data, @{@"c" : @"d"});
}

- (void)testSetDocumentWithMerge {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];

  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    [transaction setData:@{ @"a" : @"b", @"nested" : @{@"a" : @"b"} } forDocument:doc];
    [transaction setData:@{
      @"c" : @"d",
      @"nested" : @{@"c" : @"d"}
    }
             forDocument:doc
                 options:[FIRSetOptions merge]];
    return @YES;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertEqualObjects(@YES, result);
        XCTAssertNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];

  FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  XCTAssertEqualObjects(
      snapshot.data, (
                         @{ @"a" : @"b",
                            @"c" : @"d",
                            @"nested" : @{@"a" : @"b", @"c" : @"d"} }));
}

- (void)testCannotUpdateNonExistentDocument {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];

  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    [transaction updateData:@{@"foo" : @"bar"} forDocument:doc];
    return nil;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertNotNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];

  FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  XCTAssertFalse(result.exists);
}

- (void)testIncrementTransactionally {
  // A barrier to make sure every transaction reaches the same spot.
  dispatch_semaphore_t writeBarrier = dispatch_semaphore_create(0);
  __block volatile int32_t started = 0;

  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  [self writeDocumentRef:doc data:@{ @"count" : @(5.0) }];

  // Make 3 transactions that will all increment.
  int total = 3;
  for (int i = 0; i < total; i++) {
    XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
    [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
      FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
      XCTAssertNil(*error);
      int32_t nowStarted = OSAtomicIncrement32(&started);
      // Once all of the transactions have read, allow the first write.
      if (nowStarted == total) {
        dispatch_semaphore_signal(writeBarrier);
      }

      dispatch_semaphore_wait(writeBarrier, DISPATCH_TIME_FOREVER);
      // Refill the barrier so that the other transactions and retries succeed.
      dispatch_semaphore_signal(writeBarrier);

      double newCount = ((NSNumber *)snapshot[@"count"]).doubleValue + 1.0;
      [transaction setData:@{ @"count" : @(newCount) } forDocument:doc];
      return @YES;

    }
        completion:^(id _Nullable result, NSError *_Nullable error) {
          [expectation fulfill];
        }];
  }

  [self awaitExpectations];
  // Now all transaction should be completed, so check the result.
  FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  XCTAssertEqualObjects(@(5.0 + total), snapshot[@"count"]);
}

- (void)testUpdateTransactionally {
  // A barrier to make sure every transaction reaches the same spot.
  dispatch_semaphore_t writeBarrier = dispatch_semaphore_create(0);
  __block volatile int32_t started = 0;

  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  [self writeDocumentRef:doc data:@{ @"count" : @(5.0), @"other" : @"yes" }];

  // Make 3 transactions that will all increment.
  int total = 3;
  for (int i = 0; i < total; i++) {
    XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
    [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
      FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
      XCTAssertNil(*error);
      int32_t nowStarted = OSAtomicIncrement32(&started);
      // Once all of the transactions have read, allow the first write.
      if (nowStarted == total) {
        dispatch_semaphore_signal(writeBarrier);
      }

      dispatch_semaphore_wait(writeBarrier, DISPATCH_TIME_FOREVER);
      // Refill the barrier so that the other transactions and retries succeed.
      dispatch_semaphore_signal(writeBarrier);

      double newCount = ((NSNumber *)snapshot[@"count"]).doubleValue + 1.0;
      [transaction updateData:@{ @"count" : @(newCount) } forDocument:doc];
      return @YES;

    }
        completion:^(id _Nullable result, NSError *_Nullable error) {
          [expectation fulfill];
        }];
  }

  [self awaitExpectations];
  // Now all transaction should be completed, so check the result.
  FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  XCTAssertEqualObjects(@(5.0 + total), snapshot[@"count"]);
  XCTAssertEqualObjects(@"yes", snapshot[@"other"]);
}

// We currently require every document read to also be written.
// TODO(b/34879758): Re-enable this test once we fix it.
- (void)xtestHandleReadingOneDocAndWritingAnother {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc1 = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  FIRDocumentReference *doc2 = [[firestore collectionWithPath:@"counters"] documentWithAutoID];

  [self writeDocumentRef:doc1 data:@{ @"count" : @(15.0) }];

  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    // Get the first doc.
    [transaction getDocument:doc1 error:error];
    XCTAssertNil(*error);
    // Do a write outside of the transaction. The first time the
    // transaction is tried, this will bump the version, which
    // will cause the write to doc2 to fail. The second time, it
    // will be a no-op and not bump the version.
    dispatch_semaphore_t writeSemaphore = dispatch_semaphore_create(0);
    [doc1 setData:@{
      @"count" : @(1234)
    }
        completion:^(NSError *_Nullable error) {
          dispatch_semaphore_signal(writeSemaphore);
        }];
    // We can block on it, because transactions run on a background queue.
    dispatch_semaphore_wait(writeSemaphore, DISPATCH_TIME_FOREVER);
    // Now try to update the other doc from within the transaction.
    // This should fail once, because we read 15 earlier.
    [transaction setData:@{ @"count" : @(16) } forDocument:doc2];
    return nil;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        // We currently require every document read to also be written.
        // TODO(b/34879758): Add this check back once we drop that.
        // NSError *error = nil;
        // FIRDocument *snapshot = [transaction getDocument:doc1 error:&error];
        // XCTAssertNil(error);
        // XCTAssertEquals(0, tries);
        // XCTAssertEqualObjects(@(1234), snapshot[@"count"]);
        // snapshot = [transaction getDocument:doc2 error:&error];
        // XCTAssertNil(error);
        // XCTAssertEqualObjects(@(16), snapshot[@"count"]);
        XCTAssertNotNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];
}

- (void)testReadingADocTwiceWithDifferentVersions {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"counters"] documentWithAutoID];
  [self writeDocumentRef:doc data:@{ @"count" : @(15.0) }];
  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    // Get the doc once.
    FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
    XCTAssertNil(*error);
    XCTAssertEqualObjects(@(15), snapshot[@"count"]);
    // Do a write outside of the transaction.
    dispatch_semaphore_t writeSemaphore = dispatch_semaphore_create(0);
    [doc setData:@{
      @"count" : @(1234)
    }
        completion:^(NSError *_Nullable error) {
          dispatch_semaphore_signal(writeSemaphore);
        }];
    // We can block on it, because transactions run on a background queue.
    dispatch_semaphore_wait(writeSemaphore, DISPATCH_TIME_FOREVER);
    // Get the doc again in the transaction with the new version.
    snapshot = [transaction getDocument:doc error:error];
    // The get itself will fail, because we already read an earlier version of this document.
    // TODO(klimt): Perhaps we shouldn't fail reads for this, but should wait and fail the
    // whole transaction? It's an edge-case anyway, as developers shouldn't be reading the same
    // do multiple times. But they need to handle read errors anyway.
    XCTAssertNotNil(*error);
    return nil;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        [expectation fulfill];
      }];
  [self awaitExpectations];

  FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  XCTAssertEqualObjects(@(1234.0), snapshot[@"count"]);
}

// We currently require every document read to also be written.
// TODO(b/34879758): Add this test back once we fix that.
- (void)xtestCannotHaveAGetWithoutMutations {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"foo"] documentWithAutoID];
  [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    FIRDocumentSnapshot *snapshot = [transaction getDocument:doc error:error];
    XCTAssertTrue(snapshot.exists);
    XCTAssertNil(*error);
    return nil;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertNotNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];
}

- (void)testSuccessWithNoTransactionOperations {
  FIRFirestore *firestore = [self firestore];
  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    return @"yes";
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertEqualObjects(@"yes", result);
        XCTAssertNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];
}

- (void)testCancellationOnError {
  FIRFirestore *firestore = [self firestore];
  FIRDocumentReference *doc = [[firestore collectionWithPath:@"towns"] documentWithAutoID];
  __block volatile int32_t count = 0;
  XCTestExpectation *expectation = [self expectationWithDescription:@"transaction"];
  [firestore runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
    OSAtomicIncrement32(&count);
    [transaction setData:@{@"foo" : @"bar"} forDocument:doc];
    *error = [NSError errorWithDomain:NSCocoaErrorDomain code:35 userInfo:@{}];
    return nil;
  }
      completion:^(id _Nullable result, NSError *_Nullable error) {
        XCTAssertNil(result);
        XCTAssertNotNil(error);
        XCTAssertEqual(35, error.code);
        [expectation fulfill];
      }];
  [self awaitExpectations];
  XCTAssertEqual(1, (int)count);
  FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc];
  XCTAssertFalse(snapshot.exists);
}

- (void)testUpdateFieldsWithDotsTransactionally {
  FIRDocumentReference *doc = [self documentRef];

  XCTestExpectation *expectation =
      [self expectationWithDescription:@"testUpdateFieldsWithDotsTransactionally"];

  [doc.firestore
      runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
        XCTAssertNil(*error);
        [transaction setData:@{@"a.b" : @"old", @"c.d" : @"old"} forDocument:doc];
        [transaction updateData:@{
          [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"
        }
                    forDocument:doc];
        return nil;
      }
      completion:^(id result, NSError *error) {
        XCTAssertNil(error);
        [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
          XCTAssertNil(error);
          XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
        }];
        [expectation fulfill];
      }];
  [self awaitExpectations];
}

- (void)testUpdateNestedFieldsTransactionally {
  FIRDocumentReference *doc = [self documentRef];

  XCTestExpectation *expectation =
      [self expectationWithDescription:@"testUpdateNestedFieldsTransactionally"];

  [doc.firestore
      runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
        XCTAssertNil(*error);
        [transaction setData:@{
          @"a" : @{@"b" : @"old"},
          @"c" : @{@"d" : @"old"},
          @"e" : @{@"f" : @"old"}
        }
                 forDocument:doc];
        [transaction updateData:@{
          @"a.b" : @"new",
          [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
        }
                    forDocument:doc];
        return nil;
      }
      completion:^(id result, NSError *error) {
        XCTAssertNil(error);
        [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
          XCTAssertNil(error);
          XCTAssertEqualObjects(snapshot.data, (@{
                                  @"a" : @{@"b" : @"new"},
                                  @"c" : @{@"d" : @"new"},
                                  @"e" : @{@"f" : @"old"}
                                }));
        }];
        [expectation fulfill];
      }];
  [self awaitExpectations];
}

@end