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

collection.document() to assign a random ID to the document reference #14

Closed
suztomo opened this issue Feb 24, 2020 · 4 comments · Fixed by #22
Closed

collection.document() to assign a random ID to the document reference #14

suztomo opened this issue Feb 24, 2020 · 4 comments · Fixed by #22

Comments

@suztomo
Copy link
Collaborator

suztomo commented Feb 24, 2020

It would be great upon documentRefeference.setData, the document reference has a random String ID within the collection.

My expectation in the following test:

import 'package:cloud_firestore_mocks/cloud_firestore_mocks.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('documentRefeference.setData should set documentId',
      (WidgetTester tester) async {
    final fakeFirestore = MockFirestoreInstance()..setupFieldValueFactory();

    final documentRef1 = fakeFirestore
        .collection('users')
        .document();
    await documentRef1.setData(<String, dynamic>{
      'content': 'MockFirestoreInstance is great',
      'createdBy': 'Tomo',
    });
    
    expect(documentRef1.documentID, isNotNull); // This fails.

    final documentRef2 = fakeFirestore
        .collection('users')
        .document();
    await documentRef2.setData(<String, dynamic>{
      'content': 'Mock tests are good',
      'createdBy': 'Joichiro',
    });
    expect(documentRef2.documentID, isNotNull);

    expect(documentRef2.documentID, isNot(documentRef1.documentID));
  });
}

@suztomo
Copy link
Collaborator Author

suztomo commented Feb 25, 2020

But sometimes there isn't a meaningful ID for the document, and it's more convenient to let Cloud Firestore auto-generate an ID for you. You can do this by calling add()

https://firebase.google.com/docs/firestore/manage-data/add-data

@atn832
Copy link
Owner

atn832 commented Feb 25, 2020

You're right, a semi-random id is created in one of two cases:

  1. collectionRef.add(): already roughly supported at https://github.com/atn832/cloud_firestore_mocks/blob/878187ec0d94c26f921196b4f53317305db781f4/lib/src/mock_collection_reference.dart#L43
  2. collectionRef.document(): not supported yet

https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/CollectionReference/document.html:

DocumentReference document (
[String path]
)
If no path is provided, an auto-generated ID is used.

To fix this issue, we should make it so that mockCollectionRef.document() generates some ID, then returns a MockDocumentRef for that ID, perhaps something like:

  @override
  DocumentReference document([String path]) {
    if (path == null) {
      path = // make some id
    }
    return MockDocumentReference(path, getSubpath(root, path), root,
        getSubpath(snapshotStreamControllerRoot, path));
  }

Do you have such a use case in your projects? I have never run into it because I always use add().

@suztomo
Copy link
Collaborator Author

suztomo commented Feb 25, 2020

The test case reflects my use case. I don’t call “add” when setData is enough.

While it’s feasible to change my code, I prefer cloud_firestore_mocks behaves the same as real firestore as much as possible.

Memo:

Screen Shot 2020-02-25 at 5 29 51 PM

The random key (e.g., 0XZfFoJICLrYBtIQqCHP) contains 20-characters. Number, a-z, A-Z

@suztomo
Copy link
Collaborator Author

suztomo commented Feb 26, 2020

In real Firestore, document('nonexistent key').get returns a future of documentSnapshot but the document is not in Firebase Database.

    final x = await _personsCollection.document('nonexistentkey').get();

Screen Shot 2020-02-25 at 7 16 45 PM

Without specifying documentId, document().get() returns the same. Note that in both cases, 'exists' fields are false.

Screen Shot 2020-02-25 at 7 22 29 PM

@suztomo suztomo changed the title documentReference.setData to assign a random ID to the document reference collection.document() to assign a random ID to the document reference Feb 26, 2020
@atn832 atn832 closed this as completed in #22 Mar 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants