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, 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])]) 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 _make_batch_response(**kwargs): from google.cloud.firestore_v1beta1.proto import firestore_pb2 return firestore_pb2.BatchGetDocumentsResponse(**kwargs)