def test_chat(self): # Setup Expected Response content = 'content951530617' expected_response = {'content': content} expected_response = echo_pb2.EchoResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[iter([expected_response])]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = showcase_v1beta1.EchoClient() # Setup Request request = {} request = echo_pb2.EchoRequest(**request) requests = [request] response = client.chat(requests) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 actual_requests = channel.requests[0][1] assert len(actual_requests) == 1 actual_request = list(actual_requests)[0] assert request == actual_request
def test_paged_expand(self): # Setup Expected Response next_page_token = '' responses_element = {} responses = [responses_element] expected_response = { 'next_page_token': next_page_token, 'responses': responses } expected_response = echo_pb2.PagedExpandResponse(**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 = showcase_v1beta1.EchoClient() paged_list_response = client.paged_expand() resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.responses[0] == resources[0] assert len(channel.requests) == 1 expected_request = echo_pb2.PagedExpandRequest() actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_block_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = showcase_v1beta1.EchoClient() with pytest.raises(CustomException): client.block()
def test_paged_expand_exception(self): channel = ChannelStub(responses=[CustomException()]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = showcase_v1beta1.EchoClient() paged_list_response = client.paged_expand() with pytest.raises(CustomException): list(paged_list_response)
def test_chat_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = showcase_v1beta1.EchoClient() # Setup request request = {} request = echo_pb2.EchoRequest(**request) requests = [request] with pytest.raises(CustomException): client.chat(requests)
class TestEchoClient(object): channel = grpc.insecure_channel('localhost:7469') transport = echo_grpc_transport.EchoGrpcTransport(channel=channel) client = showcase_v1beta1.EchoClient(transport=transport) def test_echo(self): content = 'hello world' response = self.client.echo(content=content) assert content == response.content def test_echo_exception(self): with pytest.raises(exceptions.InternalServerError): self.client.echo( error=status_pb2.Status(code=13, message='error!!!')) def test_expand(self): content = 'The rain in Spain stays mainly on the Plain!' response = [] for r in self.client.expand(content=content): response.append(r.content) assert content == ' '.join(response) def test_collect(self): content = 'The rain in Spain stays mainly on the Plain!' requests = content.split(' ') requests = map(lambda s: echo_pb2.EchoRequest(content=s), requests) response = self.client.collect(iter(requests)) assert content == response.content def test_chat(self): content = 'The rain in Spain stays mainly on the Plain!' requests = content.split(' ') requests = map(lambda s: echo_pb2.EchoRequest(content=s), requests) responses = self.client.chat(iter(requests)) responses = map(lambda r: r.content, responses) assert content == ' '.join(responses) def test_wait(self): content = 'hello world' response = self.client.wait( ttl={'nanos': 500}, success={'content': content}, ) assert content == response.result().content
def test_wait_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_wait_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 = showcase_v1beta1.EchoClient() response = client.wait() exception = response.exception() assert exception.errors[0] == error
def test_echo(self): # Setup Expected Response content = 'content951530617' expected_response = {'content': content} expected_response = echo_pb2.EchoResponse(**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 = showcase_v1beta1.EchoClient() response = client.echo() assert expected_response == response assert len(channel.requests) == 1 expected_request = echo_pb2.EchoRequest() actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_wait(self): # Setup Expected Response content = 'content951530617' expected_response = {'content': content} expected_response = echo_pb2.WaitResponse(**expected_response) operation = operations_pb2.Operation(name='operations/test_wait', 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 = showcase_v1beta1.EchoClient() response = client.wait() result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = echo_pb2.WaitRequest() actual_request = channel.requests[0][1] assert expected_request == actual_request