Skip to content

Commit b5d9a24

Browse files
Using document_id as arg name to match Google's implementation. (#49)
* Using document_id as arg name to match Google's implementation. * Adding a test and a README entry for the document_id kwarg.
1 parent c7b16a7 commit b5d9a24

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ mock_db.collection('users').document('alovelace').update({'favourite.color': 're
5757
mock_db.collection('users').document('alovelace').update({'associates': ['Charles Babbage', 'Michael Faraday']})
5858
mock_db.collection('users').document('alovelace').collection('friends')
5959
mock_db.collection('users').document('alovelace').delete()
60+
mock_db.collection('users').document(document_id: 'alovelace').delete()
6061
mock_db.collection('users').add({'first': 'Ada', 'last': 'Lovelace'}, 'alovelace')
6162
mock_db.get_all([mock_db.collection('users').document('alovelace')])
6263
mock_db.document('users/alovelace')

mockfirestore/collection.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def __init__(self, data: Store, path: List[str],
1414
self._path = path
1515
self.parent = parent
1616

17-
def document(self, name: Optional[str] = None) -> DocumentReference:
17+
def document(self, document_id: Optional[str] = None) -> DocumentReference:
1818
collection = get_by_path(self._data, self._path)
19-
if name is None:
20-
name = generate_random_string()
21-
new_path = self._path + [name]
22-
if name not in collection:
19+
if document_id is None:
20+
document_id = generate_random_string()
21+
new_path = self._path + [document_id]
22+
if document_id not in collection:
2323
set_by_path(self._data, new_path, {})
2424
return DocumentReference(self._data, new_path, parent=self)
2525

tests/test_collection_reference.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,11 @@ def test_collection_addDocument(self):
489489

490490
with self.assertRaises(AlreadyExists):
491491
fs.collection('foo').add(doc_content)
492+
493+
def test_collection_useDocumentIdKwarg(self):
494+
fs = MockFirestore()
495+
fs._data = {'foo': {
496+
'first': {'id': 1}
497+
}}
498+
doc = fs.collection('foo').document(document_id='first').get()
499+
self.assertEqual({'id': 1}, doc.to_dict())

0 commit comments

Comments
 (0)