From a4e5b00a4d59e3416061d5c1ed32a111097e88b3 Mon Sep 17 00:00:00 2001 From: Raphael Long Date: Wed, 22 Jul 2020 19:05:29 -0500 Subject: [PATCH] feat: asyncio microgen batch (#122) * refactor: move generated client instantiation out of base class * feat: integrate microgen async client to client * feat: make collections call backed by async * fix: failing asyncmock assertion * refactor: remove unused install * fix: lint * refactor: shared functionality in client to base class * refactor: move AsyncMock to test helpers * fix: return type in client docs * feat: integrate microgen async client to collection * fix: lint * feat: integrate microgen async client to document * feat: integrate microgen async client to batch * fix: use AsyncMock for batch async tests: * fix: collection and document testing batch --- google/cloud/firestore_v1/async_batch.py | 2 +- tests/unit/v1/test_async_batch.py | 7 ++++--- tests/unit/v1/test_async_collection.py | 4 ++-- tests/unit/v1/test_async_document.py | 10 +++++----- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/google/cloud/firestore_v1/async_batch.py b/google/cloud/firestore_v1/async_batch.py index d29c30235..983a3bd98 100644 --- a/google/cloud/firestore_v1/async_batch.py +++ b/google/cloud/firestore_v1/async_batch.py @@ -42,7 +42,7 @@ async def commit(self): in the same order as the changes were applied to this batch. A write result contains an ``update_time`` field. """ - commit_response = self._client._firestore_api.commit( + commit_response = await self._client._firestore_api.commit( request={ "database": self._client._database_string, "writes": self._write_pbs, diff --git a/tests/unit/v1/test_async_batch.py b/tests/unit/v1/test_async_batch.py index acb977d86..7a5504dc4 100644 --- a/tests/unit/v1/test_async_batch.py +++ b/tests/unit/v1/test_async_batch.py @@ -16,6 +16,7 @@ import aiounittest import mock +from tests.unit.v1.test__helpers import AsyncMock class TestAsyncWriteBatch(aiounittest.AsyncTestCase): @@ -43,7 +44,7 @@ async def test_commit(self): from google.cloud.firestore_v1.types import write # Create a minimal fake GAPIC with a dummy result. - firestore_api = mock.Mock(spec=["commit"]) + firestore_api = AsyncMock(spec=["commit"]) timestamp = timestamp_pb2.Timestamp(seconds=1234567, nanos=123456798) commit_response = firestore.CommitResponse( write_results=[write.WriteResult(), write.WriteResult()], @@ -87,7 +88,7 @@ async def test_as_context_mgr_wo_error(self): from google.cloud.firestore_v1.types import firestore from google.cloud.firestore_v1.types import write - firestore_api = mock.Mock(spec=["commit"]) + firestore_api = AsyncMock(spec=["commit"]) timestamp = timestamp_pb2.Timestamp(seconds=1234567, nanos=123456798) commit_response = firestore.CommitResponse( write_results=[write.WriteResult(), write.WriteResult()], @@ -124,7 +125,7 @@ async def test_as_context_mgr_wo_error(self): @pytest.mark.asyncio async def test_as_context_mgr_w_error(self): - firestore_api = mock.Mock(spec=["commit"]) + firestore_api = AsyncMock(spec=["commit"]) client = _make_client() client._firestore_api_internal = firestore_api batch = self._make_one(client) diff --git a/tests/unit/v1/test_async_collection.py b/tests/unit/v1/test_async_collection.py index d205cfbd2..bb002ea97 100644 --- a/tests/unit/v1/test_async_collection.py +++ b/tests/unit/v1/test_async_collection.py @@ -87,7 +87,7 @@ async def test_add_auto_assigned(self): from google.cloud.firestore_v1._helpers import pbs_for_create # Create a minimal fake GAPIC add attach it to a real client. - firestore_api = mock.Mock(spec=["create_document", "commit"]) + firestore_api = AsyncMock(spec=["create_document", "commit"]) write_result = mock.Mock( update_time=mock.sentinel.update_time, spec=["update_time"] ) @@ -153,7 +153,7 @@ async def test_add_explicit_id(self): from google.cloud.firestore_v1.async_document import AsyncDocumentReference # Create a minimal fake GAPIC with a dummy response. - firestore_api = mock.Mock(spec=["commit"]) + firestore_api = AsyncMock(spec=["commit"]) write_result = mock.Mock( update_time=mock.sentinel.update_time, spec=["update_time"] ) diff --git a/tests/unit/v1/test_async_document.py b/tests/unit/v1/test_async_document.py index 6d5c1f5d1..816f3b6b7 100644 --- a/tests/unit/v1/test_async_document.py +++ b/tests/unit/v1/test_async_document.py @@ -74,7 +74,7 @@ def _write_pb_for_create(document_path, document_data): @pytest.mark.asyncio async def test_create(self): # Create a minimal fake GAPIC with a dummy response. - firestore_api = mock.Mock() + firestore_api = AsyncMock() firestore_api.commit.mock_add_spec(spec=["commit"]) firestore_api.commit.return_value = self._make_commit_repsonse() @@ -105,7 +105,7 @@ async def test_create_empty(self): from google.cloud.firestore_v1.async_document import AsyncDocumentReference from google.cloud.firestore_v1.async_document import DocumentSnapshot - firestore_api = mock.Mock(spec=["commit"]) + firestore_api = AsyncMock(spec=["commit"]) document_reference = mock.create_autospec(AsyncDocumentReference) snapshot = mock.create_autospec(DocumentSnapshot) snapshot.exists = True @@ -155,7 +155,7 @@ def _write_pb_for_set(document_path, document_data, merge): @pytest.mark.asyncio async def _set_helper(self, merge=False, **option_kwargs): # 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. @@ -208,7 +208,7 @@ async def _update_helper(self, **option_kwargs): from google.cloud.firestore_v1.transforms import DELETE_FIELD # 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. @@ -268,7 +268,7 @@ async def test_update_with_precondition(self): @pytest.mark.asyncio async def test_empty_update(self): # 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.