Skip to content

Commit

Permalink
feat: integrate microgen async client to document
Browse files Browse the repository at this point in the history
  • Loading branch information
rafilong committed Jul 21, 2020
1 parent 9377fbd commit 28f023c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions google/cloud/firestore_v1/async_document.py
Expand Up @@ -272,7 +272,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)
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 @@ -320,7 +320,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 @@ -362,7 +362,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 @@ -371,7 +371,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
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 @@ -300,7 +301,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 @@ -353,7 +354,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 @@ -441,7 +442,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 @@ -457,11 +457,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 @@ -477,7 +478,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

0 comments on commit 28f023c

Please sign in to comment.