def test_create_instance_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = instance_admin_client.InstanceAdminClient() # Mock request parent = client.project_path('[PROJECT]') instance_id = 'instanceId-2101995259' instance = spanner_instance_admin_pb2.Instance() # Mock exception response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_create_instance_exception', done=True) operation.error.CopyFrom(error) grpc_stub.CreateInstance.return_value = operation response = client.create_instance(parent, instance_id, instance) self.assertEqual(error, response.exception())
def test_batch_delete_entities_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_batch_delete_entities_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 = dialogflow_v2beta1.EntityTypesClient() # Setup Request parent = client.entity_type_path('[PROJECT]', '[ENTITY_TYPE]') entity_values = [] response = client.batch_delete_entities(parent, entity_values) exception = response.exception() assert exception.errors[0] == error
def test_create_instance_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_create_instance_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) client = redis_v1beta1.CloudRedisClient(channel=channel) # Setup Request parent = client.location_path('[PROJECT]', '[LOCATION]') instance_id = 'test_instance' tier = enums.Instance.Tier.BASIC memory_size_gb = 1 instance = {'tier': tier, 'memory_size_gb': memory_size_gb} response = client.create_instance(parent, instance_id, instance) exception = response.exception() assert exception.errors[0] == error
def test_call_info(self): channel = grpc_helpers.ChannelStub() stub = operations_pb2.OperationsStub(channel) expected_request = operations_pb2.GetOperationRequest(name="meep") expected_response = operations_pb2.Operation(name="moop") expected_metadata = [("red", "blue"), ("two", "shoe")] expected_credentials = mock.sentinel.credentials channel.GetOperation.response = expected_response response = stub.GetOperation( expected_request, timeout=42, metadata=expected_metadata, credentials=expected_credentials, ) assert response == expected_response assert channel.requests == [("GetOperation", expected_request)] assert channel.GetOperation.calls == [ (expected_request, 42, expected_metadata, expected_credentials) ]
def test_update_instance_flattened(): client = CloudMemcacheClient(credentials=credentials.AnonymousCredentials()) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.update_instance), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = operations_pb2.Operation(name="operations/op") # Call the method with a truthy value for each flattened field, # using the keyword arguments to the method. response = client.update_instance( update_mask=field_mask.FieldMask(paths=["paths_value"]), resource=cloud_memcache.Instance(name="name_value"), ) # Establish that the underlying call was made with the expected # request object values. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] assert args[0].update_mask == field_mask.FieldMask(paths=["paths_value"]) assert args[0].resource == cloud_memcache.Instance(name="name_value")
def test_purge_products_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_purge_products_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 = vision_v1.ProductSearchClient() # Setup Request parent = client.location_path("[PROJECT]", "[LOCATION]") response = client.purge_products(parent) exception = response.exception() assert exception.errors[0] == error
def test_update_instance_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_update_instance_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 = spanner_admin_instance_v1.InstanceAdminClient() # Setup Request instance = {} field_mask = {} response = client.update_instance(instance, field_mask) exception = response.exception() assert exception.errors[0] == error
def test_reload_document(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 = dialogflow_v2beta1.DocumentsClient() response = client.reload_document() assert expected_response == response assert len(channel.requests) == 1 expected_request = document_pb2.ReloadDocumentRequest() actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_run_asset_discovery_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_run_asset_discovery_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 = securitycenter_v1.SecurityCenterClient() # Setup Request parent = client.organization_path("[ORGANIZATION]") response = client.run_asset_discovery(parent) exception = response.exception() assert exception.errors[0] == error
def test_create_database_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_create_database_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 = spanner_admin_database_v1.DatabaseAdminClient() # Setup Request parent = client.instance_path('[PROJECT]', '[INSTANCE]') create_statement = 'createStatement552974828' response = client.create_database(parent, create_statement) exception = response.exception() assert exception.errors[0] == error
def test_update_document_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_update_document_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 = dialogflow_v2beta1.DocumentsClient() # Setup Request document = {} response = client.update_document(document) exception = response.exception() assert exception.errors[0] == error
def test_undeploy_model(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]) client = automl_v1beta1.AutoMlClient(channel=channel) # Setup Request name = client.model_path('[PROJECT]', '[LOCATION]', '[MODEL]') response = client.undeploy_model(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = service_pb2.UndeployModelRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_glossary_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_create_glossary_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 = translate_v3beta1.TranslationServiceClient() # Setup Request parent = client.location_path("[PROJECT]", "[LOCATION]") glossary = {} response = client.create_glossary(parent, glossary) exception = response.exception() assert exception.errors[0] == error
def test_annotate_video_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_annotate_video_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) client = videointelligence_v1p1beta1.VideoIntelligenceServiceClient( channel=channel) # Setup Request input_uri = 'gs://demomaker/cat.mp4' features_element = enums.Feature.LABEL_DETECTION features = [features_element] response = client.annotate_video( input_uri=input_uri, features=features) exception = response.exception() assert exception.errors[0] == error
def test_export_assets_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_export_assets_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 = asset_v1beta1.AssetServiceClient() # Setup Request parent = client.project_path("[PROJECT]") output_config = {} response = client.export_assets(parent, output_config) exception = response.exception() assert exception.errors[0] == error
def test_export_data_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_export_data_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 = automl_v1beta1.AutoMlClient() # Setup Request name = client.dataset_path("[PROJECT]", "[LOCATION]", "[DATASET]") output_config = {} response = client.export_data(name, output_config) exception = response.exception() assert exception.errors[0] == error
def test_delete_instance_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_delete_instance_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 = redis_v1.CloudRedisClient() # Setup Request name = client.instance_path("[PROJECT]", "[LOCATION]", "[INSTANCE]") response = client.delete_instance(name) exception = response.exception() assert exception.errors[0] == error
def test_create_instance_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_create_instance_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request parent = client.project_path('[PROJECT]') instance_id = 'instanceId-2101995259' instance = {} clusters = {} response = client.create_instance(parent, instance_id, instance, clusters) exception = response.exception() assert exception.errors[0] == error
def test_batch_update_jobs_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_batch_update_jobs_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 = talent_v4beta1.JobServiceClient() # Setup Request parent = client.tenant_path("[PROJECT]", "[TENANT]") jobs = [] response = client.batch_update_jobs(parent, jobs) exception = response.exception() assert exception.errors[0] == error
def test_async_batch_annotate_files_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_async_batch_annotate_files_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 = vision_v1p2beta1.ImageAnnotatorClient() # Setup Request requests = [] response = client.async_batch_annotate_files(requests) exception = response.exception() assert exception.errors[0] == error
def test_batch_delete_intents_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_batch_delete_intents_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 = dialogflow_v2beta1.IntentsClient() # Setup Request parent = client.project_agent_path("[PROJECT]") intents = [] response = client.batch_delete_intents(parent, intents) exception = response.exception() assert exception.errors[0] == error
def test_update_cluster_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_update_cluster_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 = bigtable_admin_v2.BigtableInstanceAdminClient() # Setup Request name = client.cluster_path('[PROJECT]', '[INSTANCE]', '[CLUSTER]') serve_nodes = 1288838783 response = client.update_cluster(name, serve_nodes) exception = response.exception() assert exception.errors[0] == error
def from_dict(cls, operation, client, **caller_metadata): """Factory: construct an instance from a dictionary. :type operation: dict :param operation: Operation as a JSON object. :type client: :class:`~google.cloud.client.Client` :param client: The client used to poll for the status of the operation. :type caller_metadata: dict :param caller_metadata: caller-assigned metadata about the operation :rtype: :class:`Operation` :returns: new instance, with attributes based on the protobuf. """ operation_pb = json_format.ParseDict(operation, operations_pb2.Operation()) result = cls(operation_pb.name, client, **caller_metadata) result._update_state(operation_pb) result._from_grpc = False return result
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]) 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) exception = response.exception() assert exception.errors[0] == error
def test_undeploy_model_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_undeploy_model_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 = automl_v1beta1.AutoMlClient() # Setup Request name = client.model_path("[PROJECT]", "[LOCATION]", "[MODEL]") response = client.undeploy_model(name) exception = response.exception() assert exception.errors[0] == error
def test_restore_database_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_restore_database_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 = spanner_admin_database_v1.DatabaseAdminClient() # Setup Request parent = client.instance_path("[PROJECT]", "[INSTANCE]") database_id = "databaseId816491103" response = client.restore_database(parent, database_id) exception = response.exception() assert exception.errors[0] == error
def test_export_instance_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_export_instance_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 = redis_v1.CloudRedisClient() # Setup Request name = "name3373707" output_config = {} response = client.export_instance(name, output_config) exception = response.exception() assert exception.errors[0] == error
def test_list_backup_operations_defaults(self): from google.api_core.operation import Operation from google.cloud.spanner_admin_database_v1.gapic import database_admin_client from google.cloud.spanner_admin_database_v1.proto import backup_pb2 from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any api = database_admin_client.DatabaseAdminClient(mock.Mock()) client = _Client(self.PROJECT) client.database_admin_api = api instance = self._make_one(self.INSTANCE_ID, client) create_backup_metadata = Any() create_backup_metadata.Pack(backup_pb2.CreateBackupMetadata()) operations_pb = backup_pb2.ListBackupOperationsResponse( operations=[ operations_pb2.Operation(name="op1", metadata=create_backup_metadata) ] ) ldo_api = api._inner_api_calls["list_backup_operations"] = mock.Mock( return_value=operations_pb ) operations = instance.list_backup_operations() for op in operations: self.assertIsInstance(op, Operation) expected_metadata = [ ("google-cloud-resource-prefix", instance.name), ("x-goog-request-params", "parent={}".format(instance.name)), ] ldo_api.assert_called_once_with( backup_pb2.ListBackupOperationsRequest(parent=self.INSTANCE_NAME), metadata=expected_metadata, retry=mock.ANY, timeout=mock.ANY, )
def test_update_cluster(self): # Setup Expected Response project_id_2 = 'projectId2939242356' cluster_name_2 = 'clusterName2875867491' cluster_uuid = 'clusterUuid-1017854240' expected_response = { 'project_id': project_id_2, 'cluster_name': cluster_name_2, 'cluster_uuid': cluster_uuid } expected_response = clusters_pb2.Cluster(**expected_response) operation = operations_pb2.Operation( name='operations/test_update_cluster', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = dataproc_v1beta2.ClusterControllerClient(channel=channel) # Setup Request project_id = 'projectId-1969970175' region = 'region-934795532' cluster_name = 'clusterName-1018081872' cluster = {} update_mask = {} response = client.update_cluster(project_id, region, cluster_name, cluster, update_mask) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = clusters_pb2.UpdateClusterRequest( project_id=project_id, region=region, cluster_name=cluster_name, cluster=cluster, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_instance(self): # Setup Expected Response name = "name3373707" config = "config-1354792126" display_name = "displayName1615086568" node_count = 1539922066 expected_response = { "name": name, "config": config, "display_name": display_name, "node_count": node_count, } expected_response = spanner_instance_admin_pb2.Instance(**expected_response) operation = operations_pb2.Operation( name="operations/test_create_instance", 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 = spanner_admin_instance_v1.InstanceAdminClient() # Setup Request parent = client.project_path("[PROJECT]") instance_id = "instanceId-2101995259" instance = {} response = client.create_instance(parent, instance_id, instance) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = spanner_instance_admin_pb2.CreateInstanceRequest( parent=parent, instance_id=instance_id, instance=instance ) actual_request = channel.requests[0][1] assert expected_request == actual_request