Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api_core): provide a 'raw_page' field for page_iterator.Page #9486

Merged
merged 8 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api_core/.coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ exclude_lines =
def __repr__
# Ignore abstract methods
raise NotImplementedError
# Ignore assert False, meant to prevent false positive tests
assert False
software-dov marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 12 additions & 4 deletions api_core/google/api_core/page_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,22 @@ class Page(object):
Callable to convert an item from the type in the raw API response
into the native object. Will be called with the iterator and a
single item.
raw_page Optional[google.protobuf.message.Message]:
The raw page response.
"""

def __init__(self, parent, items, item_to_value):
def __init__(self, parent, items, item_to_value, raw_page):
software-dov marked this conversation as resolved.
Show resolved Hide resolved
self._parent = parent
self._num_items = len(items)
self._remaining = self._num_items
self._item_iter = iter(items)
self._item_to_value = item_to_value
self._raw_page = raw_page

@property
def raw_page(self):
"""google.protobuf.message.Message"""
return self._raw_page

@property
def num_items(self):
Expand Down Expand Up @@ -360,7 +368,7 @@ def _next_page(self):
if self._has_next_page():
response = self._get_next_page_response()
items = response.get(self._items_key, ())
page = Page(self, items, self.item_to_value)
page = Page(self, items, self.item_to_value, response)
software-dov marked this conversation as resolved.
Show resolved Hide resolved
self._page_start(self, page, response)
self.next_page_token = response.get(self._next_token)
return page
Expand Down Expand Up @@ -454,7 +462,7 @@ def _next_page(self):
"""
try:
items = six.next(self._gax_page_iter)
page = Page(self, items, self.item_to_value)
page = Page(self, items, self.item_to_value, None)
software-dov marked this conversation as resolved.
Show resolved Hide resolved
self.next_page_token = self._gax_page_iter.page_token or None
return page
except StopIteration:
Expand Down Expand Up @@ -527,7 +535,7 @@ def _next_page(self):

self.next_page_token = getattr(response, self._response_token_field)
items = getattr(response, self._items_field)
page = Page(self, items, self.item_to_value)
page = Page(self, items, self.item_to_value, response)
software-dov marked this conversation as resolved.
Show resolved Hide resolved

return page

Expand Down
28 changes: 22 additions & 6 deletions api_core/tests/unit/test_page_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def test_constructor(self):
parent = mock.sentinel.parent
item_to_value = mock.sentinel.item_to_value

page = page_iterator.Page(parent, (1, 2, 3), item_to_value)
page = page_iterator.Page(parent, (1, 2, 3), item_to_value, None)
software-dov marked this conversation as resolved.
Show resolved Hide resolved

assert page.num_items == 3
assert page.remaining == 3
assert page._parent is parent
assert page._item_to_value is item_to_value

def test___iter__(self):
page = page_iterator.Page(None, (), None)
page = page_iterator.Page(None, (), None, None)
software-dov marked this conversation as resolved.
Show resolved Hide resolved
assert iter(page) is page

def test_iterator_calls_parent_item_to_value(self):
Expand All @@ -48,7 +48,7 @@ def test_iterator_calls_parent_item_to_value(self):
side_effect=lambda iterator, value: value, spec=["__call__"]
)

page = page_iterator.Page(parent, (10, 11, 12), item_to_value)
page = page_iterator.Page(parent, (10, 11, 12), item_to_value, None)
page._remaining = 100

assert item_to_value.call_count == 0
Expand All @@ -69,6 +69,22 @@ def test_iterator_calls_parent_item_to_value(self):
item_to_value.assert_called_with(parent, 12)
assert page.remaining == 97

def test_raw_page(self):
parent = mock.sentinel.parent
item_to_value = mock.sentinel.item_to_value

raw_page = mock.sentinel.raw_page

page = page_iterator.Page(parent, (1, 2, 3), item_to_value, raw_page)
assert page.raw_page is raw_page

try:
page.raw_page = None
except AttributeError:
pass
else:
assert False, "The raw_page attribute should be a read-only property"
software-dov marked this conversation as resolved.
Show resolved Hide resolved


class PageIteratorImpl(page_iterator.Iterator):
def _next_page(self):
Expand Down Expand Up @@ -116,7 +132,7 @@ def test_pages_property_restart(self):
def test__page_iter_increment(self):
iterator = PageIteratorImpl(None, None)
page = page_iterator.Page(
iterator, ("item",), page_iterator._item_to_value_identity
iterator, ("item",), page_iterator._item_to_value_identity, None
)
iterator._next_page = mock.Mock(side_effect=[page, None])

Expand Down Expand Up @@ -147,10 +163,10 @@ def test__items_iter(self):
# Make pages from mock responses
parent = mock.sentinel.parent
page1 = page_iterator.Page(
parent, (item1, item2), page_iterator._item_to_value_identity
parent, (item1, item2), page_iterator._item_to_value_identity, None
)
page2 = page_iterator.Page(
parent, (item3,), page_iterator._item_to_value_identity
parent, (item3,), page_iterator._item_to_value_identity, None
)

iterator = PageIteratorImpl(None, None)
Expand Down