Skip to content

Commit 31faecb

Browse files
authored
feat: asyncio microgen document (#121)
* 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
1 parent 6281a67 commit 31faecb

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

‎google/cloud/firestore_v1/async_document.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ async def delete(self, option=None):
270270
still return the time that the request was received by the server.
271271
"""
272272
write_pb = _helpers.pb_for_delete(self._document_path, option)
273-
commit_response = self._client._firestore_api.commit(
273+
commit_response = await self._client._firestore_api.commit(
274274
request={
275275
"database": self._client._database_string,
276276
"writes": [write_pb],
@@ -284,7 +284,7 @@ async def delete(self, option=None):
284284
async def get(self, field_paths=None, transaction=None):
285285
"""Retrieve a snapshot of the current document.
286286
287-
See :meth:`~google.cloud.firestore_v1.client.Client.field_path` for
287+
See :meth:`~google.cloud.firestore_v1.base_client.BaseClient.field_path` for
288288
more information on **field paths**.
289289
290290
If a ``transaction`` is used and it already has write operations
@@ -296,12 +296,12 @@ async def get(self, field_paths=None, transaction=None):
296296
paths (``.``-delimited list of field names) to use as a
297297
projection of document fields in the returned results. If
298298
no value is provided, all fields will be returned.
299-
transaction (Optional[:class:`~google.cloud.firestore_v1.transaction.Transaction`]):
299+
transaction (Optional[:class:`~google.cloud.firestore_v1.async_transaction.AsyncTransaction`]):
300300
An existing transaction that this reference
301301
will be retrieved in.
302302
303303
Returns:
304-
:class:`~google.cloud.firestore_v1.document.DocumentSnapshot`:
304+
:class:`~google.cloud.firestore_v1.base_document.DocumentSnapshot`:
305305
A snapshot of the current document. If the document does not
306306
exist at the time of the snapshot is taken, the snapshot's
307307
:attr:`reference`, :attr:`data`, :attr:`update_time`, and
@@ -318,7 +318,7 @@ async def get(self, field_paths=None, transaction=None):
318318

319319
firestore_api = self._client._firestore_api
320320
try:
321-
document_pb = firestore_api.get_document(
321+
document_pb = await firestore_api.get_document(
322322
request={
323323
"name": self._document_path,
324324
"mask": mask,
@@ -360,7 +360,7 @@ async def collections(self, page_size=None):
360360
document does not exist at the time of `snapshot`, the
361361
iterator will be empty
362362
"""
363-
iterator = self._client._firestore_api.list_collection_ids(
363+
iterator = await self._client._firestore_api.list_collection_ids(
364364
request={"parent": self._document_path, "page_size": page_size},
365365
metadata=self._client._rpc_metadata,
366366
)
@@ -369,7 +369,7 @@ async def collections(self, page_size=None):
369369
for i in iterator.collection_ids:
370370
yield self.collection(i)
371371
if iterator.next_page_token:
372-
iterator = self._client._firestore_api.list_collection_ids(
372+
iterator = await self._client._firestore_api.list_collection_ids(
373373
request={
374374
"parent": self._document_path,
375375
"page_size": page_size,

‎google/cloud/firestore_v1/document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def delete(self, option=None):
284284
def get(self, field_paths=None, transaction=None):
285285
"""Retrieve a snapshot of the current document.
286286
287-
See :meth:`~google.cloud.firestore_v1.client.Client.field_path` for
287+
See :meth:`~google.cloud.firestore_v1.base_client.BaseClient.field_path` for
288288
more information on **field paths**.
289289
290290
If a ``transaction`` is used and it already has write operations
@@ -301,7 +301,7 @@ def get(self, field_paths=None, transaction=None):
301301
will be retrieved in.
302302
303303
Returns:
304-
:class:`~google.cloud.firestore_v1.document.DocumentSnapshot`:
304+
:class:`~google.cloud.firestore_v1.base_document.DocumentSnapshot`:
305305
A snapshot of the current document. If the document does not
306306
exist at the time of the snapshot is taken, the snapshot's
307307
:attr:`reference`, :attr:`data`, :attr:`update_time`, and

‎tests/unit/v1/test_async_document.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import aiounittest
1818

1919
import mock
20+
from tests.unit.v1.test__helpers import AsyncMock
2021

2122

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

288289
# Create a minimal fake GAPIC with a dummy response.
289-
firestore_api = mock.Mock(spec=["commit"])
290+
firestore_api = AsyncMock(spec=["commit"])
290291
firestore_api.commit.return_value = self._make_commit_repsonse()
291292

292293
# Attach the fake GAPIC to a real client.
@@ -339,7 +340,7 @@ async def _get_helper(
339340
# Create a minimal fake GAPIC with a dummy response.
340341
create_time = 123
341342
update_time = 234
342-
firestore_api = mock.Mock(spec=["get_document"])
343+
firestore_api = AsyncMock(spec=["get_document"])
343344
response = mock.create_autospec(document.Document)
344345
response.fields = {}
345346
response.create_time = create_time
@@ -427,7 +428,6 @@ async def _collections_helper(self, page_size=None):
427428
from google.api_core.page_iterator import Iterator
428429
from google.api_core.page_iterator import Page
429430
from google.cloud.firestore_v1.async_collection import AsyncCollectionReference
430-
from google.cloud.firestore_v1.services.firestore.client import FirestoreClient
431431

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

444444
collection_ids = ["coll-1", "coll-2"]
445445
iterator = _Iterator(pages=[collection_ids])
446-
api_client = mock.create_autospec(FirestoreClient)
447-
api_client.list_collection_ids.return_value = iterator
446+
firestore_api = AsyncMock()
447+
firestore_api.mock_add_spec(spec=["list_collection_ids"])
448+
firestore_api.list_collection_ids.return_value = iterator
448449

449450
client = _make_client()
450-
client._firestore_api_internal = api_client
451+
client._firestore_api_internal = firestore_api
451452

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

466-
api_client.list_collection_ids.assert_called_once_with(
467+
firestore_api.list_collection_ids.assert_called_once_with(
467468
request={"parent": document._document_path, "page_size": page_size},
468469
metadata=client._rpc_metadata,
469470
)

0 commit comments

Comments
 (0)