1 # -------------------------------------------------------------------------
2 # Copyright (c) 2020 AT&T Intellectual Property
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # -------------------------------------------------------------------------
23 from mock import patch
24 from schematics.exceptions import DataError
26 from runtime.model_api import create_model_data, get_model_data, delete_model_data, retrieve_all_models
27 from runtime.models.api.model_request import OptimModelRequestAPI
28 from runtime.optim_engine import validate_request
30 BASE_DIR = os.path.dirname(__file__)
32 ret_val = {'modelId': 'test', 'description': 'desc', 'solver': 'mzn'}
37 def test_valid_mapi_request(self):
38 req_json = json.loads(open("./test/optengine-tests/test_modelapi_valid.json").read())
40 assert OptimModelRequestAPI(req_json).validate() is None
42 def test_invalid_mapi_request(self):
43 req_json = json.loads(open("./test/optengine-tests/test_modelapi_invalid.json").read())
44 with pytest.raises(DataError):
45 validate_request(req_json)
47 @patch('runtime.model_api.build_model_dict')
48 @patch('mysql.connector.connect')
49 @patch('runtime.model_api.osdf_config')
50 def test_create_model(self, config, conn, model_data):
51 model_data.return_value = ret_val
52 req_json = json.loads(open("./test/optengine-tests/test_modelapi_valid.json").read())
54 create_model_data(req_json)
56 @patch('runtime.model_api.build_model_dict')
57 @patch('mysql.connector.connect')
58 @patch('runtime.model_api.osdf_config')
59 def test_retrieve_model(self, config, conn, model_data):
60 model_data.return_value = ret_val
61 get_model_data('test')
63 @patch('mysql.connector.connect')
64 @patch('runtime.model_api.osdf_config')
65 def test_delete_model(self, config, conn):
66 delete_model_data('test')
68 @patch('mysql.connector.connect')
69 @patch('runtime.model_api.osdf_config')
70 def test_retrieve_all_model(self, config, conn):