def test_chat(transport: str = 'grpc'):
    client = EchoClient(
        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 = gs_echo.EchoRequest()

    requests = [request]

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client._transport.chat), '__call__') as call:
        # Designate an appropriate return value for the call.
        call.return_value = iter([gs_echo.EchoResponse()])

        response = client.chat(iter(requests))

        # Establish that the underlying gRPC stub method was called.
        assert len(call.mock_calls) == 1
        _, args, _ = call.mock_calls[0]

        assert next(args[0]) == request

    # Establish that the response is the type that we expect.
    for message in response:
        assert isinstance(message, gs_echo.EchoResponse)
Example #2
0
def test_collect():
  print("=================test collect ===================")
  client = EchoClient(transport=transport)
  content = 'The rain in Spain stays mainly on the Plain!'
  requests = content.split(' ')
  requests = map(lambda s: gs_echo.EchoRequest(content=s), requests)
  response = client.collect(iter(requests))
  print(response)
Example #3
0
def test_chat():
  print("=================test chat ===================")
  client = EchoClient(transport=transport)
  content = 'The rain in Spain stays mainly on the Plain!'
  requests = content.split(' ')
  requests = map(lambda s: gs_echo.EchoRequest(content=s), requests)
  responses = client.chat(iter(requests))
  for res in responses:
    print(res)
  print("trailing metadata...")
  print(responses.trailing_metadata())
Example #4
0
    def collect(
            self,
            request: gs_echo.EchoRequest = None,
            *,
            retry: retries.Retry = gapic_v1.method.DEFAULT,
            timeout: float = None,
            metadata: Sequence[Tuple[str, str]] = (),
    ) -> gs_echo.EchoResponse:
        r"""This method will collect the words given to it. When
        the stream is closed by the client, this method will
        return the a concatenation of the strings passed to it.
        This method showcases client-side streaming rpcs.

        Args:
            request (:class:`~.gs_echo.EchoRequest`):
                The request object. The request message used for the
                Echo, Collect and Chat methods. If content is set in
                this message then the request will succeed. If status is
                set in  this message then the status will be returned as
                an error.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, str]]): Strings which should be
                sent along with the request as metadata.

        Returns:
            ~.gs_echo.EchoResponse:
                The response message for the Echo
                methods.

        """
        # Create or coerce a protobuf request object.
        request = gs_echo.EchoRequest(request)

        # Wrap the RPC method; this adds retry and timeout information,
        # and friendly error handling.
        rpc = gapic_v1.method.wrap_method(
            self._transport.collect,
            default_timeout=None,
            client_info=_client_info,
        )

        # Send the request.
        response = rpc(
            request,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )

        # Done; return the response.
        return response
Example #5
0
def run_should_pass():
    print("================= should pass ====================")
    header_adder_interceptor = client_interceptor.header_adder_interceptor(
        'one-time-password', '42')
    channel = grpc.insecure_channel('localhost:50051')
    intercept_channel = grpc.intercept_channel(channel,
                                               header_adder_interceptor)
    transport = transports.EchoGrpcTransport(channel=intercept_channel)
    client = EchoClient(transport=transport)

    content = 'The rain in Spain stays mainly on the Plain!'
    requests = content.split(' ')
    requests = map(lambda s: gs_echo.EchoRequest(content=s), requests)
    responses = client.chat(iter(requests))
    for res in responses:
        print(res)
    print("trailing metadata...")
    print(responses.trailing_metadata())
Example #6
0
def run_should_fail():
    print(
        "================= should fail with 'Access denied!' ===================="
    )
    channel = grpc.insecure_channel('localhost:50051')
    transport = transports.EchoGrpcTransport(channel=channel)
    client = EchoClient(transport=transport)

    content = 'The rain in Spain stays mainly on the Plain!'
    requests = content.split(' ')
    requests = map(lambda s: gs_echo.EchoRequest(content=s), requests)
    try:
        responses = client.chat(iter(requests))
        for res in responses:
            print(res)
        print("trailing metadata...")
        print(responses.trailing_metadata())
    except:
        print(sys.exc_info())
Example #7
0
def test_echo():
  print("=================test echo ===================")
  client = EchoClient(transport=transport)
  request = gs_echo.EchoRequest(content='hello world')
  response = client.echo(request)
  print(response)