def test_constructor(self):
        client = mock.sentinel.client
        items_field = "items"
        iterator = page_iterator.GRPCIterator(
            client, mock.sentinel.method, mock.sentinel.request, items_field
        )

        assert not iterator._started
        assert iterator.client is client
        assert iterator.max_results is None
        assert iterator.item_to_value is page_iterator._item_to_value_identity
        assert iterator._method == mock.sentinel.method
        assert iterator._request == mock.sentinel.request
        assert iterator._items_field == items_field
        assert (
            iterator._request_token_field
            == page_iterator.GRPCIterator._DEFAULT_REQUEST_TOKEN_FIELD
        )
        assert (
            iterator._response_token_field
            == page_iterator.GRPCIterator._DEFAULT_RESPONSE_TOKEN_FIELD
        )
        # Changing attributes.
        assert iterator.page_number == 0
        assert iterator.next_page_token is None
        assert iterator.num_results == 0
 def paged_method(request, **kwargs):
     """Wrapper that invokes a method and returns a page iterator."""
     iterator = page_iterator.GRPCIterator(
         client=None,
         method=functools.partial(func, **kwargs),
         request=request,
         items_field=items_field,
         request_token_field=request_token_field,
         response_token_field=response_token_field)
     return iterator
    def test_iterate(self):
        request = mock.Mock(spec=["page_token"], page_token=None)
        response1 = mock.Mock(items=["a", "b"], next_page_token="1")
        response2 = mock.Mock(items=["c"], next_page_token="2")
        response3 = mock.Mock(items=["d"], next_page_token="")
        method = mock.Mock(side_effect=[response1, response2, response3])
        iterator = page_iterator.GRPCIterator(mock.sentinel.client, method,
                                              request, "items")

        assert iterator.num_results == 0

        items = list(iterator)
        assert items == ["a", "b", "c", "d"]

        method.assert_called_with(request)
        assert method.call_count == 3
        assert request.page_token == "2"
    def test_iterate(self):
        request = mock.Mock(spec=['page_token'], page_token=None)
        response1 = mock.Mock(items=['a', 'b'], next_page_token='1')
        response2 = mock.Mock(items=['c'], next_page_token='2')
        response3 = mock.Mock(items=['d'], next_page_token='')
        method = mock.Mock(side_effect=[response1, response2, response3])
        iterator = page_iterator.GRPCIterator(mock.sentinel.client, method,
                                              request, 'items')

        assert iterator.num_results == 0

        items = list(iterator)
        assert items == ['a', 'b', 'c', 'd']

        method.assert_called_with(request)
        assert method.call_count == 3
        assert request.page_token == '2'
    def test_constructor_options(self):
        client = mock.sentinel.client
        items_field = 'items'
        request_field = 'request'
        response_field = 'response'
        iterator = page_iterator.GRPCIterator(
            client,
            mock.sentinel.method,
            mock.sentinel.request,
            items_field,
            item_to_value=mock.sentinel.item_to_value,
            request_token_field=request_field,
            response_token_field=response_field,
            max_results=42)

        assert iterator.client is client
        assert iterator.max_results == 42
        assert iterator._method == mock.sentinel.method
        assert iterator._request == mock.sentinel.request
        assert iterator._items_field == items_field
        assert iterator._item_to_value is mock.sentinel.item_to_value
        assert iterator._request_token_field == request_field
        assert iterator._response_token_field == response_field
    def list_operations(
            self, name, filter_,
            retry=gapic_v1.method.DEFAULT, timeout=gapic_v1.method.DEFAULT):
        """
        Lists operations that match the specified filter in the request.

        Example:
            >>> from google.api_core import operations_v1
            >>> api = operations_v1.OperationsClient()
            >>> name = ''
            >>>
            >>> # Iterate over all results
            >>> for operation in api.list_operations(name):
            >>>   # process operation
            >>>   pass
            >>>
            >>> # Or iterate over results one page at a time
            >>> iter = api.list_operations(name)
            >>> for page in iter.pages:
            >>>   for operation in page:
            >>>     # process operation
            >>>     pass

        Args:
            name (str): The name of the operation collection.
            filter_ (str): The standard list filter.
            retry (google.api_core.retry.Retry): The retry strategy to use
                when invoking the RPC. If unspecified, the default retry from
                the client configuration will be used. If ``None``, then this
                method will not retry the RPC at all.
            timeout (float): The amount of time in seconds to wait for the RPC
                to complete. Note that if ``retry`` is used, this timeout
                applies to each individual attempt and the overall time it
                takes for this method to complete may be longer. If
                unspecified, the the default timeout in the client
                configuration is used. If ``None``, then the RPC method will
                not time out.

        Returns:
            google.api_core.page_iterator.Iterator: An iterator that yields
                :class:`google.longrunning.operations_pb2.Operation` instances.

        Raises:
            google.api_core.exceptions.MethodNotImplemented: If the server
                does not support this method. Services are not required to
                implement this method.
            google.api_core.exceptions.GoogleAPICallError: If an error occurred
                while invoking the RPC, the appropriate ``GoogleAPICallError``
                subclass will be raised.
        """
        # Create the request object.
        request = operations_pb2.ListOperationsRequest(
            name=name, filter=filter_)

        # Create the method used to fetch pages
        method = functools.partial(
            self._list_operations, retry=retry, timeout=timeout)

        iterator = page_iterator.GRPCIterator(
            client=None,
            method=method,
            request=request,
            items_field='operations',
            request_token_field='page_token',
            response_token_field='next_page_token')

        return iterator