Skip to content

Commit

Permalink
Merge pull request #37 from suztomo/i30
Browse files Browse the repository at this point in the history
Assert the number of segment in MockFirestoreInstance's document() and collection()
  • Loading branch information
atn832 committed Mar 13, 2020
2 parents 08068f7 + a6ea6ee commit 7d6362d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
18 changes: 16 additions & 2 deletions lib/src/cloud_firestore_mocks_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@ class MockFirestoreInstance extends Mock implements Firestore {

@override
CollectionReference collection(String path) {
final segments = path.split('/');
assert(segments.length % 2 == 1,
'Invalid document reference. Collection references must have an odd number of segments');
return MockCollectionReference(this, path, getSubpath(_root, path),
getSubpath(_snapshotStreamControllerRoot, path));
}

@override
DocumentReference document(String path) {
final documentId = path.split('/').last;
return MockDocumentReference(this, path, documentId, getSubpath(_root, path), _root,
final segments = path.split('/');
// The actual behavior of Firestore for an invalid number of segments
// differs by platforms. This library imitates it with assert.
// https://github.com/atn832/cloud_firestore_mocks/issues/30
assert(segments.length % 2 == 0,
'Invalid document reference. Document references must have an even number of segments');
final documentId = segments.last;
return MockDocumentReference(
this,
path,
documentId,
getSubpath(_root, path),
_root,
getSubpath(_snapshotStreamControllerRoot, path));
}

Expand Down
43 changes: 40 additions & 3 deletions test/cloud_firestore_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,11 @@ void main() {
group('FieldValue', () {
test('FieldValue.delete() deletes key values', () async {
final firestore = MockFirestoreInstance();
await firestore.document('root').setData({'flower': 'rose'});
await firestore.document('root').setData({'flower': FieldValue.delete()});
final document = await firestore.document('root').get();
await firestore.document('root/foo').setData({'flower': 'rose'});
await firestore
.document('root/foo')
.setData({'flower': FieldValue.delete()});
final document = await firestore.document('root/foo').get();
expect(document.data.isEmpty, equals(true));
});

Expand Down Expand Up @@ -490,4 +492,39 @@ void main() {
final nameMap = savedFoo['name'] as Map<String, dynamic>;
expect(nameMap['firstName'], 'Survivor');
});

test('MockFirestoreInstance.document with a valid path', () async {
final firestore = MockFirestoreInstance();
final documentReference = firestore.document('users/1234');
expect(documentReference, isNotNull);
});

test('MockFirestoreInstance.document with a invalid path', () async {
final firestore = MockFirestoreInstance();

// This should fail because users (1 segments) and users/1234/friends (3 segments)
// are a reference to a subcollection, not a document.
// In real Firestore, the behavior of this error depends on the platforms;
// in iOS, it's NSInternalInconsistencyException that would terminate
// the app. This library imitates it with assert.
// https://github.com/atn832/cloud_firestore_mocks/issues/30
expect(() => firestore.document('users'),
throwsA(isA<AssertionError>()));

// subcollection
expect(() => firestore.document('users/1234/friends'),
throwsA(isA<AssertionError>()));
});

test('MockFirestoreInstance.collection with a invalid path', () async {
final firestore = MockFirestoreInstance();

// This should fail because users/1234 (2 segments) is a reference to a
// document, not a collection.
expect(() => firestore.collection('users/1234'),
throwsA(isA<AssertionError>()));

expect(() => firestore.collection('users/1234/friends/567'),
throwsA(isA<AssertionError>()));
});
}

0 comments on commit 7d6362d

Please sign in to comment.