update link to upper-constraints.txt
[optf/osdf.git] / test / test_optim_engine.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 osdf.operation.exceptions import BusinessException
27 from runtime.optim_engine import validate_request, process_request
28
29 BASE_DIR = os.path.dirname(__file__)
30
31
32 class TestOptimEngine():
33
34     def test_valid_optim_request(self):
35         req_json = json.loads(open("./test/optengine-tests/test_optengine_valid.json").read())
36
37         assert validate_request(req_json) == True
38
39     def test_invalid_optim_request(self):
40         req_json = json.loads(open("./test/optengine-tests/test_optengine_invalid.json").read())
41         with pytest.raises(DataError):
42             validate_request(req_json)
43
44     def test_invalid_optim_request_without_modelid(self):
45         req_json = json.loads(open("./test/optengine-tests/test_optengine_invalid2.json").read())
46         with pytest.raises(BusinessException):
47             validate_request(req_json)
48
49     def test_invalid_optim_request_no_optdata(self):
50         req_json = json.loads(open("./test/optengine-tests/test_optengine_no_optdata.json").read())
51         with pytest.raises(BusinessException):
52             validate_request(req_json)
53
54     def test_process_request(self):
55         req_json = json.loads(open("./test/optengine-tests/test_optengine_valid.json").read())
56
57         res = process_request(req_json)
58         assert res.status_code == 400
59
60     def test_py_process_request(self):
61         req_json = json.loads(open("./test/optengine-tests/test_py_optengine_valid.json").read())
62
63         res = process_request(req_json)
64         assert res.status_code == 200
65
66     def test_invalid_solver(self):
67         req_json = json.loads(open("./test/optengine-tests/test_optengine_invalid_solver.json").read())
68
69         with pytest.raises(BusinessException):
70             process_request(req_json)
71
72     @patch('runtime.optim_engine.get_model_data')
73     def test_process_solverid_request(self, mocker):
74         req_json = json.loads(open("./test/optengine-tests/test_optengine_modelId.json").read())
75
76         data = 200, ('junk', '', '', 'py')
77         mocker.return_value = data
78         process_request(req_json)