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

Add list collections to doc ref #80

Closed
Closed
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
4 changes: 4 additions & 0 deletions mockfirestore/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def __init__(self, data: Store, path: List[str],
self._path = path
self.parent = parent

@property
def id(self):
return self._path[-1]

def document(self, document_id: Optional[str] = None) -> DocumentReference:
collection = get_by_path(self._data, self._path)
if document_id is None:
Expand Down
16 changes: 15 additions & 1 deletion mockfirestore/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def __init__(self, data: Store, path: List[str],
def id(self):
return self._path[-1]

def get(self) -> DocumentSnapshot:
@property
def path(self):
return '/'.join(self._path)

def get(self, **kwargs) -> DocumentSnapshot:
return DocumentSnapshot(self, get_by_path(self._data, self._path))

def delete(self):
Expand Down Expand Up @@ -92,3 +96,13 @@ def collection(self, name) -> 'CollectionReference':
if name not in document:
set_by_path(self._data, new_path, {})
return CollectionReference(self._data, new_path, parent=self)

def collections(self) -> list['CollectionReference']:
from mockfirestore.collection import CollectionReference
document = get_by_path(self._data, self._path)
_collections = []
for collname, values in document.items():
if isinstance(values, dict):
new_path = self._path + [collname]
_collections.append(CollectionReference(self._data, new_path, parent=self))
return _collections
36 changes: 35 additions & 1 deletion tests/test_document_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,31 @@ def test_get_document_by_path(self):
self.assertEqual({'id': 1}, doc.to_dict())
self.assertEqual('first', doc.id)

def test_get_document_by_path_with_transaction_kwarg(self):
fs = MockFirestore()
fs._data = {'foo': {
'first': {'id': 1}
}}
doc = fs.document('foo/first').get(transaction='tx')
self.assertEqual({'id': 1}, doc.to_dict())
self.assertEqual('first', doc.id)

def test_document_path_property(self):
fs = MockFirestore()
fs._data = {'foo': {
'first': {'id': 1}
}}
doc = fs.document('foo/first')
self.assertEqual('foo/first', doc.path)

def test_set_document_by_path(self):
fs = MockFirestore()
fs._data = {}
doc_content = {'id': 'bar'}
fs.document('foo/doc1/bar/doc2').set(doc_content)
doc = fs.document('foo/doc1/bar/doc2').get().to_dict()
self.assertEqual(doc_content, doc)

def test_document_get_returnsDocument(self):
fs = MockFirestore()
fs._data = {'foo': {
Expand Down Expand Up @@ -72,6 +89,23 @@ def test_get_nestedDocument(self):

self.assertEqual({'id': 1.1}, doc)

def test_get_nestedCollections(self):
fs = MockFirestore()
fs._data = {'top_collection': {
'top_document': {
'id': 1,
'nested_collection': {
'nested_document': {'id': 1.1}
}
}
}}
collections = fs.collection('top_collection')\
.document('top_document')\
.collections()

self.assertEqual(1, len(collections))
self.assertEqual(collections[0].id, 'nested_collection')

def test_get_nestedDocument_documentDoesNotExist(self):
fs = MockFirestore()
fs._data = {'top_collection': {
Expand Down
Loading