コード例 #1
0
    def test8(self):
        """testing predict() controller will handle exceptions in the model class correctly"""
        # arrange
        model_manager = ModelManager()
        model_manager.load_models(configuration=[{
            "module_name": "tests.web_api.controllers_test",
            "class_name": "MLModelMock"
        }])

        # act
        exception_raised = False
        try:
            result = controllers.predict(qualified_name="qualified_name",
                                         request_body='{}')
            schema = ErrorSchema()
            data = schema.loads(json_data=result.data)
        except Exception as e:
            exception_raised = True

        # assert
        self.assertFalse(exception_raised)
        self.assertTrue(type(result) == controllers.Response)
        self.assertTrue(result.status == 500)
        self.assertTrue(result.mimetype == "application/json")
        self.assertTrue(
            json.loads(result.data) == {
                "message": "Could not make a prediction.",
                "type": "ERROR"
            })
コード例 #2
0
    def test7(self):
        """testing predict() controller with data that does not meet the model schema"""
        # arrange
        model_manager = ModelManager()
        model_manager.load_models(configuration=[{
            "module_name": "iris_model.iris_predict",
            "class_name": "IrisModel"
        }])

        # act
        result = controllers.predict(
            qualified_name="iris_model",
            request_body=
            '{"petal_length": "asdf", "petal_width": 1.0, "sepal_length": 1.0, "sepal_width": 1.0}'
        )
        schema = ErrorSchema()
        data = schema.loads(json_data=result.data)

        # assert
        self.assertTrue(type(result) == controllers.Response)
        self.assertTrue(result.status == 400)
        self.assertTrue(result.mimetype == "application/json")
        self.assertTrue(
            json.loads(result.data) == {
                "message":
                "Failed to validate input data: Key 'petal_length' error:\n'asdf' should be instance of 'float'",
                "type": "SCHEMA_ERROR"
            })
コード例 #3
0
    def test5(self):
        """testing predict() controller with non-existing model"""
        # arrange
        model_manager = ModelManager()
        model_manager.load_models(configuration=[{
            "module_name": "iris_model.iris_predict",
            "class_name": "IrisModel"
        }])

        # act
        result = controllers.predict(
            qualified_name="asdf",
            request_body=
            '{"petal_length": 1.0, "petal_width": 1.0, "sepal_length": 1.0, "sepal_width": 1.0}'
        )
        schema = ErrorSchema()
        data = schema.loads(json_data=result.data)

        # assert
        self.assertTrue(type(result) == controllers.Response)
        self.assertTrue(result.status == 404)
        self.assertTrue(result.mimetype == "application/json")
        self.assertTrue(
            json.loads(result.data) == {
                "type": "ERROR",
                "message": "Model not found."
            })
コード例 #4
0
def lambda_handler(event, context):
    """Lambda handler function."""
    # detecting if the event came from an API Gateway
    if event.get("resource") is not None \
            and event.get("path") is not None \
            and event.get("httpMethod") is not None:

        if event["resource"] == "/api/models" and event["httpMethod"] == "GET":
            # calling the get_models controller function
            response = get_models()

        elif event[
                "resource"] == "/api/models/{qualified_name}/metadata" and event[
                    "httpMethod"] == "GET":
            # calling the get_metadata controller function
            response = get_metadata(
                qualified_name=event["pathParameters"]["qualified_name"])

        elif event["resource"] == "/api/models/{qualified_name}/predict" \
                and event["httpMethod"] == "POST" \
                and event.get("pathParameters") is not None \
                and event["pathParameters"].get("qualified_name") is not None:
            # calling the predict controller function
            response = predict(
                qualified_name=event["pathParameters"]["qualified_name"],
                request_body=event["body"])

        else:
            raise ValueError("This lambda cannot handle this resource.")

        return {
            "isBase64Encoded": False,
            "statusCode": response.status,
            "headers": {
                "Content-Type": response.mimetype
            },
            "body": response.data
        }

    else:
        raise ValueError("This lambda cannot handle this event type.")
コード例 #5
0
    def test6(self):
        """testing predict() controller with good data"""
        # arrange
        model_manager = ModelManager()
        model_manager.load_models(configuration=[{
            "module_name": "iris_model.iris_predict",
            "class_name": "IrisModel"
        }])

        # act
        result = controllers.predict(
            qualified_name="iris_model",
            request_body=
            '{"petal_length": 1.0, "petal_width": 1.0, "sepal_length": 1.0, "sepal_width": 1.0}'
        )
        data = json.loads(result.data)

        # assert
        self.assertTrue(type(result) == controllers.Response)
        self.assertTrue(result.status == 200)
        self.assertTrue(result.mimetype == "application/json")
        self.assertTrue(json.loads(result.data) == {"species": "setosa"})
コード例 #6
0
    def test4(self):
        """testing predict() controller with bad data"""
        # arrange
        model_manager = ModelManager()
        model_manager.load_models(configuration=[{
            "module_name": "iris_model.iris_predict",
            "class_name": "IrisModel"
        }])

        # act
        result = controllers.predict(qualified_name="iris_model",
                                     request_body="")
        schema = ErrorSchema()
        data = schema.loads(json_data=result.data)

        # assert
        self.assertTrue(type(result) == controllers.Response)
        self.assertTrue(result.status == 400)
        self.assertTrue(result.mimetype == "application/json")
        self.assertTrue(
            json.loads(result.data) == {
                "type": "DESERIALIZATION_ERROR",
                "message": "Expecting value: line 1 column 1 (char 0)"
            })