def test_list_traces(self):
        # Setup Expected Response
        next_page_token = ''
        traces_element = {}
        traces = [traces_element]
        expected_response = {
            'next_page_token': next_page_token,
            'traces': traces
        }
        expected_response = trace_pb2.ListTracesResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = trace_v1.TraceServiceClient(channel=channel)

        # Setup Request
        project_id = 'projectId-1969970175'

        paged_list_response = client.list_traces(project_id)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.traces[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = trace_pb2.ListTracesRequest(project_id=project_id)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_traces(self):
        # Setup Expected Response
        next_page_token = ""
        traces_element = {}
        traces = [traces_element]
        expected_response = {
            "next_page_token": next_page_token,
            "traces": traces
        }
        expected_response = trace_pb2.ListTracesResponse(**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 = trace_v1.TraceServiceClient()

        # Setup Request
        project_id = "projectId-1969970175"

        paged_list_response = client.list_traces(project_id)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.traces[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = trace_pb2.ListTracesRequest(project_id=project_id)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_traces(self):
        from google.cloud._helpers import _rfc3339_to_datetime
        from google.cloud._helpers import UTC
        from google.cloud.trace_v1.gapic import trace_service_client
        from google.cloud.trace_v1.gapic.enums import ListTracesRequest as Enum
        from google.cloud.trace_v1.proto import trace_pb2

        trace_id = "test_trace_id"
        span_id = 1234
        span_name = "test_span_name"
        span_kind = "RPC_CLIENT"
        parent_span_id = 123
        start_ts = datetime.datetime.utcnow()
        end_ts = datetime.datetime.utcnow()
        labels = {
            "/http/status_code": "200",
            "/component": "HTTP load balancer"
        }
        size = 10
        view_type = Enum.ViewType.COMPLETE
        token = "TOKEN"

        trace_pb = self._make_trace_pb(
            self.project,
            trace_id,
            span_id,
            span_name,
            start_ts.isoformat() + "Z",
            end_ts.isoformat() + "Z",
            parent_span_id,
            labels,
        )

        gapic_api = mock.Mock(spec=trace_service_client.TraceServiceClient)
        gapic_api.list_traces = mock.create_autospec(gapic_api.list_traces)
        channel, api = self._make_one()

        channel.ListTraces.response = trace_pb2.ListTracesResponse(
            traces=[trace_pb[0]])
        iterator = api.list_traces(project_id=self.project,
                                   view=view_type,
                                   page_size=size,
                                   page_token=token)

        traces = list(iterator)

        self.assertEqual(len(traces), 1)
        trace = traces[0]

        self.assertEqual(len(trace["spans"]), 1)
        span = trace["spans"][0]

        self.assertEqual(trace["projectId"], self.project)
        self.assertEqual(trace["traceId"], trace_id)

        self.assertEqual(span["spanId"], str(span_id))
        self.assertEqual(span["name"], span_name)

        self.assertEqual(_rfc3339_to_datetime(span["startTime"]),
                         start_ts.replace(tzinfo=UTC))
        self.assertEqual(_rfc3339_to_datetime(span["endTime"]),
                         end_ts.replace(tzinfo=UTC))
        self.assertEqual(span["kind"], span_kind)
        self.assertEqual(span["parentSpanId"], str(parent_span_id))
        self.assertEqual(span["labels"], labels)

        self.assertEqual(len(channel.ListTraces.requests), 1)
        request = channel.ListTraces.requests[0]

        self.assertEqual(request.project_id, self.project)
        self.assertEqual(request.view, view_type)
        self.assertEqual(request.page_size, size)
        self.assertEqual(request.start_time.ToDatetime(),
                         datetime.datetime(1970, 1, 1, 0, 0))
        self.assertEqual(request.end_time.ToDatetime(),
                         datetime.datetime(1970, 1, 1, 0, 0))
        self.assertEqual(request.filter, "")
        self.assertEqual(request.order_by, "")
    def test_list_traces(self):
        from google.api_core.page_iterator import GRPCIterator
        from google.cloud._helpers import _rfc3339_to_datetime
        from google.cloud._helpers import UTC
        from google.cloud.trace_v1.gapic import trace_service_client
        from google.cloud.trace_v1.gapic.enums import ListTracesRequest as Enum
        from google.cloud.trace_v1.proto import trace_pb2

        trace_id = 'test_trace_id'
        span_id = 1234
        span_name = 'test_span_name'
        span_kind = 'RPC_CLIENT'
        parent_span_id = 123
        start_ts = datetime.datetime.utcnow()
        end_ts = datetime.datetime.utcnow()
        labels = {
            '/http/status_code': '200',
            '/component': 'HTTP load balancer',
        }
        size = 10
        view_type = Enum.ViewType.COMPLETE
        token = 'TOKEN'

        trace_pb = self._make_trace_pb(self.project, trace_id, span_id,
                                       span_name,
                                       start_ts.isoformat() + 'Z',
                                       end_ts.isoformat() + 'Z',
                                       parent_span_id, labels)

        gapic_api = mock.Mock(spec=trace_service_client.TraceServiceClient)
        gapic_api.list_traces = mock.create_autospec(gapic_api.list_traces)
        channel, api = self._make_one()

        channel.ListTraces.response = trace_pb2.ListTracesResponse(
            traces=[trace_pb[0]])
        iterator = api.list_traces(project_id=self.project,
                                   view=view_type,
                                   page_size=size,
                                   page_token=token)

        traces = list(iterator)

        self.assertEqual(len(traces), 1)
        trace = traces[0]

        self.assertEqual(len(trace['spans']), 1)
        span = trace['spans'][0]

        self.assertEqual(trace['projectId'], self.project)
        self.assertEqual(trace['traceId'], trace_id)

        self.assertEqual(span['spanId'], str(span_id))
        self.assertEqual(span['name'], span_name)

        self.assertEqual(_rfc3339_to_datetime(span['startTime']),
                         start_ts.replace(tzinfo=UTC))
        self.assertEqual(_rfc3339_to_datetime(span['endTime']),
                         end_ts.replace(tzinfo=UTC))
        self.assertEqual(span['kind'], span_kind)
        self.assertEqual(span['parentSpanId'], str(parent_span_id))
        self.assertEqual(span['labels'], labels)

        self.assertEqual(len(channel.ListTraces.requests), 1)
        request = channel.ListTraces.requests[0]

        self.assertEqual(request.project_id, self.project)
        self.assertEqual(request.view, view_type)
        self.assertEqual(request.page_size, size)
        self.assertEqual(request.start_time.ToDatetime(),
                         datetime.datetime(1970, 1, 1, 0, 0))
        self.assertEqual(request.end_time.ToDatetime(),
                         datetime.datetime(1970, 1, 1, 0, 0))
        self.assertEqual(request.filter, '')
        self.assertEqual(request.order_by, '')