@@ -50,8 +50,9 @@ apiDescribe('Database transactions', (persistence: boolean) => {
50
50
// expect(snapshot.data()['owner']).to.equal('Jonny');
51
51
// });
52
52
. then ( ( ) => expect . fail ( 'transaction should fail' ) )
53
- . catch ( err => {
53
+ . catch ( ( err : firestore . FirestoreError ) => {
54
54
expect ( err ) . to . exist ;
55
+ expect ( err . code ) . to . equal ( 'invalid-argument' ) ;
55
56
expect ( err . message ) . to . contain (
56
57
'Every document read in a transaction must also be' +
57
58
' written'
@@ -117,11 +118,9 @@ apiDescribe('Database transactions', (persistence: boolean) => {
117
118
} ) ;
118
119
} )
119
120
. then ( ( ) => expect . fail ( 'transaction should fail' ) )
120
- . catch ( err => {
121
+ . catch ( ( err : firestore . FirestoreError ) => {
121
122
expect ( err ) . to . exist ;
122
- expect ( err . message ) . to . equal (
123
- "Can't update a document that doesn't exist."
124
- ) ;
123
+ expect ( err . code ) . to . equal ( 'invalid-argument' ) ;
125
124
} ) ;
126
125
} ) ;
127
126
} ) ;
@@ -139,16 +138,10 @@ apiDescribe('Database transactions', (persistence: boolean) => {
139
138
transaction . update ( docRef , { foo : 'new-bar' } ) ;
140
139
} ) ;
141
140
} )
142
- . then ( ( ) => expect . fail ( 'transaction should fail' ) )
143
- . catch ( err => {
141
+ . catch ( ( err : firestore . FirestoreError ) => {
144
142
expect ( err ) . to . exist ;
145
- // TODO(b/65409268): This is likely the wrong error code
146
- expect ( ( err as firestore . FirestoreError ) . code ) . to . equal (
147
- 'failed-precondition'
148
- ) ;
149
- expect ( err . message ) . to . equal (
150
- "Can't update a document that doesn't exist."
151
- ) ;
143
+ // This is the error surfaced by the backend.
144
+ expect ( err . code ) . to . equal ( 'invalid-argument' ) ;
152
145
} ) ;
153
146
} ) ;
154
147
} ) ;
@@ -172,7 +165,7 @@ apiDescribe('Database transactions', (persistence: boolean) => {
172
165
. then ( ( ) => expect . fail ( 'transaction should fail' ) )
173
166
. catch ( err => {
174
167
expect ( err ) . to . exist ;
175
- // TODO(b/65409268): This is likely the wrong error code
168
+ // This is the error surfaced by the backend.
176
169
expect ( ( err as firestore . FirestoreError ) . code ) . to . equal (
177
170
'invalid-argument'
178
171
) ;
@@ -412,8 +405,9 @@ apiDescribe('Database transactions', (persistence: boolean) => {
412
405
// })
413
406
// .then(snapshot => expect(snapshot.data()['count']).to.equal(16));
414
407
. then ( ( ) => expect . fail ( 'transaction should fail' ) )
415
- . catch ( err => {
408
+ . catch ( ( err : firestore . FirestoreError ) => {
416
409
expect ( err ) . to . exist ;
410
+ expect ( err . code ) . to . equal ( 'invalid-argument' ) ;
417
411
expect ( err . message ) . to . contain (
418
412
'Every document read in a transaction must also be ' + 'written'
419
413
) ;
@@ -447,7 +441,10 @@ apiDescribe('Database transactions', (persistence: boolean) => {
447
441
} ) ;
448
442
} )
449
443
. then ( ( ) => expect . fail ( 'transaction should fail' ) )
450
- . catch ( err => expect ( err ) . to . exist )
444
+ . catch ( err => {
445
+ expect ( err ) . to . exist ;
446
+ expect ( ( err as firestore . FirestoreError ) . code ) . to . equal ( 'aborted' ) ;
447
+ } )
451
448
. then ( ( ) => doc . get ( ) )
452
449
. then ( snapshot => {
453
450
expect ( snapshot . data ( ) ! [ 'count' ] ) . to . equal ( 1234 ) ;
@@ -466,12 +463,48 @@ apiDescribe('Database transactions', (persistence: boolean) => {
466
463
. then ( ( ) => {
467
464
expect . fail ( 'transaction should fail' ) ;
468
465
} )
469
- . catch ( err => {
466
+ . catch ( ( err : firestore . FirestoreError ) => {
470
467
expect ( err ) . to . exist ;
468
+ expect ( err . code ) . to . equal ( 'invalid-argument' ) ;
469
+ expect ( err . message ) . to . contain (
470
+ 'Firestore transactions require all reads to be executed'
471
+ ) ;
471
472
} ) ;
472
473
} ) ;
473
474
} ) ;
474
475
476
+ it (
477
+ 'cannot read non-existent document then update, even if ' +
478
+ 'document is written after the read' ,
479
+ ( ) => {
480
+ return integrationHelpers . withTestDb ( persistence , db => {
481
+ return db
482
+ . runTransaction ( transaction => {
483
+ const doc = db . collection ( 'nonexistent' ) . doc ( ) ;
484
+ return (
485
+ transaction
486
+ // Get and update a document that doesn't exist so that the transaction fails.
487
+ . get ( doc )
488
+ // Do a write outside of the transaction.
489
+ . then ( ( ) => doc . set ( { count : 1234 } ) )
490
+ // Now try to update the doc from within the transaction. This
491
+ // should fail, because the document didn't exist at the start
492
+ // of the transaction.
493
+ . then ( ( ) => transaction . update ( doc , { count : 16 } ) )
494
+ ) ;
495
+ } )
496
+ . then ( ( ) => expect . fail ( 'transaction should fail' ) )
497
+ . catch ( ( err : firestore . FirestoreError ) => {
498
+ expect ( err ) . to . exist ;
499
+ expect ( err . code ) . to . equal ( 'invalid-argument' ) ;
500
+ expect ( err . message ) . to . contain (
501
+ "Can't update a document that doesn't exist."
502
+ ) ;
503
+ } ) ;
504
+ } ) ;
505
+ }
506
+ ) ;
507
+
475
508
describe ( 'must return a promise:' , ( ) => {
476
509
const noop = ( ) : void => {
477
510
/* -_- */
@@ -522,8 +555,9 @@ apiDescribe('Database transactions', (persistence: boolean) => {
522
555
// expect(snapshot.data()['foo']).to.equal('bar');
523
556
// });
524
557
. then ( ( ) => expect . fail ( 'transaction should fail' ) )
525
- . catch ( err => {
558
+ . catch ( ( err : firestore . FirestoreError ) => {
526
559
expect ( err ) . to . exist ;
560
+ expect ( err . code ) . to . equal ( 'invalid-argument' ) ;
527
561
expect ( err . message ) . to . contain (
528
562
'Every document read in a transaction must also be ' + 'written'
529
563
) ;
0 commit comments