def test_batch_get_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 database = client.database_root_path('[PROJECT]', '[DATABASE]') documents = [] # Mock response missing = 'missing1069449574' transaction = b'-34' expected_response = {'missing': missing, 'transaction': transaction} expected_response = firestore_pb2.BatchGetDocumentsResponse( **expected_response) grpc_stub.BatchGetDocuments.return_value = iter([expected_response]) response = client.batch_get_documents(database, documents) resources = list(response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response, resources[0]) grpc_stub.BatchGetDocuments.assert_called_once() args, kwargs = grpc_stub.BatchGetDocuments.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = firestore_pb2.BatchGetDocumentsRequest( database=database, documents=documents) self.assertEqual(expected_request, actual_request)
def test_batch_get_documents(self): # Setup Expected Response missing = "missing1069449574" transaction = b"-34" expected_response = {"missing": missing, "transaction": transaction} expected_response = firestore_pb2.BatchGetDocumentsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[iter([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 database = client.database_root_path("[PROJECT]", "[DATABASE]") documents = [] response = client.batch_get_documents(database, documents) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 expected_request = firestore_pb2.BatchGetDocumentsRequest( database=database, documents=documents) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_get_documents(self): # Setup Expected Response missing = 'missing1069449574' transaction = b'-34' expected_response = {'missing': missing, 'transaction': transaction} expected_response = firestore_pb2.BatchGetDocumentsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[iter([expected_response])]) client = firestore_client.FirestoreClient(channel=channel) # Setup Request database = client.database_root_path('[PROJECT]', '[DATABASE]') documents = [] response = client.batch_get_documents(database, documents) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 expected_request = firestore_pb2.BatchGetDocumentsRequest( database=database, documents=documents) actual_request = channel.requests[0][1] assert expected_request == actual_request
def batch_get_documents(self, database, documents, mask=None, transaction=None, new_transaction=None, read_time=None, options=None): """ Gets multiple documents. Documents returned by this method are not guaranteed to be returned in the same order that they were requested. Example: >>> from google.cloud import firestore_v1beta1 >>> >>> client = firestore_v1beta1.FirestoreClient() >>> >>> database = client.database_root_path('[PROJECT]', '[DATABASE]') >>> documents = [] >>> >>> for element in client.batch_get_documents(database, documents): ... # process element ... pass Args: database (str): The database name. In the format: ``projects/{project_id}/databases/{database_id}``. documents (list[str]): The names of the documents to retrieve. In the format: ``projects/{project_id}/databases/{database_id}/documents/{document_path}``. The request will fail if any of the document is not a child resource of the given ``database``. Duplicate names will be elided. 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. new_transaction (Union[dict, ~google.cloud.firestore_v1beta1.types.TransactionOptions]): Starts a new transaction and reads the documents. Defaults to a read-only transaction. The new transaction ID will be returned as the first response in the stream. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.firestore_v1beta1.types.TransactionOptions` 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` options (~google.gax.CallOptions): Overrides the default settings for this call, e.g, timeout, retries etc. Returns: Iterable[~google.cloud.firestore_v1beta1.types.BatchGetDocumentsResponse]. 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, new_transaction=new_transaction, read_time=read_time, ) request = firestore_pb2.BatchGetDocumentsRequest( database=database, documents=documents, mask=mask, transaction=transaction, new_transaction=new_transaction, read_time=read_time) return self._batch_get_documents(request, options)