def test_json_handle_aws_lambda_event(make_api, json_file):
    with open(json_file) as f:
        test_content = f.read()
    api = make_api(LegacyJsonInput(), predict)

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

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

    error_event_obj = {
        "headers": {
            "Content-Type": "application/json"
        },
        "body": "bad json{}",
    }
    error_response = api.handle_aws_lambda_event(error_event_obj)
    assert error_response["statusCode"] == 400
    assert error_response["body"]
def test_json_handle_cli(capsys, make_api, json_file):
    api = make_api(LegacyJsonInput(), predict)

    test_args = ["--input-file", json_file]
    api.handle_cli(test_args)
    out, _ = capsys.readouterr()
    assert out.strip() == '"kaith"'
def test_image_input_http_request_post_json(make_api, json_file):
    api = make_api(LegacyJsonInput(), predict)
    request = mock.MagicMock(spec=flask.Request)
    request.method = "POST"
    request.files = {}
    request.headers = {}
    request.get_data.return_value = open(str(json_file), 'rb').read()

    response = api.handle_request(request)

    assert response.status_code == 200
    assert response.json == 'kaith'
Exemplo n.º 4
0
class ExampleBentoService(bentoml.BentoService):
    """
    Example BentoService class made for testing purpose
    """
    @bentoml.api(input=DataframeInput(),
                 mb_max_latency=1000,
                 mb_max_batch_size=2000,
                 batch=True)
    def predict(self, df):
        """An API for testing simple bento model service
        """
        return self.artifacts.model.predict(df)

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

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

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

    @bentoml.api(input=LegacyImageInput(input_names=('original', 'compared')),
                 batch=False)
    def predict_legacy_images(self, original, compared):
        return self.artifacts.model.predict_legacy_images(original, compared)

    @bentoml.api(input=JsonInput(), batch=True)
    def predict_json(self, input_data):
        return self.artifacts.model.predict_json(input_data)

    @bentoml.api(input=LegacyJsonInput(), batch=False)
    def predict_legacy_json(self, input_data):
        return self.artifacts.model.predict_legacy_json(input_data)