Beispiel #1
0
def test_handle_validate_and_initialize_user_error(env, validate_user_module):
    test_status_code = http_client.FORBIDDEN
    test_error_message = "Foo"

    class FooUserError(BaseInferenceToolkitError):
        def __init__(self, status_code, message):
            self.status_code = status_code
            self.message = message
            self.phrase = "Foo"

    data = [{"body": INPUT_DATA}]
    context = Mock()
    transform_fn = Mock()
    model_fn = Mock()

    transformer = Transformer()

    transformer._model = MODEL
    transformer._transform_fn = transform_fn
    transformer._model_fn = model_fn

    validate_user_module.side_effect = FooUserError(test_status_code,
                                                    test_error_message)

    assert transformer._initialized is False

    response = transformer.transform(data, context)
    assert test_error_message in str(response)
    context.set_response_status.assert_called_with(code=http_client.FORBIDDEN,
                                                   phrase=test_error_message)
Beispiel #2
0
def test_handle_validate_and_initialize_error(env, validate_user_module):
    data = [{"body": INPUT_DATA}]
    request_processor = Mock()

    context = Mock()
    context.request_processor = [request_processor]

    transform_fn = Mock()
    model_fn = Mock()

    transformer = Transformer()

    transformer._model = MODEL
    transformer._transform_fn = transform_fn
    transformer._model_fn = model_fn

    test_error_message = "Foo"
    validate_user_module.side_effect = ValueError(test_error_message)

    assert transformer._initialized is False

    response = transformer.transform(data, context)
    assert test_error_message in str(response)
    context.set_response_status.assert_called_with(
        code=http_client.INTERNAL_SERVER_ERROR, phrase=test_error_message)
def test_transform_no_accept(validate, retrieve_content_type_header):
    data = [{"body": INPUT_DATA}]
    context = Mock()
    request_processor = Mock()
    transform_fn = Mock()
    environment = Mock()
    environment.default_accept = DEFAULT_ACCEPT

    context.request_processor = [request_processor]
    request_processor.get_request_properties.return_value = dict()

    transformer = Transformer()
    transformer._model = MODEL
    transformer._transform_fn = transform_fn
    transformer._environment = environment

    transformer.transform(data, context)

    validate.assert_called_once_with()
    transform_fn.assert_called_once_with(MODEL, INPUT_DATA, CONTENT_TYPE, DEFAULT_ACCEPT)
def test_transform_decode(validate, retrieve_content_type_header, content_type):
    input_data = Mock()
    context = Mock()
    request_processor = Mock()
    transform_fn = Mock()
    data = [{"body": input_data}]

    input_data.decode.return_value = INPUT_DATA
    context.request_processor = [request_processor]
    request_processor.get_request_properties.return_value = {"accept": ACCEPT}
    retrieve_content_type_header.return_value = content_type

    transformer = Transformer()
    transformer._model = MODEL
    transformer._transform_fn = transform_fn

    transformer.transform(data, context)

    input_data.decode.assert_called_once_with("utf-8")
    transform_fn.assert_called_once_with(MODEL, INPUT_DATA, content_type, ACCEPT)
def test_transform_tuple(validate, retrieve_content_type_header):
    data = [{"body": INPUT_DATA}]
    context = Mock()
    request_processor = Mock()
    transform_fn = Mock(return_value=(RESULT, ACCEPT))

    context.request_processor = [request_processor]
    request_processor.get_request_properties.return_value = {"accept": ACCEPT}

    transformer = Transformer()
    transformer._model = MODEL
    transformer._transform_fn = transform_fn

    result = transformer.transform(data, context)

    transform_fn.assert_called_once_with(MODEL, INPUT_DATA, CONTENT_TYPE, ACCEPT)
    context.set_response_content_type.assert_called_once_with(0, transform_fn()[1])
    assert isinstance(result, list)
    assert result[0] == transform_fn()[0]
def test_transform(validate, retrieve_content_type_header, accept_key):
    data = [{"body": INPUT_DATA}]
    context = Mock()
    request_processor = Mock()
    transform_fn = Mock(return_value=RESULT)

    context.request_processor = [request_processor]
    request_property = {accept_key: ACCEPT}
    request_processor.get_request_properties.return_value = request_property

    transformer = Transformer()
    transformer._model = MODEL
    transformer._transform_fn = transform_fn

    result = transformer.transform(data, context)

    validate.assert_called_once_with()
    retrieve_content_type_header.assert_called_once_with(request_property)
    transform_fn.assert_called_once_with(MODEL, INPUT_DATA, CONTENT_TYPE, ACCEPT)
    context.set_response_content_type.assert_called_once_with(0, ACCEPT)
    assert isinstance(result, list)
    assert result[0] == RESULT