def test_list_documents(self): # Setup Expected Response next_page_token = "" documents_element = {} documents = [documents_element] expected_response = { "next_page_token": next_page_token, "documents": documents } expected_response = firestore_pb2.ListDocumentsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = firestore_client.FirestoreClient() # Setup Request parent = client.any_path_path("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]") collection_id = "collectionId-821242276" paged_list_response = client.list_documents(parent, collection_id) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.documents[0] == resources[0] assert len(channel.requests) == 1 expected_request = firestore_pb2.ListDocumentsRequest( parent=parent, collection_id=collection_id) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_documents(self): # Setup Expected Response next_page_token = '' documents_element = {} documents = [documents_element] expected_response = { 'next_page_token': next_page_token, 'documents': documents } expected_response = firestore_pb2.ListDocumentsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = firestore_client.FirestoreClient(channel=channel) # Setup Request parent = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]', '[ANY_PATH]') collection_id = 'collectionId-821242276' paged_list_response = client.list_documents(parent, collection_id) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.documents[0] == resources[0] assert len(channel.requests) == 1 expected_request = firestore_pb2.ListDocumentsRequest( parent=parent, collection_id=collection_id) actual_request = channel.requests[0][1] assert expected_request == actual_request
def _documents(stub): """Probes to test ListDocuments grpc call from Firestore stub. Args: stub: An object of FirestoreStub. """ _documents_tracer = initialize_tracer() with _documents_tracer.span(name='_documents'): list_document_request = firestore_pb2.ListDocumentsRequest( parent=_PARENT_RESOURCE) with _documents_tracer.span('stub.ListDocuments'): stub.ListDocuments(list_document_request)
def _documents(stub): """Probes to test ListDocuments grpc call from Firestore stub. Args: stub: An object of FirestoreStub. """ _documents_tracer = initialize_tracer() with _documents_tracer.span(name='_documents') as root_span: root_span.add_annotation('endpoint info available', endpoint=_FIRESTORE_TARGET) list_document_request = firestore_pb2.ListDocumentsRequest( parent=_PARENT_RESOURCE) with _documents_tracer.span('stub.ListDocuments'): stub.ListDocuments(list_document_request)
def test_list_documents(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = firestore_client.FirestoreClient() # Mock request parent = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]', '[ANY_PATH]') collection_id = 'collectionId-821242276' # Mock response next_page_token = '' documents_element = {} documents = [documents_element] expected_response = { 'next_page_token': next_page_token, 'documents': documents } expected_response = firestore_pb2.ListDocumentsResponse( **expected_response) grpc_stub.ListDocuments.return_value = expected_response paged_list_response = client.list_documents(parent, collection_id) resources = list(paged_list_response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response.documents[0], resources[0]) grpc_stub.ListDocuments.assert_called_once() args, kwargs = grpc_stub.ListDocuments.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = firestore_pb2.ListDocumentsRequest( parent=parent, collection_id=collection_id) self.assertEqual(expected_request, actual_request)
def list_documents(self, parent, collection_id, page_size=None, order_by=None, mask=None, transaction=None, read_time=None, show_missing=None, options=None): """ Lists documents. Example: >>> from google.cloud import firestore_v1beta1 >>> from google.gax import CallOptions, INITIAL_PAGE >>> >>> client = firestore_v1beta1.FirestoreClient() >>> >>> parent = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]', '[ANY_PATH]') >>> collection_id = '' >>> >>> >>> # Iterate over all results >>> for element in client.list_documents(parent, collection_id): ... # process element ... pass >>> >>> # Or iterate over results one page at a time >>> for page in client.list_documents(parent, collection_id, options=CallOptions(page_token=INITIAL_PAGE)): ... for element in page: ... # process element ... pass Args: parent (str): The parent resource name. In the format: ``projects/{project_id}/databases/{database_id}/documents`` or ``projects/{project_id}/databases/{database_id}/documents/{document_path}``. For example: ``projects/my-project/databases/my-database/documents`` or ``projects/my-project/databases/my-database/documents/chatrooms/my-chatroom`` collection_id (str): The collection ID, relative to ``parent``, to list. For example: ``chatrooms`` or ``messages``. page_size (int): The maximum number of resources contained in the underlying API response. If page streaming is performed per- resource, this parameter does not affect the return value. If page streaming is performed per-page, this determines the maximum number of resources in a page. order_by (str): The order to sort results by. For example: ``priority desc, name``. mask (Union[dict, ~google.cloud.firestore_v1beta1.types.DocumentMask]): The fields to return. If not set, returns all fields. If a document has a field that is not present in this mask, that field will not be returned in the response. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.firestore_v1beta1.types.DocumentMask` transaction (bytes): Reads documents in a transaction. read_time (Union[dict, ~google.cloud.firestore_v1beta1.types.Timestamp]): Reads documents as they were at the given time. This may not be older than 60 seconds. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.firestore_v1beta1.types.Timestamp` show_missing (bool): If the list should show missing documents. A missing document is a document that does not exist but has sub-documents. These documents will be returned with a key but will not have fields, ``Document.create_time``, or ``Document.update_time`` set. Requests with ``show_missing`` may not specify ``where`` or ``order_by``. options (~google.gax.CallOptions): Overrides the default settings for this call, e.g, timeout, retries etc. Returns: A :class:`~google.gax.PageIterator` instance. By default, this is an iterable of :class:`~google.cloud.firestore_v1beta1.types.Document` instances. This object can also be configured to iterate over the pages of the response through the `options` parameter. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ # Sanity check: We have some fields which are mutually exclusive; # raise ValueError if more than one is sent. oneof.check_oneof( transaction=transaction, read_time=read_time, ) request = firestore_pb2.ListDocumentsRequest( parent=parent, collection_id=collection_id, page_size=page_size, order_by=order_by, mask=mask, transaction=transaction, read_time=read_time, show_missing=show_missing) return self._list_documents(request, options)