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 osdf.operation.exceptions import BusinessException
27 from runtime.optim_engine import validate_request, process_request
29 BASE_DIR = os.path.dirname(__file__)
32 class TestOptimEngine():
34 def test_valid_optim_request(self):
35 req_json = json.loads(open("./test/optengine-tests/test_optengine_valid.json").read())
37 assert validate_request(req_json) == True
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)
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)
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)
54 def test_process_request(self):
55 req_json = json.loads(open("./test/optengine-tests/test_optengine_valid.json").read())
57 res = process_request(req_json)
58 assert res.status_code == 400
60 def test_py_process_request(self):
61 req_json = json.loads(open("./test/optengine-tests/test_py_optengine_valid.json").read())
63 res = process_request(req_json)
64 assert res.status_code == 200
66 def test_invalid_solver(self):
67 req_json = json.loads(open("./test/optengine-tests/test_optengine_invalid_solver.json").read())
69 with pytest.raises(BusinessException):
70 process_request(req_json)
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())
76 data = 200, ('junk', '', '', 'py')
77 mocker.return_value = data
78 process_request(req_json)