def test_listen_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) 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]') request = {'database': database} request = firestore_pb2.ListenRequest(**request) requests = [request] with pytest.raises(CustomException): client.listen(requests)
def test_list_documents_exception(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 exception response grpc_stub.ListDocuments.side_effect = CustomException() paged_list_response = client.list_documents(parent, collection_id) self.assertRaises(errors.GaxError, list, paged_list_response)
def test_listen_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) 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 = "database1789464955" request = {"database": database} request = firestore_pb2.ListenRequest(**request) requests = [request] with pytest.raises(CustomException): client.listen(requests)
def test_write_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) 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]") request = {"database": database} request = firestore_pb2.WriteRequest(**request) requests = [request] with pytest.raises(CustomException): client.write(requests)
def _make_firestore_api(client): """Create an instance of the GAPIC Firestore client. Args: client (~.firestore_v1beta1.client.Client): The client that holds configuration details. Returns: ~.gapic.firestore.v1beta1.firestore_client.FirestoreClient: A Firestore GAPIC client instance with the proper credentials. """ host = firestore_client.FirestoreClient.SERVICE_ADDRESS channel = make_secure_channel(client._credentials, DEFAULT_USER_AGENT, host) return firestore_client.FirestoreClient(channel=channel, lib_name='gccl', lib_version=__version__)
def test_delete_document(self): channel = ChannelStub() 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 name = client.any_path_path("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]") client.delete_document(name) assert len(channel.requests) == 1 expected_request = firestore_pb2.DeleteDocumentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_document_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) 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" document_id = "documentId506676927" document = {} with pytest.raises(CustomException): client.create_document(parent, collection_id, document_id, document)
def test_rollback(self): channel = ChannelStub() 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]") transaction = b"-34" client.rollback(database, transaction) assert len(channel.requests) == 1 expected_request = firestore_pb2.RollbackRequest( database=database, transaction=transaction) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_document_exception(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' document_id = 'documentId506676927' document = {} # Mock exception response grpc_stub.CreateDocument.side_effect = CustomException() self.assertRaises(errors.GaxError, client.create_document, parent, collection_id, document_id, document)
def test_get_document(self): # Setup Expected Response name_2 = 'name2-1052831874' expected_response = {'name': name_2} expected_response = document_pb2.Document(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = firestore_client.FirestoreClient(channel=channel) # Setup Request name = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]', '[ANY_PATH]') response = client.get_document(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.GetDocumentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_commit(self): # Setup Expected Response expected_response = {} expected_response = firestore_pb2.CommitResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = firestore_client.FirestoreClient(channel=channel) # Setup Request database = client.database_root_path('[PROJECT]', '[DATABASE]') writes = [] response = client.commit(database, writes) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.CommitRequest(database=database, writes=writes) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_begin_transaction(self): # Setup Expected Response transaction = b'-34' expected_response = {'transaction': transaction} expected_response = firestore_pb2.BeginTransactionResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = firestore_client.FirestoreClient(channel=channel) # Setup Request database = client.database_root_path('[PROJECT]', '[DATABASE]') response = client.begin_transaction(database) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.BeginTransactionRequest( database=database) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_document(self): # Setup Expected Response name = 'name3373707' expected_response = {'name': name} expected_response = document_pb2.Document(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = firestore_client.FirestoreClient(channel=channel) # Setup Request document = {} update_mask = {} response = client.update_document(document, update_mask) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.UpdateDocumentRequest( document=document, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_commit(self): # Setup Expected Response expected_response = {} expected_response = firestore_pb2.CommitResponse(**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 database = "database1789464955" response = client.commit(database) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.CommitRequest(database=database) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_delete_document(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = firestore_client.FirestoreClient() # Mock request name = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]', '[ANY_PATH]') client.delete_document(name) grpc_stub.DeleteDocument.assert_called_once() args, kwargs = grpc_stub.DeleteDocument.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = firestore_pb2.DeleteDocumentRequest(name=name) self.assertEqual(expected_request, actual_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 test_rollback(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]') transaction = b'-34' client.rollback(database, transaction) grpc_stub.Rollback.assert_called_once() args, kwargs = grpc_stub.Rollback.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = firestore_pb2.RollbackRequest( database=database, transaction=transaction) self.assertEqual(expected_request, actual_request)
def _firestore_api(self): """Lazy-loading getter GAPIC Firestore API. Returns: ~.gapic.firestore.v1beta1.firestore_client.FirestoreClient: The GAPIC client with the credentials of the current client. """ if self._firestore_api_internal is None: # Use a custom channel. # We need this in order to set appropriate keepalive options. channel = firestore_grpc_transport.FirestoreGrpcTransport.create_channel( self._target, credentials=self._credentials, options={"grpc.keepalive_time_ms": 30000}.items(), ) self._transport = firestore_grpc_transport.FirestoreGrpcTransport( address=self._target, channel=channel) self._firestore_api_internal = firestore_client.FirestoreClient( transport=self._transport) return self._firestore_api_internal
def test_get_document(self): # Setup Expected Response name_2 = "name2-1052831874" expected_response = {"name": name_2} expected_response = document_pb2.Document(**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 name = "name3373707" response = client.get_document(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.GetDocumentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_document(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' document_id = 'documentId506676927' document = {} # Mock response name = 'name3373707' expected_response = {'name': name} expected_response = document_pb2.Document(**expected_response) grpc_stub.CreateDocument.return_value = expected_response response = client.create_document(parent, collection_id, document_id, document) self.assertEqual(expected_response, response) grpc_stub.CreateDocument.assert_called_once() args, kwargs = grpc_stub.CreateDocument.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = firestore_pb2.CreateDocumentRequest( parent=parent, collection_id=collection_id, document_id=document_id, document=document) self.assertEqual(expected_request, actual_request)
def test_commit(self): # Setup Expected Response expected_response = {} expected_response = firestore_pb2.CommitResponse(**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 database = client.database_root_path('[PROJECT]', '[DATABASE]') writes = [] response = client.commit(database, writes) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.CommitRequest(database=database, writes=writes) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_document(self): # Setup Expected Response name_2 = 'name2-1052831874' expected_response = {'name': name_2} expected_response = document_pb2.Document(**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 name = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]', '[ANY_PATH]') response = client.get_document(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.GetDocumentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_write(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]') request = {'database': database} requests = [request] # Mock response stream_id = 'streamId-315624902' stream_token = b'122' expected_response = { 'stream_id': stream_id, 'stream_token': stream_token } expected_response = firestore_pb2.WriteResponse(**expected_response) grpc_stub.Write.return_value = iter([expected_response]) response = client.write(requests) resources = list(response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response, resources[0]) grpc_stub.Write.assert_called_once() args, kwargs = grpc_stub.Write.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_requests = args[0] self.assertEqual(1, len(actual_requests)) actual_request = list(actual_requests)[0] self.assertEqual(request, actual_request)