def test_get_log_metric(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = metrics_service_v2_client.MetricsServiceV2Client() # Mock request metric_name = client.metric_path('[PROJECT]', '[METRIC]') # Mock response name = 'name3373707' description = 'description-1724546052' filter_ = 'filter-1274492040' expected_response = logging_metrics_pb2.LogMetric( name=name, description=description, filter=filter_) grpc_stub.GetLogMetric.return_value = expected_response response = client.get_log_metric(metric_name) self.assertEqual(expected_response, response) grpc_stub.GetLogMetric.assert_called_once() args, kwargs = grpc_stub.GetLogMetric.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = logging_metrics_pb2.GetLogMetricRequest( metric_name=metric_name) self.assertEqual(expected_request, actual_request)
def test_list_log_metrics(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = metrics_service_v2_client.MetricsServiceV2Client() # Mock request parent = client.project_path('[PROJECT]') # Mock response next_page_token = '' metrics_element = logging_metrics_pb2.LogMetric() metrics = [metrics_element] expected_response = logging_metrics_pb2.ListLogMetricsResponse( next_page_token=next_page_token, metrics=metrics) grpc_stub.ListLogMetrics.return_value = expected_response paged_list_response = client.list_log_metrics(parent) resources = list(paged_list_response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response.metrics[0], resources[0]) grpc_stub.ListLogMetrics.assert_called_once() args, kwargs = grpc_stub.ListLogMetrics.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = logging_metrics_pb2.ListLogMetricsRequest( parent=parent) self.assertEqual(expected_request, actual_request)
def test_get_log_metric_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = metrics_service_v2_client.MetricsServiceV2Client() # Mock request metric_name = client.metric_path('[PROJECT]', '[METRIC]') # Mock exception response grpc_stub.GetLogMetric.side_effect = CustomException() self.assertRaises(errors.GaxError, client.get_log_metric, metric_name)
def test_list_log_metrics_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = metrics_service_v2_client.MetricsServiceV2Client() # Mock request parent = client.project_path('[PROJECT]') # Mock exception response grpc_stub.ListLogMetrics.side_effect = CustomException() paged_list_response = client.list_log_metrics(parent) self.assertRaises(errors.GaxError, list, paged_list_response)
def test_create_log_metric_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = metrics_service_v2_client.MetricsServiceV2Client() # Mock request parent = client.project_path('[PROJECT]') metric = logging_metrics_pb2.LogMetric() # Mock exception response grpc_stub.CreateLogMetric.side_effect = CustomException() self.assertRaises(errors.GaxError, client.create_log_metric, parent, metric)
def test_delete_log_metric(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = metrics_service_v2_client.MetricsServiceV2Client() # Mock request metric_name = client.metric_path('[PROJECT]', '[METRIC]') client.delete_log_metric(metric_name) grpc_stub.DeleteLogMetric.assert_called_once() args, kwargs = grpc_stub.DeleteLogMetric.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = logging_metrics_pb2.DeleteLogMetricRequest( metric_name=metric_name) self.assertEqual(expected_request, actual_request)