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_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_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)
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 echo(self, content=None, error=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None): """ This method simply echos the request. This method is showcases unary rpcs. Example: >>> from google import showcase_v1beta1 >>> >>> client = showcase_v1beta1.EchoClient() >>> >>> response = client.echo() Args: content (str): The content to be echoed by the server. error (Union[dict, ~google.showcase_v1beta1.types.Status]): The error to be thrown by the server. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.showcase_v1beta1.types.Status` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will be retried using a default configuration. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.showcase_v1beta1.types.EchoResponse` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if 'echo' not in self._inner_api_calls: self._inner_api_calls[ 'echo'] = google.api_core.gapic_v1.method.wrap_method( self.transport.echo, default_retry=self._method_configs['Echo'].retry, default_timeout=self._method_configs['Echo'].timeout, client_info=self._client_info, ) # Sanity check: We have some fields which are mutually exclusive; # raise ValueError if more than one is sent. google.api_core.protobuf_helpers.check_oneof( content=content, error=error, ) request = echo_pb2.EchoRequest( content=content, error=error, ) return self._inner_api_calls['echo'](request, retry=retry, timeout=timeout, metadata=metadata)