def test_import_documents(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name="operations/test_import_documents", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = firestore_admin_v1.FirestoreAdminClient() # Setup Request name = client.database_path("[PROJECT]", "[DATABASE]") response = client.import_documents(name) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = firestore_admin_pb2.ImportDocumentsRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_fields(self): # Setup Expected Response next_page_token = "" fields_element = {} fields = [fields_element] expected_response = { "next_page_token": next_page_token, "fields": fields } expected_response = firestore_admin_pb2.ListFieldsResponse( **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_admin_v1.FirestoreAdminClient() # Setup Request parent = client.parent_path("[PROJECT]", "[DATABASE]", "[COLLECTION_ID]") paged_list_response = client.list_fields(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.fields[0] == resources[0] assert len(channel.requests) == 1 expected_request = firestore_admin_pb2.ListFieldsRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_field(self): # Setup Expected Response name_2 = "name2-1052831874" expected_response = {"name": name_2} expected_response = field_pb2.Field(**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_admin_v1.FirestoreAdminClient() # Setup Request name = client.field_path( "[PROJECT]", "[DATABASE]", "[COLLECTION_ID]", "[FIELD_ID]" ) response = client.get_field(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_admin_pb2.GetFieldRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_index(self): # Setup Expected Response name = "name3373707" done = True expected_response = {"name": name, "done": done} expected_response = operations_pb2.Operation(**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_admin_v1.FirestoreAdminClient() # Setup Request parent = client.parent_path("[PROJECT]", "[DATABASE]", "[COLLECTION_ID]") index = {} response = client.create_index(parent, index) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_admin_pb2.CreateIndexRequest( parent=parent, index=index) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_index(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = index_pb2.Index(**expected_response) operation = operations_pb2.Operation( name="operations/test_create_index", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = firestore_admin_v1.FirestoreAdminClient() # Setup Request parent = client.collection_group_path("[PROJECT]", "[DATABASE]", "[COLLECTION]") index = {} response = client.create_index(parent, index) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = firestore_admin_pb2.CreateIndexRequest( parent=parent, index=index) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_export_documents(self): # Setup Expected Response name_2 = "name2-1052831874" done = True expected_response = {"name": name_2, "done": done} expected_response = operations_pb2.Operation(**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_admin_v1.FirestoreAdminClient() # Setup Request name = client.database_path("[PROJECT]", "[DATABASE]") response = client.export_documents(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = firestore_admin_pb2.ExportDocumentsRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_field(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = field_pb2.Field(**expected_response) operation = operations_pb2.Operation( name="operations/test_update_field", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = firestore_admin_v1.FirestoreAdminClient() # Setup Request field = {} response = client.update_field(field) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = firestore_admin_pb2.UpdateFieldRequest(field=field) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_export_documents_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_admin_v1.FirestoreAdminClient() # Setup request name = client.database_path("[PROJECT]", "[DATABASE]") with pytest.raises(CustomException): client.export_documents(name)
def test_list_fields_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_admin_v1.FirestoreAdminClient() # Setup request parent = client.parent_path("[PROJECT]", "[DATABASE]", "[COLLECTION_ID]") paged_list_response = client.list_fields(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_update_field_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_admin_v1.FirestoreAdminClient() # Setup request field = {} with pytest.raises(CustomException): client.update_field(field)
def test_create_index_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_admin_v1.FirestoreAdminClient() # Setup request parent = client.parent_path("[PROJECT]", "[DATABASE]", "[COLLECTION_ID]") index = {} with pytest.raises(CustomException): client.create_index(parent, index)
def test_get_field_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_admin_v1.FirestoreAdminClient() # Setup request name = client.field_path("[PROJECT]", "[DATABASE]", "[COLLECTION_ID]", "[FIELD_ID]") with pytest.raises(CustomException): client.get_field(name)
def test_delete_index(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_admin_v1.FirestoreAdminClient() # Setup Request name = client.index_path("[PROJECT]", "[DATABASE]", "[COLLECTION_ID]", "[INDEX_ID]") client.delete_index(name) assert len(channel.requests) == 1 expected_request = firestore_admin_pb2.DeleteIndexRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_import_documents_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_import_documents_exception", done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = firestore_admin_v1.FirestoreAdminClient() # Setup Request name = client.database_path("[PROJECT]", "[DATABASE]") response = client.import_documents(name) exception = response.exception() assert exception.errors[0] == error
def test_update_field_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_update_field_exception", done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = firestore_admin_v1.FirestoreAdminClient() # Setup Request field = {} response = client.update_field(field) exception = response.exception() assert exception.errors[0] == error
def test_create_index_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_create_index_exception", done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = firestore_admin_v1.FirestoreAdminClient() # Setup Request parent = client.collection_group_path("[PROJECT]", "[DATABASE]", "[COLLECTION]") index = {} response = client.create_index(parent, index) exception = response.exception() assert exception.errors[0] == error
import json import sys import google.api_core.exceptions as gcp_exceptions import google.auth from google.cloud import firestore_admin_v1 catalogfile = open(sys.argv[1], "r") catalog = json.load(catalogfile) credentials, project_id = google.auth.default() fs_client = firestore_admin_v1.FirestoreAdminClient(credentials=credentials) def get_order_value(order): """ Returns order value based on google.firestore.admin.v1.Index.IndexField.Order: - ORDER_UNSPECIFIED: 0 - ASCENDING: 1 - DESCENDING: 2 """ if not order: return 0 if order == "asc": return 1 if order == "desc": return 2 return order