1 # -------------------------------------------------------------------------
2 # Copyright (c) 2017-2018 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 # -------------------------------------------------------------------------
21 from osdf.operation.exceptions import BusinessException
22 from requests import Request, RequestException
23 from schematics.exceptions import DataError
24 from unittest import mock, TestCase
25 from unittest.mock import patch
28 class TestOSDFApp(TestCase):
31 self.patcher_g = patch('osdfapp.g', return_value={'request_id':'DUMMY-REQ'})
32 self.Mock_g = self.patcher_g.start()
33 # self.patcher2 = patch('package.module.Class2')
34 # self.MockClass2 = self.patcher2.start()
39 def dummy_request_exception(self):
40 e = RequestException("Web Request Exception Description")
41 e.response = mock.MagicMock()
42 e.request = Request(method="GET", url="SOME-URL")
43 e.response.status_code = 400
44 e.response.content = "Some request exception occurred"
45 # request().raise_for_status.side_effect = e
48 def test_handle_business_exception(self):
49 e = BusinessException("Business Exception Description")
50 resp = osdfapp.handle_business_exception(e)
51 assert resp.status_code == 400
53 def test_handle_request_exception(self):
54 e = self.dummy_request_exception()
55 resp = osdfapp.handle_request_exception(e)
56 assert resp.status_code == 400
58 def test_handle_data_error(self):
59 e = DataError({"A1": "A1 Data Error"})
60 resp = osdfapp.handle_data_error(e)
61 assert resp.status_code == 400
63 def test_internal_failure(self):
64 e = Exception("An Internal Error")
65 resp = osdfapp.internal_failure(e)
66 assert resp.status_code == 500
68 def test_get_options_default(self):
69 opts = osdfapp.get_options(["PROG"]) # ensure nothing breaks
72 if __name__ == "__main__":