def test_json_handle_aws_lambda_event():
    test_content = '[{"name": "john","game": "mario","city": "sf"}]'

    def test_func(obj):
        return obj[0]['name']

    handler = JsonHandler()
    success_event_obj = {
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': test_content
    }
    success_response = handler.handle_aws_lambda_event(success_event_obj,
                                                       test_func)

    assert success_response['statusCode'] == 200
    assert success_response['body'] == '"john"'

    error_event_obj = {
        'headers': {
            'Content-Type': 'this_will_fail'
        },
        'body': test_content
    }
    error_response = handler.handle_aws_lambda_event(error_event_obj,
                                                     test_func)
    assert error_response['statusCode'] == 400
Exemple #2
0
def test_json_handle_aws_lambda_event():
    test_content = '[{"name": "john","game": "mario","city": "sf"}]'

    def test_func(obj):
        return obj[0]["name"]

    handler = JsonHandler()
    success_event_obj = {
        "headers": {
            "Content-Type": "application/json"
        },
        "body": test_content,
    }
    success_response = handler.handle_aws_lambda_event(success_event_obj,
                                                       test_func)

    assert success_response["statusCode"] == 200
    assert success_response["body"] == '"john"'

    error_event_obj = {
        "headers": {
            "Content-Type": "this_will_fail"
        },
        "body": test_content,
    }
    error_response = handler.handle_aws_lambda_event(error_event_obj,
                                                     test_func)
    assert error_response["statusCode"] == 400
def test_json_handle_cli(capsys, tmpdir):
    def test_func(obj):
        return obj[0]["name"]

    handler = JsonHandler()

    json_file = tmpdir.join("test.json")
    with open(str(json_file), "w") as f:
        f.write('[{"name": "john","game": "mario","city": "sf"}]')

    test_args = ["--input={}".format(json_file)]
    handler.handle_cli(test_args, test_func)
    out, err = capsys.readouterr()
    assert out.strip().endswith("john")
def test_json_handle_cli(capsys, tmpdir):
    def test_func(obj):
        return obj[0]['name']

    handler = JsonHandler()

    import json
    json_file = tmpdir.join('test.json')
    with open(str(json_file), 'w') as f:
        f.write('[{"name": "john","game": "mario","city": "sf"}]')

    test_args = ['--input={}'.format(json_file)]
    handler.handle_cli(test_args, test_func)
    out, err = capsys.readouterr()
    assert out.strip().endswith('john')
class ExampleBentoService(bentoml.BentoService):
    """
    Example BentoService class made for testing purpose
    """
    @bentoml.api(input=DataframeHandler())
    def predict(self, df):
        """An API for testing simple bento model service
        """
        return self.artifacts.model.predict(df)

    @bentoml.api(input=DataframeHandler(input_dtypes={"col1": "int"}))
    def predict_dataframe(self, df):
        """predict_dataframe expects dataframe as input
        """
        return self.artifacts.model.predict_dataframe(df)

    @bentoml.api(DataframeHandler, input_dtypes={"col1": "int"})
    def predict_dataframe_v1(self, df):
        """predict_dataframe expects dataframe as input
        """
        return self.artifacts.model.predict_dataframe(df)

    @bentoml.api(input=ImageHandler())
    def predict_image(self, images):
        return self.artifacts.model.predict_image(images)

    @bentoml.api(input=LegacyImageHandler(input_names=('original', 'compared'))
                 )
    def predict_images(self, original, compared):
        return original[0, 0] == compared[0, 0]

    @bentoml.api(input=JsonHandler())
    def predict_json(self, input_data):
        return self.artifacts.model.predict_json(input_data)
def test_json_handle_aws_lambda_event():
    test_content = '[{"name": "john","game": "mario","city": "sf"}]'

    def test_func(obj):
        return obj[0]["name"]

    handler = JsonHandler()
    success_event_obj = {
        "headers": {"Content-Type": "application/json"},
        "body": test_content,
    }
    success_response = handler.handle_aws_lambda_event(success_event_obj, test_func)

    assert success_response["statusCode"] == 200
    assert success_response["body"] == '"john"'

    error_event_obj = {
        "headers": {"Content-Type": "this_will_fail"},
        "body": test_content,
    }
    with pytest.raises(BadInput) as e:
        handler.handle_aws_lambda_event(error_event_obj, test_func)

    assert "Request content-type must be 'application/json" in str(e.value)