Skip to content

Commit

Permalink
fix: asyncio microgen client get_all type (#126)
Browse files Browse the repository at this point in the history
* feat: create AsyncIter class for mocking

* fix: type error on mocked return on batch_get_documents
  • Loading branch information
rafilong committed Jul 23, 2020
1 parent a4e5b00 commit 9095368
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion google/cloud/firestore_v1/async_client.py
Expand Up @@ -252,7 +252,7 @@ async def get_all(self, references, field_paths=None, transaction=None):
metadata=self._rpc_metadata,
)

for get_doc_response in response_iterator:
async for get_doc_response in response_iterator:
yield _parse_batch_get(get_doc_response, reference_map, self)

async def collections(self):
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/v1/test__helpers.py
Expand Up @@ -25,6 +25,15 @@ async def __call__(self, *args, **kwargs):
return super(AsyncMock, self).__call__(*args, **kwargs)


class AsyncIter:
def __init__(self, items):
self.items = items

async def __aiter__(self, **_):
for i in self.items:
yield i


class TestGeoPoint(unittest.TestCase):
@staticmethod
def _get_target_class():
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/v1/test_async_client.py
Expand Up @@ -18,7 +18,7 @@
import aiounittest

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


class TestAsyncClient(aiounittest.AsyncTestCase):
Expand Down Expand Up @@ -237,7 +237,7 @@ def _next_page(self):
async def _get_all_helper(self, client, references, document_pbs, **kwargs):
# Create a minimal fake GAPIC with a dummy response.
firestore_api = mock.Mock(spec=["batch_get_documents"])
response_iterator = iter(document_pbs)
response_iterator = AsyncIter(document_pbs)
firestore_api.batch_get_documents.return_value = response_iterator

# Attach the fake GAPIC to a real client.
Expand Down
19 changes: 5 additions & 14 deletions tests/unit/v1/test_async_collection.py
Expand Up @@ -17,16 +17,7 @@
import aiounittest

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


class MockAsyncIter:
def __init__(self, count):
self.count = count

async def __aiter__(self, **_):
for i in range(self.count):
yield i
from tests.unit.v1.test__helpers import AsyncMock, AsyncIter


class TestAsyncCollectionReference(aiounittest.AsyncTestCase):
Expand Down Expand Up @@ -258,7 +249,7 @@ async def test_list_documents_w_page_size(self):
async def test_get(self, query_class):
import warnings

query_class.return_value.stream.return_value = MockAsyncIter(3)
query_class.return_value.stream.return_value = AsyncIter(range(3))

collection = self._make_one("collection")
with warnings.catch_warnings(record=True) as warned:
Expand All @@ -280,7 +271,7 @@ async def test_get(self, query_class):
async def test_get_with_transaction(self, query_class):
import warnings

query_class.return_value.stream.return_value = MockAsyncIter(3)
query_class.return_value.stream.return_value = AsyncIter(range(3))

collection = self._make_one("collection")
transaction = mock.sentinel.txn
Expand All @@ -301,7 +292,7 @@ async def test_get_with_transaction(self, query_class):
@mock.patch("google.cloud.firestore_v1.async_query.AsyncQuery", autospec=True)
@pytest.mark.asyncio
async def test_stream(self, query_class):
query_class.return_value.stream.return_value = MockAsyncIter(3)
query_class.return_value.stream.return_value = AsyncIter(range(3))

collection = self._make_one("collection")
stream_response = collection.stream()
Expand All @@ -316,7 +307,7 @@ async def test_stream(self, query_class):
@mock.patch("google.cloud.firestore_v1.async_query.AsyncQuery", autospec=True)
@pytest.mark.asyncio
async def test_stream_with_transaction(self, query_class):
query_class.return_value.stream.return_value = MockAsyncIter(3)
query_class.return_value.stream.return_value = AsyncIter(range(3))

collection = self._make_one("collection")
transaction = mock.sentinel.txn
Expand Down

0 comments on commit 9095368

Please sign in to comment.