def test_get_recommendation(transport: str = 'grpc'):
    client = RewardRecommendationClient(
        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 = reward_recommendation.RewardRecommendationRequest()

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

        response = client.get_recommendation(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,
                      reward_recommendation.RewardRecommendationResponse)
    assert response.reward_ids == ['reward_ids_value']
def test_reward_recommendation_client_from_service_account_file():
    creds = credentials.AnonymousCredentials()
    with mock.patch.object(service_account.Credentials,
                           'from_service_account_file') as factory:
        factory.return_value = creds
        client = RewardRecommendationClient.from_service_account_file(
            "dummy/file/path.json")
        assert client._transport._credentials == creds

        client = RewardRecommendationClient.from_service_account_json(
            "dummy/file/path.json")
        assert client._transport._credentials == creds

        assert client._transport._host == 'ml-api.data.perxtech.net:443'
def test_reward_recommendation_host_with_port():
    client = RewardRecommendationClient(
        credentials=credentials.AnonymousCredentials(),
        client_options=client_options.ClientOptions(
            api_endpoint='ml-api.data.perxtech.net:8000'),
        transport='grpc',
    )
    assert client._transport._host == 'ml-api.data.perxtech.net:8000'
def test_transport_grpc_default():
    # A client should use the gRPC transport by default.
    client = RewardRecommendationClient(
        credentials=credentials.AnonymousCredentials(), )
    assert isinstance(
        client._transport,
        transports.RewardRecommendationGrpcTransport,
    )
def test_reward_recommendation_client_client_options_from_dict():
    with mock.patch(
            'perxtech.ml_v1beta1.services.reward_recommendation.RewardRecommendationClient.get_transport_class'
    ) as gtc:
        transport = gtc.return_value = mock.MagicMock()
        client = RewardRecommendationClient(
            client_options={'api_endpoint': 'squid.clam.whelk'})
        transport.assert_called_once_with(credentials=None,
                                          host="squid.clam.whelk")
def test_credentials_transport_error():
    # It is an error to provide credentials and a transport instance.
    transport = transports.RewardRecommendationGrpcTransport(
        credentials=credentials.AnonymousCredentials(), )
    with pytest.raises(ValueError):
        client = RewardRecommendationClient(
            credentials=credentials.AnonymousCredentials(),
            transport=transport,
        )
def test_reward_recommendation_client_client_options():
    # Check the default options have their expected values.
    assert RewardRecommendationClient.DEFAULT_OPTIONS.api_endpoint == 'ml-api.data.perxtech.net'

    # Check that options can be customized.
    options = client_options.ClientOptions(api_endpoint="squid.clam.whelk")
    with mock.patch(
            'perxtech.ml_v1beta1.services.reward_recommendation.RewardRecommendationClient.get_transport_class'
    ) as gtc:
        transport = gtc.return_value = mock.MagicMock()
        client = RewardRecommendationClient(client_options=options)
        transport.assert_called_once_with(credentials=None,
                                          host="squid.clam.whelk")
def test_reward_recommendation_auth_adc():
    # If no credentials are provided, we should use ADC credentials.
    with mock.patch.object(auth, 'default') as adc:
        adc.return_value = (credentials.AnonymousCredentials(), None)
        RewardRecommendationClient()
        adc.assert_called_once_with(scopes=())
def test_transport_instance():
    # A client may be instantiated with a custom transport instance.
    transport = transports.RewardRecommendationGrpcTransport(
        credentials=credentials.AnonymousCredentials(), )
    client = RewardRecommendationClient(transport=transport)
    assert client._transport is transport