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

Auto ID for document() #22

Merged
merged 3 commits into from
Mar 1, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/src/mock_collection_reference.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:math';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';
Expand Down Expand Up @@ -31,8 +32,21 @@ class MockCollectionReference extends MockQuery implements CollectionReference {
.map((entry) => MockDocumentSnapshot(entry.key, entry.value))
.toList());

static final Random _random = Random();
static final String _autoIdCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
static String _generateAutoId() {
final maxIndex = _autoIdCharacters.length - 1;
final autoId = List<int>.generate(20, (_) => _random.nextInt(maxIndex))
.map((i) => _autoIdCharacters[i]).join();
return autoId;
}

@override
DocumentReference document([String path]) {
if (path == null) {
path = _generateAutoId();
}

return MockDocumentReference(path, getSubpath(root, path), root,
getSubpath(snapshotStreamControllerRoot, path));
}
Expand Down
50 changes: 50 additions & 0 deletions test/cloud_firestore_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,56 @@ void main() {
final fooNameFirstName = fooName['firstName'] as String;
expect(fooNameFirstName, 'Tomo');
});

test('auto generate ID', () async {
final firestore = MockFirestoreInstance();
firestore.setupFieldValueFactory();

final reference1 = firestore.collection('users').document();
final document1Id = reference1.documentID;
final reference2 = firestore.collection('users').document();
expect(document1Id, isNot(reference2.documentID));

await reference1.setData({
'someField': 'someValue',
});
final snapshot1 = await reference1.get();
expect(snapshot1.exists, true);
// reference2 is not saved
final snapshot2 = await reference2.get();
expect(snapshot2.exists, false);

final snapshot =
await firestore.collection('users').document(document1Id).get();
expect(snapshot['someField'], 'someValue');

QuerySnapshot querySnapshot =
await firestore.collection('users').getDocuments();
// TODO: assert result length size. It should be 1.
// https://github.com/atn832/cloud_firestore_mocks/issues/20
Copy link
Collaborator Author

@suztomo suztomo Feb 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under current implementation, the query snapshot unexpectedly holds 2 documents.

expect(querySnapshot.documents.first['someField'], 'someValue');
});

test('Snapshot before saving data', () async {
final firestore = MockFirestoreInstance();
firestore.setupFieldValueFactory();

// These documents are not saved
final nonExistentId = 'salkdjfaarecikvdiko0';
final snapshot1 =
await firestore.collection('users').document(nonExistentId).get();
expect(snapshot1, isNotNull);
expect(snapshot1.documentID, nonExistentId);
// TODO: data field should be null before the document is saved
// https://github.com/atn832/cloud_firestore_mocks/issues/21
// expect(snapshot1.data, isNull);
Copy link
Collaborator Author

@suztomo suztomo Feb 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion fails under current implementation. It’s unexpectedly an empty map.

expect(snapshot1.exists, false);

final snapshot2 = await firestore.collection('users').document().get();
expect(snapshot2, isNotNull);
expect(snapshot2.documentID.length, 20);
expect(snapshot2.exists, false);
});
}

class QuerySnapshotMatcher implements Matcher {
Expand Down