Esempio n. 1
0
 def test_request_schema(self):
     model = mock.Mock()
     model_name = api_version = mock.MagicMock()
     mock_additional_checks = mock.Mock()
     feature_schema = schemas.Object(properties=dict(
         x=schemas.Integer(),
         y=schemas.Number(),
         z=schemas.String(),
     ))
     with mock.patch('porter.services.BaseService.add_request_schema') as mock_add_request_schema:
         prediction_service = PredictionService(
             model=model,
             name=model_name,
             api_version=api_version,
             meta={},
             preprocessor=None,
             postprocessor=None,
             batch_prediction=False,
             feature_schema=feature_schema,
         )
     request_schema = prediction_service.request_schema
     request = dict(id=1, x=2, y=3.5, z='4')
     request_schema.validate(request)
     with self.assertRaisesRegex(ValueError, 'data must contain'):
         request = dict(x=2, y=3.5, z='4')
         request_schema.validate(request)
Esempio n. 2
0
 def test__add_feature_schema_batch(self):
     model = mock.Mock()
     model_name = api_version = mock.MagicMock()
     mock_additional_checks = mock.Mock()
     feature_schema = schemas.Object(properties=dict(
         x=schemas.Integer(),
         y=schemas.Number(),
         z=schemas.String(),
     ))
     with mock.patch('porter.services.BaseService.add_request_schema') as mock_add_request_schema:
         prediction_service = PredictionService(
             model=model,
             name=model_name,
             api_version=api_version,
             meta={},
             preprocessor=None,
             postprocessor=None,
             batch_prediction=True,
             feature_schema=feature_schema,
         )
     args = mock_add_request_schema.call_args_list[0][0]
     self.assertEqual(args[0].upper(), 'POST')
     request_obj = args[1]
     self.assertIsInstance(request_obj, schemas.Array)
     item_obj = request_obj.item_type
     self.assertIn('id', item_obj.properties)
     self.assertIn('x', item_obj.properties)
     self.assertIn('y', item_obj.properties)
     self.assertIn('z', item_obj.properties)
Esempio n. 3
0
 def setUpClass(cls):
     service1 = PredictionService(
         name='service1',
         api_version='2',
         model=None,  # we're not going to make calls for predictions here
         feature_schema=sc.Object(properties={
             'a': sc.Integer(),
             'b': sc.Integer(),
             'c': sc.Number()
         }))
     service1 = PredictionService(
         namespace='ns',
         name='service2',
         api_version='1',
         model=None,  # we're not going to make calls for predictions here
         feature_schema=sc.Object(properties={
             'a': sc.Integer(),
             'b': sc.Integer()
         }))
Esempio n. 4
0
 def test_add_request_schema(self):
     input_schema = schemas.Object(properties=dict(
         x=schemas.Integer(),
         y=schemas.Number(),
         z=schemas.String()
     ))
     service = mock.MagicMock()
     service.request_schemas = {}
     BaseService.add_request_schema(
         service, 'post', input_schema, description='test')
     request_schema = service.request_schemas['POST']
     self.assertIsInstance(request_schema, schemas.RequestSchema)
     self.assertEqual(request_schema.description, 'test')
     self.assertIs(request_schema.api_obj, input_schema)
Esempio n. 5
0
    def test_get_post_data_validation(self, mock_request_json):
        # this test also implicitly covers BaseService.get_post_data
        mock_model = mock.Mock()
        mock_model.predict.return_value = []
        mock_name = mock_version = mock.MagicMock()
        feature_schema = schemas.Object(properties=dict(x=schemas.Integer()))
        prediction_service = PredictionService(
            model=mock_model,
            name=mock_name,
            api_version=mock_version,
            meta={},
            preprocessor=None,
            postprocessor=None,
            batch_prediction=True,
            feature_schema=feature_schema,
            additional_checks=None
        )

        # Succeed
        mock_request_json.return_value = [{'id': 1, 'x': 37}]
        prediction_service.get_post_data()

        # Succeed
        mock_request_json.return_value = [{'id': 1, 'x': 3.7}]
        prediction_service.get_post_data()

        # Fail
        prediction_service = PredictionService(
            model=mock_model,
            name=mock_name,
            api_version=mock_version + 1,
            meta={},
            preprocessor=None,
            postprocessor=None,
            batch_prediction=True,
            feature_schema=feature_schema,
            validate_request_data=True,
            additional_checks=None)
        with self.assertRaises(werkzeug_exc.UnprocessableEntity):
            prediction_service.get_post_data()