Skip to content

Commit

Permalink
feat: asyncio microgen document (#121)
Browse files Browse the repository at this point in the history
* feat: make collections call backed by async

* fix: failing asyncmock assertion

* fix: lint

* refactor: move AsyncMock to test helpers

* feat: integrate microgen async client to collection

* fix: lint

* feat: integrate microgen async client to document

* fix: docstring fixes
  • Loading branch information
rafilong committed Jul 22, 2020
1 parent 6281a67 commit 31faecb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
14 changes: 7 additions & 7 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)
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 All @@ -284,7 +284,7 @@ async def delete(self, option=None):
async def get(self, field_paths=None, transaction=None):
"""Retrieve a snapshot of the current document.
See :meth:`~google.cloud.firestore_v1.client.Client.field_path` for
See :meth:`~google.cloud.firestore_v1.base_client.BaseClient.field_path` for
more information on **field paths**.
If a ``transaction`` is used and it already has write operations
Expand All @@ -296,12 +296,12 @@ async def get(self, field_paths=None, transaction=None):
paths (``.``-delimited list of field names) to use as a
projection of document fields in the returned results. If
no value is provided, all fields will be returned.
transaction (Optional[:class:`~google.cloud.firestore_v1.transaction.Transaction`]):
transaction (Optional[:class:`~google.cloud.firestore_v1.async_transaction.AsyncTransaction`]):
An existing transaction that this reference
will be retrieved in.
Returns:
:class:`~google.cloud.firestore_v1.document.DocumentSnapshot`:
:class:`~google.cloud.firestore_v1.base_document.DocumentSnapshot`:
A snapshot of the current document. If the document does not
exist at the time of the snapshot is taken, the snapshot's
:attr:`reference`, :attr:`data`, :attr:`update_time`, and
Expand All @@ -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
4 changes: 2 additions & 2 deletions google/cloud/firestore_v1/document.py
Expand Up @@ -284,7 +284,7 @@ def delete(self, option=None):
def get(self, field_paths=None, transaction=None):
"""Retrieve a snapshot of the current document.
See :meth:`~google.cloud.firestore_v1.client.Client.field_path` for
See :meth:`~google.cloud.firestore_v1.base_client.BaseClient.field_path` for
more information on **field paths**.
If a ``transaction`` is used and it already has write operations
Expand All @@ -301,7 +301,7 @@ def get(self, field_paths=None, transaction=None):
will be retrieved in.
Returns:
:class:`~google.cloud.firestore_v1.document.DocumentSnapshot`:
:class:`~google.cloud.firestore_v1.base_document.DocumentSnapshot`:
A snapshot of the current document. If the document does not
exist at the time of the snapshot is taken, the snapshot's
:attr:`reference`, :attr:`data`, :attr:`update_time`, and
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

0 comments on commit 31faecb

Please sign in to comment.