update link to upper-constraints.txt
[optf/osdf.git] / test / test_model_api.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2020 AT&T Intellectual Property
3 #
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
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 import json
20 import os
21
22 import pytest
23 from mock import patch
24 from schematics.exceptions import DataError
25
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
29
30 BASE_DIR = os.path.dirname(__file__)
31
32 ret_val = {'modelId': 'test', 'description': 'desc', 'solver': 'mzn'}
33
34
35 class TestModelApi():
36
37     def test_valid_mapi_request(self):
38         req_json = json.loads(open("./test/optengine-tests/test_modelapi_valid.json").read())
39
40         assert OptimModelRequestAPI(req_json).validate() is None
41
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)
46
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())
53
54         create_model_data(req_json)
55
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')
62
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')
67
68     @patch('mysql.connector.connect')
69     @patch('runtime.model_api.osdf_config')
70     def test_retrieve_all_model(self, config, conn):
71         retrieve_all_models()