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]) 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 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_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_partition_query(self): # Setup Expected Response next_page_token = "" partitions_element = {} partitions = [partitions_element] expected_response = { "next_page_token": next_page_token, "partitions": partitions, } expected_response = firestore_pb2.PartitionQueryResponse( **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 = "parent-995424086" paged_list_response = client.partition_query(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.partitions[0] == resources[0] assert len(channel.requests) == 1 expected_request = firestore_pb2.PartitionQueryRequest(parent=parent) 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]) 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]") 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_write(self): # Setup Expected 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) # 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 = "database1789464955" request = {"database": database} request = firestore_pb2.WriteRequest(**request) requests = [request] response = client.write(requests) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 actual_requests = channel.requests[0][1] assert len(actual_requests) == 1 actual_request = list(actual_requests)[0] assert request == actual_request
def test_listen(self): # Setup Expected Response expected_response = {} expected_response = firestore_pb2.ListenResponse(**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]") request = {"database": database} request = firestore_pb2.ListenRequest(**request) requests = [request] response = client.listen(requests) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 actual_requests = channel.requests[0][1] assert len(actual_requests) == 1 actual_request = list(actual_requests)[0] assert request == actual_request
def test_run_query(self): # Setup Expected Response transaction = b"-34" skipped_results = 880286183 expected_response = { "transaction": transaction, "skipped_results": skipped_results, } expected_response = firestore_pb2.RunQueryResponse(**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 parent = client.any_path_path("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]") response = client.run_query(parent) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 expected_request = firestore_pb2.RunQueryRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_collection_ids(self): # Setup Expected Response next_page_token = "" collection_ids_element = "collectionIdsElement1368994900" collection_ids = [collection_ids_element] expected_response = { "next_page_token": next_page_token, "collection_ids": collection_ids, } expected_response = firestore_pb2.ListCollectionIdsResponse( **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]") paged_list_response = client.list_collection_ids(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.collection_ids[0] == resources[0] assert len(channel.requests) == 1 expected_request = firestore_pb2.ListCollectionIdsRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def _firestore_api(self): """Lazy-loading getter GAPIC Firestore API. Returns: :class:`~google.cloud.gapic.firestore.v1`.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. if self._emulator_host is not None: channel = firestore_grpc_transport.firestore_pb2_grpc.grpc.insecure_channel( self._emulator_host) else: 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, client_info=self._client_info) return self._firestore_api_internal
def test_create_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]) 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 = "parent-995424086" collection_id = "collectionId-821242276" document = {} response = client.create_document(parent, collection_id, "documentid", document) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.CreateDocumentRequest( parent=parent, collection_id=collection_id, document_id="documentid", document=document, ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_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]) 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 = {} response = client.create_document(parent, collection_id, document_id, document) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_pb2.CreateDocumentRequest( parent=parent, collection_id=collection_id, document_id=document_id, document=document, ) 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]) 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 = "parent-995424086" 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 _firestore_api(self): """Lazy-loading getter GAPIC Firestore API. Returns: ~.gapic.firestore.v1.firestore_client.FirestoreClient: The GAPIC client with the credentials of the current client. """ if self._firestore_api_internal is None: self._firestore_api_internal = firestore_client.FirestoreClient( credentials=self._credentials) return self._firestore_api_internal
def _firestore_api(self): """Lazy-loading getter GAPIC Firestore API. Returns: :class:`~google.cloud.gapic.firestore.v1`.firestore_client.FirestoreClient: <The GAPIC client with the credentials of the current client. """ if self._firestore_api_internal is None: self._firestore_api_internal = firestore_client.FirestoreClient( credentials=self._credentials, client_info=self._client_info) return self._firestore_api_internal
def test_delete_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 name = "name3373707" with pytest.raises(CustomException): client.delete_document(name)
def test_partition_query_exception(self): 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 = "parent-995424086" paged_list_response = client.partition_query(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_begin_transaction_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]") with pytest.raises(CustomException): client.begin_transaction(database)
def test_commit_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" with pytest.raises(CustomException): client.commit(database)
def test_list_collection_ids_exception(self): 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]") paged_list_response = client.list_collection_ids(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_delete_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 name = client.any_path_path("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]") with pytest.raises(CustomException): client.delete_document(name)
def test_list_documents_exception(self): 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 = "parent-995424086" collection_id = "collectionId-821242276" paged_list_response = client.list_documents(parent, collection_id) with pytest.raises(CustomException): list(paged_list_response)
def test_update_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 document = {} update_mask = {} with pytest.raises(CustomException): client.update_document(document, update_mask)
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 = "name3373707" 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 = "parent-995424086" collection_id = "collectionId-821242276" document = {} with pytest.raises(CustomException): client.create_document(parent, collection_id, "documentid", document)
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_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 = "database1789464955" request = {"database": database} request = firestore_pb2.WriteRequest(**request) requests = [request] with pytest.raises(CustomException): client.write(requests)
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_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 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_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