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

feat: asyncio microgen document #121

Merged
merged 8 commits into from Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion google/cloud/firestore_v1/async_collection.py
Expand Up @@ -110,7 +110,7 @@ async def list_documents(self, page_size=None):
"""
parent, _ = self._parent_info()

iterator = self._client._firestore_api.list_documents(
iterator = await self._client._firestore_api.list_documents(
request={
"parent": parent,
"collection_id": self.id,
Expand Down
8 changes: 4 additions & 4 deletions google/cloud/firestore_v1/async_document.py
Expand Up @@ -270,7 +270,7 @@ async def delete(self, option=None):
still return the time that the request was received by the server.
"""
write_pb = _helpers.pb_for_delete(self._document_path, option)
rafilong marked this conversation as resolved.
Show resolved Hide resolved
commit_response = self._client._firestore_api.commit(
commit_response = await self._client._firestore_api.commit(
request={
"database": self._client._database_string,
"writes": [write_pb],
Expand Down Expand Up @@ -318,7 +318,7 @@ async def get(self, field_paths=None, transaction=None):

firestore_api = self._client._firestore_api
try:
document_pb = firestore_api.get_document(
document_pb = await firestore_api.get_document(
request={
"name": self._document_path,
"mask": mask,
Expand Down Expand Up @@ -360,7 +360,7 @@ async def collections(self, page_size=None):
document does not exist at the time of `snapshot`, the
iterator will be empty
"""
iterator = self._client._firestore_api.list_collection_ids(
iterator = await self._client._firestore_api.list_collection_ids(
request={"parent": self._document_path, "page_size": page_size},
metadata=self._client._rpc_metadata,
)
Expand All @@ -369,7 +369,7 @@ async def collections(self, page_size=None):
for i in iterator.collection_ids:
yield self.collection(i)
if iterator.next_page_token:
iterator = self._client._firestore_api.list_collection_ids(
iterator = await self._client._firestore_api.list_collection_ids(
request={
"parent": self._document_path,
"page_size": page_size,
Expand Down
11 changes: 6 additions & 5 deletions tests/unit/v1/test_async_collection.py
Expand Up @@ -17,6 +17,7 @@
import aiounittest

import mock
from tests.unit.v1.test__helpers import AsyncMock


class MockAsyncIter:
Expand Down Expand Up @@ -196,7 +197,6 @@ async def _list_documents_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_document import AsyncDocumentReference
from google.cloud.firestore_v1.services.firestore.client import FirestoreClient
from google.cloud.firestore_v1.types.document import Document

class _Iterator(Iterator):
Expand All @@ -216,9 +216,10 @@ def _next_page(self):
Document(name=template.format(document_id)) for document_id in document_ids
]
iterator = _Iterator(pages=[documents])
api_client = mock.create_autospec(FirestoreClient)
api_client.list_documents.return_value = iterator
client._firestore_api_internal = api_client
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 = self._make_one("collection", client=client)

if page_size is not None:
Expand All @@ -234,7 +235,7 @@ def _next_page(self):
self.assertEqual(document.id, document_id)

parent, _ = collection._parent_info()
api_client.list_documents.assert_called_once_with(
firestore_api.list_documents.assert_called_once_with(
request={
"parent": parent,
"collection_id": collection.id,
Expand Down
15 changes: 8 additions & 7 deletions tests/unit/v1/test_async_document.py
Expand Up @@ -17,6 +17,7 @@
import aiounittest

import mock
from tests.unit.v1.test__helpers import AsyncMock


class TestAsyncDocumentReference(aiounittest.AsyncTestCase):
Expand Down Expand Up @@ -286,7 +287,7 @@ async def _delete_helper(self, **option_kwargs):
from google.cloud.firestore_v1.types import write

# Create a minimal fake GAPIC with a dummy response.
firestore_api = mock.Mock(spec=["commit"])
firestore_api = AsyncMock(spec=["commit"])
firestore_api.commit.return_value = self._make_commit_repsonse()

# Attach the fake GAPIC to a real client.
Expand Down Expand Up @@ -339,7 +340,7 @@ async def _get_helper(
# Create a minimal fake GAPIC with a dummy response.
create_time = 123
update_time = 234
firestore_api = mock.Mock(spec=["get_document"])
firestore_api = AsyncMock(spec=["get_document"])
response = mock.create_autospec(document.Document)
response.fields = {}
response.create_time = create_time
Expand Down Expand Up @@ -427,7 +428,6 @@ 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
from google.cloud.firestore_v1.services.firestore.client import FirestoreClient

# TODO(microgen): https://github.com/googleapis/gapic-generator-python/issues/516
class _Iterator(Iterator):
Expand All @@ -443,11 +443,12 @@ def _next_page(self):

collection_ids = ["coll-1", "coll-2"]
iterator = _Iterator(pages=[collection_ids])
api_client = mock.create_autospec(FirestoreClient)
api_client.list_collection_ids.return_value = iterator
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 = api_client
client._firestore_api_internal = firestore_api

# Actually make a document and call delete().
document = self._make_one("where", "we-are", client=client)
Expand All @@ -463,7 +464,7 @@ def _next_page(self):
self.assertEqual(collection.parent, document)
self.assertEqual(collection.id, collection_id)

api_client.list_collection_ids.assert_called_once_with(
firestore_api.list_collection_ids.assert_called_once_with(
request={"parent": document._document_path, "page_size": page_size},
metadata=client._rpc_metadata,
)
Expand Down