Skip to content

Commit 76abf63

Browse files
committed
test: add tests for empty batch commits, batch resets, and field path validation
1 parent ffb0b17 commit 76abf63

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

packages/google_cloud_firestore/test/write_batch_test.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,68 @@ void main() {
263263
throwsA(isA<StateError>()),
264264
);
265265
});
266+
267+
test(
268+
'throws ArgumentError when a field and its ancestor are both set',
269+
() {
270+
// e.g. setting 'a' and 'a.b' at the same time is ambiguous
271+
final batch = firestore.batch();
272+
final docRef = testCollection.doc();
273+
274+
expect(
275+
() => batch.update(docRef, {
276+
FieldPath(const ['a']): 1,
277+
FieldPath(const ['a', 'b']): 2,
278+
}),
279+
throwsA(
280+
isA<ArgumentError>().having(
281+
(e) => e.message,
282+
'message',
283+
contains('was specified multiple times'),
284+
),
285+
),
286+
);
287+
},
288+
);
289+
});
290+
291+
group('reset()', () {
292+
test('allows adding operations after a committed batch', () async {
293+
final docRef1 = testCollection.doc('reset-doc-1');
294+
final docRef2 = testCollection.doc('reset-doc-2');
295+
296+
final batch = firestore.batch();
297+
batch.create(docRef1, {'value': 1});
298+
await batch.commit();
299+
300+
// After reset, the batch should accept new operations
301+
batch.reset();
302+
batch.create(docRef2, {'value': 2});
303+
await batch.commit();
304+
305+
expect((await docRef2.get()).data(), {'value': 2});
306+
});
307+
308+
test('clears pending operations', () async {
309+
final docRef = testCollection.doc();
310+
311+
final batch = firestore.batch();
312+
batch.create(docRef, {'value': 1});
313+
314+
batch.reset(); // Clears the pending create
315+
316+
// Committing an empty batch after reset should succeed with no writes
317+
final results = await batch.commit();
318+
expect(results, isEmpty);
319+
});
320+
});
321+
322+
group('commit()', () {
323+
test('committing an empty batch returns empty results', () async {
324+
final batch = firestore.batch();
325+
final results = await batch.commit();
326+
expect(results, isEmpty);
327+
});
266328
});
267329
});
268330
}

0 commit comments

Comments
 (0)