def test_get_session(transport: str = 'grpc'):
    client = TestingClient(
        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 = testing.GetSessionRequest()

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client._transport.get_session),
                           '__call__') as call:
        # Designate an appropriate return value for the call.
        call.return_value = testing.Session(
            name='name_value',
            version=testing.Session.Version.V1_LATEST,
        )

        response = client.get_session(request)

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

        assert args[0] == request

    # Establish that the response is the type that we expect.
    assert isinstance(response, testing.Session)
    assert response.name == 'name_value'
    assert response.version == testing.Session.Version.V1_LATEST
Ejemplo n.º 2
0
def test_get_session_field_headers():
    client = TestingClient(
        credentials=credentials.AnonymousCredentials(),
  )

    # Any value that is part of the HTTP/1.1 URI should be sent as
    # a field header. Set these to a non-empty value.
    request = testing.GetSessionRequest(
        name='name/value',
    )

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(
            type(client._transport.get_session),
            '__call__') as call:
        call.return_value = testing.Session()
        response = client.get_session(request)

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

    # Establish that the field header was sent.
    _, _, kw = call.mock_calls[0]
    assert (
        'x-goog-request-params',
        'name=name/value',
    ) in kw['metadata']
Ejemplo n.º 3
0
    def get_session(
            self,
            request: testing.GetSessionRequest = None,
            *,
            retry: retries.Retry = gapic_v1.method.DEFAULT,
            timeout: float = None,
            metadata: Sequence[Tuple[str, str]] = (),
    ) -> testing.Session:
        r"""Gets a testing session.

        Args:
            request (:class:`~.testing.GetSessionRequest`):
                The request object. The request for the GetSession
                method.

            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:
            ~.testing.Session:
                A session is a suite of tests,
                generally being made in the context of
                testing code generation.
                A session defines tests it may expect,
                based on which version of the code
                generation spec is in use.

        """
        # Create or coerce a protobuf request object.
        request = testing.GetSessionRequest(request)

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

        # Certain fields should be provided within the metadata header;
        # add these here.
        metadata = tuple(metadata) + (gapic_v1.routing_header.to_grpc_metadata(
            (('name', request.name), )), )

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

        # Done; return the response.
        return response