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() should not save a document #20

Closed
suztomo opened this issue Feb 26, 2020 · 2 comments · Fixed by #32
Closed

collection('...').document() should not save a document #20

suztomo opened this issue Feb 26, 2020 · 2 comments · Fixed by #32

Comments

@suztomo
Copy link
Collaborator

suztomo commented Feb 26, 2020

With current implementation, the following test code fails:

    final reference1 = firestore.collection('users').document();
    final reference2 = firestore.collection('users').document();

    await reference1.setData({
      'someField': 'someValue',
    });

    QuerySnapshot querySnapshot = await firestore.collection('users').getDocuments();
    // Only reference1 is saved
    expect(querySnapshot.documents, hasLength(1));
    expect(querySnapshot.documents.first['someField'], 'someValue');

It fails because as of now cloud_firestore_mocks does not distinguish saved and unsaved documents.

I suspect code below should be updated to accommodate this.

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

    return MockDocumentReference(path, getSubpath(root, path), root,
        getSubpath(snapshotStreamControllerRoot, path));
  }

getSubpath(root, path) creates a document to the root.

@suztomo
Copy link
Collaborator Author

suztomo commented Mar 2, 2020

Initial Attempt failed

Initial attempt that avoids modifying rootParent until updateData and setData failed.
suztomo@3ed76fe#diff-855d1299f8d5526ef0aacc2ddf24af06R28

This attempt fails to record subcollection whose parent is not saved.
cloud_firestore_mocks internally maintains documents as a tree using Map. When parent is not saved, getting collection reference creates a new map which does not have the subcolleciton.

This issue requires to save whether a document is stored in the instance or not. Maybe MockFirestoreInstance remembers all saved document location such as /users/t1gVCkBrgMU9LjFRnrtu1FFjlir1/persons/FkHWNY0ISMz9Zia0mF0q and MockDocumentReference and MockCollectionReference remembers MockFirestoreInstance.

@suztomo
Copy link
Collaborator Author

suztomo commented Mar 2, 2020

Note. In real Firestore, creating a non-top-level document does not create its parent document. For example, creating users/abc/friends/{documentId} does not create a document at users/abc.

    final firestore = Firestore.instance;
    await firestore.collection('dummy').document('abc')
    .collection('friends').document().setData(
      <String, dynamic>{
        'name': 'Tomo',
      }
    );

    final abcSaved = await firestore.collection('dummy').document('abc').get();
    if (abcSaved.exists) { // This is false.
      print('$abcSaved');
    }

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.

1 participant