def test_get_trace(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = trace_service_client.TraceServiceClient() # Mock request project_id = 'projectId-1969970175' trace_id = 'traceId1270300245' # Mock response project_id_2 = 'projectId2939242356' trace_id_2 = 'traceId2987826376' expected_response = trace_pb2.Trace(project_id=project_id_2, trace_id=trace_id_2) grpc_stub.GetTrace.return_value = expected_response response = client.get_trace(project_id, trace_id) self.assertEqual(expected_response, response) grpc_stub.GetTrace.assert_called_once() args, kwargs = grpc_stub.GetTrace.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = trace_pb2.GetTraceRequest(project_id=project_id, trace_id=trace_id) self.assertEqual(expected_request, actual_request)
def test_list_traces(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = trace_service_client.TraceServiceClient() # Mock request project_id = 'projectId-1969970175' # Mock response next_page_token = '' traces_element = trace_pb2.Trace() traces = [traces_element] expected_response = trace_pb2.ListTracesResponse( next_page_token=next_page_token, traces=traces) grpc_stub.ListTraces.return_value = expected_response paged_list_response = client.list_traces(project_id) resources = list(paged_list_response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response.traces[0], resources[0]) grpc_stub.ListTraces.assert_called_once() args, kwargs = grpc_stub.ListTraces.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = trace_pb2.ListTracesRequest(project_id=project_id) self.assertEqual(expected_request, actual_request)
def make_gax_trace_api(client): """Create an instance of the GAX Trace API. :type client: :class:`~google.cloud.trace.client.Client` :param client: The client that holds configuration details. :rtype: :class:`~google.cloud.trace._gax._TraceAPI` :returns: A Trace API instance with the proper configurations. """ channel = make_secure_channel( client._credentials, DEFAULT_USER_AGENT, trace_service_client.TraceServiceClient.SERVICE_ADDRESS) generated = trace_service_client.TraceServiceClient(channel=channel, lib_name='gccl') return _TraceAPI(generated, client)
def test_list_traces_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = trace_service_client.TraceServiceClient() # Mock request project_id = 'projectId-1969970175' # Mock exception response grpc_stub.ListTraces.side_effect = CustomException() paged_list_response = client.list_traces(project_id) self.assertRaises(errors.GaxError, list, paged_list_response)
def test_patch_traces_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = trace_service_client.TraceServiceClient() # Mock request project_id = 'projectId-1969970175' traces = trace_pb2.Traces() # Mock exception response grpc_stub.PatchTraces.side_effect = CustomException() self.assertRaises(errors.GaxError, client.patch_traces, project_id, traces)
def test_get_trace_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = trace_service_client.TraceServiceClient() # Mock request project_id = 'projectId-1969970175' trace_id = 'traceId1270300245' # Mock exception response grpc_stub.GetTrace.side_effect = CustomException() self.assertRaises(errors.GaxError, client.get_trace, project_id, trace_id)
def test_patch_traces(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = trace_service_client.TraceServiceClient() # Mock request project_id = 'projectId-1969970175' traces = trace_pb2.Traces() client.patch_traces(project_id, traces) grpc_stub.PatchTraces.assert_called_once() args, kwargs = grpc_stub.PatchTraces.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = trace_pb2.PatchTracesRequest(project_id=project_id, traces=traces) self.assertEqual(expected_request, actual_request)