コード例 #1
0
def test_dataframe_handle_aws_lambda_event():
    test_content = '[{"name": "john","game": "mario","city": "sf"}]'

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

    handler = DataframeHandler()
    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
コード例 #2
0
def test_dataframe_handle_aws_lambda_event():
    test_content = '[{"name": "john","game": "mario","city": "sf"}]'

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

    handler = DataframeHandler()
    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"'

    with pytest.raises(BadInput):
        error_event_obj = {
            "headers": {
                "Content-Type": "this_will_fail"
            },
            "body": test_content,
        }
        handler.handle_aws_lambda_event(error_event_obj, test_func)
コード例 #3
0
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)
コード例 #4
0
def test_dataframe_handle_request_csv():
    def test_function(df):
        return df["name"][0]

    handler = DataframeHandler()
    csv_data = 'name,game,city\njohn,mario,sf'.encode('utf-8')
    request = Mock()
    request.headers = {'output_orient': 'records', 'orient': 'records'}
    request.content_type = 'text/csv'
    request.data = csv_data

    result = handler.handle_request(request, test_function)
    assert result.get_data().decode('utf-8') == '"john"'
コード例 #5
0
def test_dataframe_handle_aws_lambda_event():
    test_content = '[{"name": "john","game": "mario","city": "sf"}]'

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

    handler = DataframeHandler()
    event = {
        "headers": {
            "Content-Type": "application/json"
        },
        "body": test_content,
    }
    response = handler.handle_aws_lambda_event(event, test_func)
    assert response["statusCode"] == 200
    assert response["body"] == '"john"'

    handler = DataframeHandler()
    event_without_content_type_header = {
        "headers": {},
        "body": test_content,
    }
    response = handler.handle_aws_lambda_event(
        event_without_content_type_header, test_func)
    assert response["statusCode"] == 200
    assert response["body"] == '"john"'

    with pytest.raises(BadInput):
        event_with_bad_input = {
            "headers": {},
            "body": "bad_input_content",
        }
        handler.handle_aws_lambda_event(event_with_bad_input, test_func)
コード例 #6
0
def test_dataframe_handle_cli(capsys, tmpdir):
    def test_func(df):
        return df["name"][0]

    handler = DataframeHandler()

    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")
コード例 #7
0
def test_dataframe_handle_cli(capsys, tmpdir):
    def test_func(df):
        return df['name'][0]

    handler = DataframeHandler()

    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')
コード例 #8
0
def test_dataframe_request_schema():
    handler = DataframeHandler(
        input_dtypes={"col1": "int", "col2": "float", "col3": "string"}
    )

    schema = handler.request_schema["application/json"]["schema"]
    assert "object" == schema["type"]
    assert 3 == len(schema["properties"])
    assert "array" == schema["properties"]["col1"]["type"]
    assert "integer" == schema["properties"]["col1"]["items"]["type"]
    assert "number" == schema["properties"]["col2"]["items"]["type"]
    assert "string" == schema["properties"]["col3"]["items"]["type"]
コード例 #9
0
def test_custom_api_name():
    # these names should work:
    bentoml.api(input=DataframeHandler(), api_name="a_valid_name")(lambda x: x)
    bentoml.api(input=DataframeHandler(), api_name="AValidName")(lambda x: x)
    bentoml.api(input=DataframeHandler(), api_name="_AValidName")(lambda x: x)
    bentoml.api(input=DataframeHandler(), api_name="a_valid_name_123")(lambda x: x)

    with pytest.raises(InvalidArgument) as e:
        bentoml.api(input=DataframeHandler(), api_name="a invalid name")(lambda x: x)
    assert str(e.value).startswith("Invalid API name")

    with pytest.raises(InvalidArgument) as e:
        bentoml.api(input=DataframeHandler(), api_name="123_a_invalid_name")(
            lambda x: x
        )
    assert str(e.value).startswith("Invalid API name")

    with pytest.raises(InvalidArgument) as e:
        bentoml.api(input=DataframeHandler(), api_name="a-invalid-name")(lambda x: x)
    assert str(e.value).startswith("Invalid API name")