def test_list_app_profiles(self): from google.api_core.page_iterator import Iterator from google.api_core.page_iterator import Page from google.cloud.bigtable_admin_v2.gapic import bigtable_instance_admin_client from google.cloud.bigtable_admin_v2.proto import instance_pb2 as data_v2_pb2 from google.cloud.bigtable.app_profile import AppProfile class _Iterator(Iterator): def __init__(self, pages): super(_Iterator, self).__init__(client=None) self._pages = pages def _next_page(self): if self._pages: page, self._pages = self._pages[0], self._pages[1:] return Page(self, page, self.item_to_value) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) # Setup Expected Response app_profile_path_template = "projects/{}/instances/{}/appProfiles/{}" app_profile_id1 = "app-profile-id1" app_profile_id2 = "app-profile-id2" app_profile_name1 = app_profile_path_template.format( self.PROJECT, self.INSTANCE_ID, app_profile_id1) app_profile_name2 = app_profile_path_template.format( self.PROJECT, self.INSTANCE_ID, app_profile_id2) routing_policy = data_v2_pb2.AppProfile.MultiClusterRoutingUseAny() app_profiles = [ data_v2_pb2.AppProfile( name=app_profile_name1, multi_cluster_routing_use_any=routing_policy), data_v2_pb2.AppProfile( name=app_profile_name2, multi_cluster_routing_use_any=routing_policy), ] iterator = _Iterator(pages=[app_profiles]) # Patch the stub used by the API method. instance_api = mock.create_autospec( bigtable_instance_admin_client.BigtableInstanceAdminClient) client._instance_admin_client = instance_api instance_api.app_profile_path = app_profile_path_template.format instance_api.list_app_profiles.return_value = iterator # Perform the method and check the result. app_profiles = instance.list_app_profiles() app_profile_1, app_profile_2 = app_profiles self.assertIsInstance(app_profile_1, AppProfile) self.assertEqual(app_profile_1.name, app_profile_name1) self.assertIsInstance(app_profile_2, AppProfile) self.assertEqual(app_profile_2.name, app_profile_name2)
def test_update_app_profile(self): # Setup Expected Response name = 'name3373707' etag = 'etag3123477' description = 'description-1724546052' expected_response = { 'name': name, 'etag': etag, 'description': description } expected_response = instance_pb2.AppProfile(**expected_response) operation = operations_pb2.Operation( name='operations/test_update_app_profile', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request app_profile = instance_pb2.AppProfile() update_mask = {} response = client.update_app_profile(app_profile, update_mask) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.UpdateAppProfileRequest( app_profile=app_profile, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_app_profiles(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud.bigtable_admin_v2.proto import ( instance_pb2 as data_v2_pb2) from google.cloud.bigtable.app_profile import AppProfile instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) # Setup Expected Response next_page_token = '' app_profile_id1 = 'app-profile-id1' app_profile_id2 = 'app-profile-id2' app_profile_name1 = (client.instance_admin_client.app_profile_path( self.PROJECT, self.INSTANCE_ID, app_profile_id1)) app_profile_name2 = (client.instance_admin_client.app_profile_path( self.PROJECT, self.INSTANCE_ID, app_profile_id2)) routing_policy = data_v2_pb2.AppProfile.MultiClusterRoutingUseAny() expected_response = messages_v2_pb2.ListAppProfilesResponse( next_page_token=next_page_token, app_profiles=[ data_v2_pb2.AppProfile( name=app_profile_name1, multi_cluster_routing_use_any=routing_policy, ), data_v2_pb2.AppProfile( name=app_profile_name2, multi_cluster_routing_use_any=routing_policy, ) ], ) # Patch the stub used by the API method. client._instance_admin_client = instance_api bigtable_instance_stub = ( client._instance_admin_client.transport) bigtable_instance_stub.list_app_profiles.side_effect = [ expected_response] # Perform the method and check the result. app_profiles = instance.list_app_profiles() app_profile_1, app_profile_2 = app_profiles self.assertIsInstance(app_profile_1, AppProfile) self.assertEqual(app_profile_1.name, app_profile_name1) self.assertIsInstance(app_profile_2, AppProfile) self.assertEqual(app_profile_2.name, app_profile_name2)
def test_update_app_profile(self): # Setup Expected Response name = "name3373707" etag = "etag3123477" description = "description-1724546052" expected_response = { "name": name, "etag": etag, "description": description } expected_response = instance_pb2.AppProfile(**expected_response) operation = operations_pb2.Operation( name="operations/test_update_app_profile", 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 = bigtable_admin_v2.BigtableInstanceAdminClient() # Setup Request app_profile = {} update_mask = {} response = client.update_app_profile(app_profile, update_mask) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.UpdateAppProfileRequest( app_profile=app_profile, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_app_profile(self): # Setup Expected Response name_2 = "name2-1052831874" etag = "etag3123477" description = "description-1724546052" expected_response = { "name": name_2, "etag": etag, "description": description } expected_response = instance_pb2.AppProfile(**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 = bigtable_admin_v2.BigtableInstanceAdminClient() # Setup Request name = client.app_profile_path("[PROJECT]", "[INSTANCE]", "[APP_PROFILE]") response = client.get_app_profile(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.GetAppProfileRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_app_profile(self): # Setup Expected Response name = "name3373707" etag = "etag3123477" description = "description-1724546052" expected_response = { "name": name, "etag": etag, "description": description } expected_response = instance_pb2.AppProfile(**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 = bigtable_admin_v2.BigtableInstanceAdminClient() # Setup Request parent = client.instance_path("[PROJECT]", "[INSTANCE]") app_profile_id = "appProfileId1262094415" app_profile = {} response = client.create_app_profile(parent, app_profile_id, app_profile) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.CreateAppProfileRequest( parent=parent, app_profile_id=app_profile_id, app_profile=app_profile) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_app_profile(self): # Setup Expected Response name_2 = 'name2-1052831874' etag = 'etag3123477' description = 'description-1724546052' expected_response = { 'name': name_2, 'etag': etag, 'description': description } expected_response = instance_pb2.AppProfile(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request name = client.app_profile_path('[PROJECT]', '[INSTANCE]', '[APP_PROFILE]') response = client.get_app_profile(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.GetAppProfileRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_app_profile(self): # Setup Expected Response name = 'name3373707' etag = 'etag3123477' description = 'description-1724546052' expected_response = { 'name': name, 'etag': etag, 'description': description } expected_response = instance_pb2.AppProfile(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request parent = client.instance_path('[PROJECT]', '[INSTANCE]') app_profile_id = 'appProfileId1262094415' app_profile = {} response = client.create_app_profile(parent, app_profile_id, app_profile) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.CreateAppProfileRequest( parent=parent, app_profile_id=app_profile_id, app_profile=app_profile) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_app_profile(self): from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as instance_data_v2_pb2) from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) name = 'name3373707' etag = 'etag3123477' description = 'description-1724546052' expected_response = { 'name': name, 'etag': etag, 'description': description } expected_response = instance_data_v2_pb2.AppProfile( **expected_response) response_pb = instance_data_v2_pb2.AppProfile(name=name, etag=etag, description=description) # Patch the stub used by the API method. client._instance_admin_client = instance_api bigtable_instance_stub = ( client._instance_admin_client.bigtable_instance_admin_stub) bigtable_instance_stub.GetAppProfile.side_effect = [response_pb] # Perform the method and check the result. app_profile_id = 'appProfileId1262094415' result = instance.get_app_profile(app_profile_id=app_profile_id) self.assertEqual(result, expected_response)
def test_create_app_profile_with_single_routing_policy(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client, self.LOCATION_ID) description = 'description-1724546052' app_profile_id = 'appProfileId1262094415' cluster_id = 'cluster-id' expected_response = { 'name': self.APP_PROFILE_PATH + app_profile_id, 'description': description, 'single_cluster_routing': instance_pb2.AppProfile.SingleClusterRouting( cluster_id=cluster_id, allow_transactional_writes=False) } expected_request = { 'app_profile_id': app_profile_id, 'routing_policy_type': 2, 'description': description, 'cluster_id': cluster_id } expected_response = instance_pb2.AppProfile(**expected_response) channel = ChannelStub(responses=[expected_response]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) # Patch the stub used by the API method. client._instance_admin_client = instance_api # Perform the method and check the result. result = instance.create_app_profile(**expected_request) parent = client._instance_admin_client.instance_path( self.PROJECT, self.INSTANCE_ID) expected_request = _CreateAppProfileRequestPB( parent=parent, app_profile_id=app_profile_id, app_profile=expected_response, ) actual_request = channel.requests[0][1] assert expected_request == actual_request self.assertEqual(result, expected_response)
def test_update_app_profile_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_update_app_profile_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request app_profile = instance_pb2.AppProfile() update_mask = {} response = client.update_app_profile(app_profile, update_mask) exception = response.exception() assert exception.errors[0] == error