Skip to content

Commit

Permalink
fix: remove workaround for missing autogen ListCollectionIdsPager
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Oct 22, 2020
1 parent 560afa2 commit a0ff66c
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 159 deletions.
20 changes: 2 additions & 18 deletions google/cloud/firestore_v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,24 +284,8 @@ async def collections(
request=request, metadata=self._rpc_metadata, **kwargs,
)

while True:
for i in iterator.collection_ids:
yield self.collection(i)
if iterator.next_page_token:
next_request = request.copy()
next_request["page_token"] = iterator.next_page_token
iterator = await self._firestore_api.list_collection_ids(
request=next_request, metadata=self._rpc_metadata, **kwargs,
)
else:
return

# TODO(microgen): currently this method is rewritten to iterate/page itself.
# https://github.com/googleapis/gapic-generator-python/issues/516
# it seems the generator ought to be able to do this itself.
# iterator.client = self
# iterator.item_to_value = _item_to_collection_ref
# return iterator
for collection_id in iterator:
yield self.collection(collection_id)

def batch(self) -> AsyncWriteBatch:
"""Get a batch instance from this client.
Expand Down
19 changes: 2 additions & 17 deletions google/cloud/firestore_v1/async_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,20 +407,5 @@ async def collections(
request=request, metadata=self._client._rpc_metadata, **kwargs,
)

while True:
for i in iterator.collection_ids:
yield self.collection(i)
if iterator.next_page_token:
next_request = request.copy()
next_request["page_token"] = iterator.next_page_token
iterator = await self._client._firestore_api.list_collection_ids(
request=request, metadata=self._client._rpc_metadata, **kwargs
)
else:
return

# TODO(microgen): currently this method is rewritten to iterate/page itself.
# it seems the generator ought to be able to do this itself.
# iterator.document = self
# iterator.item_to_value = _item_to_collection_ref
# return iterator
for collection_id in iterator:
yield self.collection(collection_id)
11 changes: 0 additions & 11 deletions google/cloud/firestore_v1/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,17 +536,6 @@ def _get_doc_mask(field_paths: Iterable[str]) -> Optional[types.common.DocumentM
return types.DocumentMask(field_paths=field_paths)


def _item_to_collection_ref(iterator, item: str) -> Any:
"""Convert collection ID to collection ref.
Args:
iterator (google.api_core.page_iterator.GRPCIterator):
iterator response
item (str): ID of the collection
"""
return iterator.client.collection(item)


def _path_helper(path: tuple) -> Any:
"""Standardize path into a tuple of path segments.
Expand Down
11 changes: 0 additions & 11 deletions google/cloud/firestore_v1/base_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,3 @@ def _first_write_result(write_results: list) -> Any:
raise ValueError("Expected at least one write result")

return write_results[0]


def _item_to_collection_ref(iterator, item: str) -> Any:
"""Convert collection ID to collection ref.
Args:
iterator (google.api_core.page_iterator.GRPCIterator):
iterator response
item (str): ID of the collection
"""
return iterator.document.collection(item)
20 changes: 2 additions & 18 deletions google/cloud/firestore_v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,24 +280,8 @@ def collections(
request=request, metadata=self._rpc_metadata, **kwargs,
)

while True:
for i in iterator.collection_ids:
yield self.collection(i)
if iterator.next_page_token:
next_request = request.copy()
next_request["page_token"] = iterator.next_page_token
iterator = self._firestore_api.list_collection_ids(
request=next_request, metadata=self._rpc_metadata, **kwargs,
)
else:
return

# TODO(microgen): currently this method is rewritten to iterate/page itself.
# https://github.com/googleapis/gapic-generator-python/issues/516
# it seems the generator ought to be able to do this itself.
# iterator.client = self
# iterator.item_to_value = _item_to_collection_ref
# return iterator
for collection_id in iterator:
yield self.collection(collection_id)

def batch(self) -> WriteBatch:
"""Get a batch instance from this client.
Expand Down
19 changes: 2 additions & 17 deletions google/cloud/firestore_v1/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,23 +408,8 @@ def collections(
request=request, metadata=self._client._rpc_metadata, **kwargs,
)

while True:
for i in iterator.collection_ids:
yield self.collection(i)
if iterator.next_page_token:
next_request = request.copy()
next_request["page_token"] = iterator.next_page_token
iterator = self._client._firestore_api.list_collection_ids(
request=request, metadata=self._client._rpc_metadata, **kwargs
)
else:
return

# TODO(microgen): currently this method is rewritten to iterate/page itself.
# it seems the generator ought to be able to do this itself.
# iterator.document = self
# iterator.item_to_value = _item_to_collection_ref
# return iterator
for collection_id in iterator:
yield self.collection(collection_id)

def on_snapshot(self, callback: Callable) -> Watch:
"""Watch this document.
Expand Down
27 changes: 8 additions & 19 deletions tests/unit/v1/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,33 +196,22 @@ def test_document_factory_w_nested_path(self):
self.assertIsInstance(document2, AsyncDocumentReference)

