Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for throwing exceptions on get(), update() and delete() #275

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/src/mock_document_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class MockDocumentReference<T extends Object?> implements DocumentReference<T> {
@override
Future<void> update(Map<Object, Object?> data) async {
await _firestore.maybeThrowSecurityException(path, Method.update);
maybeThrowException(this, Invocation.method(#update, [data]));
if (!_exists()) {
return Future.error(FirebaseException(
plugin: 'FakeFirestore',
Expand Down Expand Up @@ -215,6 +216,7 @@ class MockDocumentReference<T extends Object?> implements DocumentReference<T> {
@override
Future<DocumentSnapshot<T>> get([GetOptions? options]) async {
await _firestore.maybeThrowSecurityException(path, Method.read);
maybeThrowException(this, Invocation.method(#get, [options]));
return _getSync(options);
}

Expand Down Expand Up @@ -268,6 +270,7 @@ class MockDocumentReference<T extends Object?> implements DocumentReference<T> {
@override
Future<void> delete() async {
await _firestore.maybeThrowSecurityException(path, Method.delete);
maybeThrowException(this, Invocation.method(#delete, null));
rootParent.remove(id);
_firestore.removeSavedDocument(path);
docsData.remove(path);
Expand Down
2 changes: 2 additions & 0 deletions lib/src/mock_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:math';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fake_cloud_firestore/src/util.dart';
import 'package:flutter/services.dart';
import 'package:mock_exceptions/mock_exceptions.dart';
import 'package:quiver/core.dart';

import 'converter.dart';
Expand Down Expand Up @@ -39,6 +40,7 @@ class MockQuery<T extends Object?> extends FakeQueryWithParent<T> {
// Collection references: parent query is null.
// Regular queries: _parentQuery and _operation are not null.
assert(_parentQuery != null && _operation != null);
maybeThrowException(this, Invocation.method(#get, [options]));
final parentQueryResult = await _parentQuery!.get(options);
final docs = _operation!(parentQueryResult.docs);
return MockQuerySnapshot<T>(docs, options?.source == Source.cache);
Expand Down
28 changes: 28 additions & 0 deletions test/fake_cloud_firestore_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ void main() {
.thenThrow(FirebaseException(plugin: 'firestore'));
expect(() => doc.set({'name': 'Bob'}), throwsA(isA<FirebaseException>()));
});

test('DocumentReference.get throws exceptions', () async {
final instance = FakeFirebaseFirestore();
final doc = instance.collection('users').doc(uid);
whenCalling(Invocation.method(#get, null))
.on(doc)
.thenThrow(FirebaseException(plugin: 'firestore'));
expect(() => doc.get(), throwsA(isA<FirebaseException>()));
});

test('DocumentReference.delete throws exceptions', () async {
final instance = FakeFirebaseFirestore();
final doc = instance.collection('users').doc(uid);
whenCalling(Invocation.method(#delete, null))
.on(doc)
.thenThrow(FirebaseException(plugin: 'firestore'));
expect(() => doc.delete(), throwsA(isA<FirebaseException>()));
});

test('DocumentReference.update throws exceptions', () async {
final instance = FakeFirebaseFirestore();
final doc = instance.collection('users').doc(uid);
whenCalling(Invocation.method(#update, null))
.on(doc)
.thenThrow(FirebaseException(plugin: 'firestore'));
expect(() => doc.update({'data' : 'new'}), throwsA(isA<FirebaseException>()));
});

test('DocumentReference.set throws exceptions on certain conditions',
() async {
final instance = FakeFirebaseFirestore();
Expand Down
24 changes: 24 additions & 0 deletions test/mock_query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fake_cloud_firestore/fake_cloud_firestore.dart';
import 'package:fake_cloud_firestore/src/util.dart';
import 'package:flutter/services.dart';
import 'package:mock_exceptions/mock_exceptions.dart';
import 'package:test/test.dart';

import 'document_snapshot_matcher.dart';
Expand Down Expand Up @@ -1654,4 +1655,27 @@ void main() {
expect(query.metadata.isFromCache, false);
});
});

group('exceptions', () {
test('get', () async {
final firestore = FakeFirebaseFirestore();
final movies = firestore.collection('movies');
final query = firestore
.collection('movies')
.where('title', isEqualTo: 'Test Movie');

await movies.add({
'title': 'Test Movie',
});

whenCalling(Invocation.method(#get, null))
.on(query)
.thenThrow(FirebaseException(plugin: 'firestore'));

expect(
() async => await query.get(GetOptions(source: Source.server)),
throwsA(isA<FirebaseException>()),
);
});
});
}