async def _collections_helper(self, retry=None, timeout=None): from google.cloud.firestore_v1.async_collection import AsyncCollectionReference from google.cloud.firestore_v1 import _helpers collection_ids = ["users", "projects"] class Pager(object): async def __aiter__(self, **_): for collection_id in collection_ids: yield collection_id firestore_api = AsyncMock() firestore_api.mock_add_spec(spec=["list_collection_ids"]) firestore_api.list_collection_ids.return_value = Pager() client = self._make_default_one() client._firestore_api_internal = firestore_api kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout) collections = [c async for c in client.collections(**kwargs)] self.assertEqual(len(collections), len(collection_ids)) for collection, collection_id in zip(collections, collection_ids): self.assertIsInstance(collection, AsyncCollectionReference) self.assertEqual(collection.parent, None) self.assertEqual(collection.id, collection_id) base_path = client._database_string + "/documents" firestore_api.list_collection_ids.assert_called_once_with( request={"parent": base_path}, metadata=client._rpc_metadata, **kwargs, )
async def _list_documents_helper(page_size=None, retry=None, timeout=None): from google.cloud.firestore_v1 import _helpers from google.api_core.page_iterator_async import AsyncIterator from google.api_core.page_iterator import Page from google.cloud.firestore_v1.async_document import AsyncDocumentReference from google.cloud.firestore_v1.types.document import Document class _AsyncIterator(AsyncIterator): def __init__(self, pages): super(_AsyncIterator, self).__init__(client=None) self._pages = pages async def _next_page(self): if self._pages: page, self._pages = self._pages[0], self._pages[1:] return Page(self, page, self.item_to_value) client = _make_client() template = client._database_string + "/documents/{}" document_ids = ["doc-1", "doc-2"] documents = [ Document(name=template.format(document_id)) for document_id in document_ids ] iterator = _AsyncIterator(pages=[documents]) firestore_api = AsyncMock() firestore_api.mock_add_spec(spec=["list_documents"]) firestore_api.list_documents.return_value = iterator client._firestore_api_internal = firestore_api collection = _make_async_collection_reference("collection", client=client) kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout) if page_size is not None: documents = [ i async for i in collection.list_documents(page_size=page_size, **kwargs,) ] else: documents = [i async for i in collection.list_documents(**kwargs)] # Verify the response and the mocks. assert len(documents) == len(document_ids) for document, document_id in zip(documents, document_ids): assert isinstance(document, AsyncDocumentReference) assert document.parent == collection assert document.id == document_id parent, _ = collection._parent_info() firestore_api.list_documents.assert_called_once_with( request={ "parent": parent, "collection_id": collection.id, "page_size": page_size, "show_missing": True, "mask": {"field_paths": None}, }, metadata=client._rpc_metadata, **kwargs, )
async def _collections_helper(self, page_size=None): from google.api_core.page_iterator import Iterator from google.api_core.page_iterator import Page from google.cloud.firestore_v1.async_collection import AsyncCollectionReference # TODO(microgen): https://github.com/googleapis/gapic-generator-python/issues/516 class _Iterator(Iterator): def __init__(self, pages): super(_Iterator, self).__init__(client=None) self._pages = pages self.collection_ids = pages[0] def _next_page(self): if self._pages: page, self._pages = self._pages[0], self._pages[1:] return Page(self, page, self.item_to_value) collection_ids = ["coll-1", "coll-2"] iterator = _Iterator(pages=[collection_ids]) firestore_api = AsyncMock() firestore_api.mock_add_spec(spec=["list_collection_ids"]) firestore_api.list_collection_ids.return_value = iterator client = _make_client() client._firestore_api_internal = firestore_api # Actually make a document and call delete(). document = self._make_one("where", "we-are", client=client) if page_size is not None: collections = [ c async for c in document.collections(page_size=page_size) ] else: collections = [c async for c in document.collections()] # Verify the response and the mocks. self.assertEqual(len(collections), len(collection_ids)) for collection, collection_id in zip(collections, collection_ids): self.assertIsInstance(collection, AsyncCollectionReference) self.assertEqual(collection.parent, document) self.assertEqual(collection.id, collection_id) firestore_api.list_collection_ids.assert_called_once_with( request={ "parent": document._document_path, "page_size": page_size }, metadata=client._rpc_metadata, )
async def _collections_helper(self, page_size=None, retry=None, timeout=None): from google.cloud.firestore_v1 import _helpers from google.cloud.firestore_v1.async_collection import AsyncCollectionReference collection_ids = ["coll-1", "coll-2"] class Pager(object): async def __aiter__(self, **_): for collection_id in collection_ids: yield collection_id firestore_api = AsyncMock() firestore_api.mock_add_spec(spec=["list_collection_ids"]) firestore_api.list_collection_ids.return_value = Pager() client = _make_client() client._firestore_api_internal = firestore_api kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout) # Actually make a document and call delete(). document = self._make_one("where", "we-are", client=client) if page_size is not None: collections = [ c async for c in document.collections(page_size=page_size, **kwargs) ] else: collections = [c async for c in document.collections(**kwargs)] # Verify the response and the mocks. self.assertEqual(len(collections), len(collection_ids)) for collection, collection_id in zip(collections, collection_ids): self.assertIsInstance(collection, AsyncCollectionReference) self.assertEqual(collection.parent, document) self.assertEqual(collection.id, collection_id) firestore_api.list_collection_ids.assert_called_once_with( request={ "parent": document._document_path, "page_size": page_size }, metadata=client._rpc_metadata, **kwargs, )
async def test_collections(self): from google.api_core.page_iterator import Iterator from google.api_core.page_iterator import Page from google.cloud.firestore_v1.async_collection import AsyncCollectionReference collection_ids = ["users", "projects"] client = self._make_default_one() firestore_api = AsyncMock() firestore_api.mock_add_spec(spec=["list_collection_ids"]) client._firestore_api_internal = firestore_api # TODO(microgen): list_collection_ids isn't a pager. # https://github.com/googleapis/gapic-generator-python/issues/516 class _Iterator(Iterator): def __init__(self, pages): super(_Iterator, self).__init__(client=None) self._pages = pages self.collection_ids = pages[0] def _next_page(self): if self._pages: page, self._pages = self._pages[0], self._pages[1:] return Page(self, page, self.item_to_value) iterator = _Iterator(pages=[collection_ids]) firestore_api.list_collection_ids.return_value = iterator collections = [c async for c in client.collections()] self.assertEqual(len(collections), len(collection_ids)) for collection, collection_id in zip(collections, collection_ids): self.assertIsInstance(collection, AsyncCollectionReference) self.assertEqual(collection.parent, None) self.assertEqual(collection.id, collection_id) base_path = client._database_string + "/documents" firestore_api.list_collection_ids.assert_called_once_with( request={"parent": base_path}, metadata=client._rpc_metadata)