async def _collections_helper(self, retry=None, timeout=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 import _helpers

collection_ids = ["users", "projects"]
client = self._make_default_one()
firestore_api = AsyncMock()
firestore_api.mock_add_spec(spec=["list_collection_ids"])
client._firestore_api_internal = firestore_api

# TODO(microgen): list_collection_ids isn't a pager.
# https://github.com/googleapis/gapic-generator-python/issues/516
class _Iterator(Iterator):
def __init__(self, pages):
super(_Iterator, self).__init__(client=None)
self._pages = pages
self.collection_ids = pages[0]
class Pager(object):
def __iter__(self):
yield from collection_ids

def _next_page(self):
if self._pages:
page, self._pages = self._pages[0], self._pages[1:]
return Page(self, page, self.item_to_value)
firestore_api = AsyncMock()
firestore_api.mock_add_spec(spec=["list_collection_ids"])
firestore_api.list_collection_ids.return_value = Pager()

client = self._make_default_one()
client._firestore_api_internal = firestore_api
kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout)
iterator = _Iterator(pages=[collection_ids])
firestore_api.list_collection_ids.return_value = iterator

collections = [c async for c in client.collections(**kwargs)]

Expand Down
20 changes: 5 additions & 15 deletions tests/unit/v1/test_async_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,27 +497,17 @@ async def test_get_with_transaction(self):
@pytest.mark.asyncio
async def _collections_helper(self, page_size=None, retry=None, timeout=None):
from google.cloud.firestore_v1 import _helpers
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

# TODO(microgen): https://github.com/googleapis/gapic-generator-python/issues/516
class _Iterator(Iterator):
def __init__(self, pages):
super(_Iterator, self).__init__(client=None)
self._pages = pages
self.collection_ids = pages[0]
collection_ids = ["coll-1", "coll-2"]

def _next_page(self):
if self._pages:
page, self._pages = self._pages[0], self._pages[1:]
return Page(self, page, self.item_to_value)
class Pager(object):
def __iter__(self):
yield from collection_ids

collection_ids = ["coll-1", "coll-2"]
iterator = _Iterator(pages=[collection_ids])
firestore_api = AsyncMock()
firestore_api.mock_add_spec(spec=["list_collection_ids"])
firestore_api.list_collection_ids.return_value = iterator
firestore_api.list_collection_ids.return_value = Pager()

client = _make_client()
client._firestore_api_internal = firestore_api
Expand Down
25 changes: 7 additions & 18 deletions tests/unit/v1/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,31 +195,20 @@ def test_document_factory_w_nested_path(self):

def _collections_helper(self, retry=None, timeout=None):
from google.cloud.firestore_v1 import _helpers
from google.api_core.page_iterator import Iterator
from google.api_core.page_iterator import Page
from google.cloud.firestore_v1.collection import CollectionReference

collection_ids = ["users", "projects"]
client = self._make_default_one()
firestore_api = mock.Mock(spec=["list_collection_ids"])
client._firestore_api_internal = firestore_api

# TODO(microgen): list_collection_ids isn't a pager.
# https://github.com/googleapis/gapic-generator-python/issues/516
class _Iterator(Iterator):
def __init__(self, pages):
super(_Iterator, self).__init__(client=None)
self._pages = pages
self.collection_ids = pages[0]
class Pager(object):
def __iter__(self):
yield from collection_ids

def _next_page(self):
if self._pages:
page, self._pages = self._pages[0], self._pages[1:]
return Page(self, page, self.item_to_value)
firestore_api = mock.Mock(spec=["list_collection_ids"])
firestore_api.list_collection_ids.return_value = Pager()

client = self._make_default_one()
client._firestore_api_internal = firestore_api
kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout)
iterator = _Iterator(pages=[collection_ids])
firestore_api.list_collection_ids.return_value = iterator

collections = list(client.collections(**kwargs))

Expand Down
20 changes: 5 additions & 15 deletions tests/unit/v1/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,28 +468,18 @@ def test_get_with_transaction(self):
self._get_helper(use_transaction=True)

def _collections_helper(self, page_size=None, retry=None, timeout=None):
from google.api_core.page_iterator import Iterator
from google.api_core.page_iterator import Page
from google.cloud.firestore_v1.collection import CollectionReference
from google.cloud.firestore_v1 import _helpers
from google.cloud.firestore_v1.services.firestore.client import FirestoreClient

# TODO(microgen): https://github.com/googleapis/gapic-generator-python/issues/516
class _Iterator(Iterator):
def __init__(self, pages):
super(_Iterator, self).__init__(client=None)
self._pages = pages
self.collection_ids = pages[0]
collection_ids = ["coll-1", "coll-2"]

def _next_page(self):
if self._pages:
page, self._pages = self._pages[0], self._pages[1:]
return Page(self, page, self.item_to_value)
class Pager(object):
def __iter__(self):
yield from collection_ids

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
api_client.list_collection_ids.return_value = Pager()

client = _make_client()
client._firestore_api_internal = api_client
Expand Down

0 comments on commit a0ff66c

Please sign in to comment.