def test_list_traces_pages(): client = TraceServiceClient(credentials=credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.list_traces), "__call__") as call: # Set the response to a series of pages. call.side_effect = ( trace.ListTracesResponse( traces=[ trace.Trace(), trace.Trace(), trace.Trace(), ], next_page_token="abc", ), trace.ListTracesResponse( traces=[], next_page_token="def", ), trace.ListTracesResponse( traces=[ trace.Trace(), ], next_page_token="ghi", ), trace.ListTracesResponse(traces=[ trace.Trace(), trace.Trace(), ], ), RuntimeError, ) pages = list(client.list_traces(request={}).pages) for page_, token in zip(pages, ["abc", "def", "ghi", ""]): assert page_.raw_page.next_page_token == token
async def test_list_traces_async(transport: str = "grpc_asyncio"): client = TraceServiceAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = trace.ListTracesRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._client._transport.list_traces), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( trace.ListTracesResponse( next_page_token="next_page_token_value", )) response = await client.list_traces(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] assert args[0] == request # Establish that the response is the type that we expect. assert isinstance(response, pagers.ListTracesAsyncPager) assert response.next_page_token == "next_page_token_value"
async def test_list_traces_async_pager(): client = TraceServiceAsyncClient( credentials=credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( type(client._client._transport.list_traces), "__call__", new_callable=mock.AsyncMock, ) as call: # Set the response to a series of pages. call.side_effect = ( trace.ListTracesResponse( traces=[ trace.Trace(), trace.Trace(), trace.Trace(), ], next_page_token="abc", ), trace.ListTracesResponse( traces=[], next_page_token="def", ), trace.ListTracesResponse( traces=[ trace.Trace(), ], next_page_token="ghi", ), trace.ListTracesResponse(traces=[ trace.Trace(), trace.Trace(), ], ), RuntimeError, ) async_pager = await client.list_traces(request={}, ) assert async_pager.next_page_token == "abc" responses = [] async for response in async_pager: responses.append(response) assert len(responses) == 6 assert all(isinstance(i, trace.Trace) for i in responses)
def test_list_traces_pager(): client = TraceServiceClient(credentials=credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.list_traces), "__call__") as call: # Set the response to a series of pages. call.side_effect = ( trace.ListTracesResponse( traces=[ trace.Trace(), trace.Trace(), trace.Trace(), ], next_page_token="abc", ), trace.ListTracesResponse( traces=[], next_page_token="def", ), trace.ListTracesResponse( traces=[ trace.Trace(), ], next_page_token="ghi", ), trace.ListTracesResponse(traces=[ trace.Trace(), trace.Trace(), ], ), RuntimeError, ) metadata = () pager = client.list_traces(request={}) assert pager._metadata == metadata results = [i for i in pager] assert len(results) == 6 assert all(isinstance(i, trace.Trace) for i in results)
async def test_list_traces_flattened_async(): client = TraceServiceAsyncClient( credentials=credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._client._transport.list_traces), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = trace.ListTracesResponse() call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( trace.ListTracesResponse()) # Call the method with a truthy value for each flattened field, # using the keyword arguments to the method. response = await client.list_traces(project_id="project_id_value", ) # Establish that the underlying call was made with the expected # request object values. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] assert args[0].project_id == "project_id_value"