4 from osdf.operation.exceptions import BusinessException
5 from requests import Request, RequestException
6 from schematics.exceptions import DataError
7 from unittest import mock, TestCase
8 from unittest.mock import patch
11 class TestOSDFApp(TestCase):
14 self.patcher_g = patch('osdfapp.g', return_value={'request_id':'DUMMY-REQ'})
15 self.Mock_g = self.patcher_g.start()
16 # self.patcher2 = patch('package.module.Class2')
17 # self.MockClass2 = self.patcher2.start()
22 def dummy_request_exception(self):
23 e = RequestException("Web Request Exception Description")
24 e.response = mock.MagicMock()
25 e.request = Request(method="GET", url="SOME-URL")
26 e.response.status_code = 400
27 e.response.content = "Some request exception occurred"
28 # request().raise_for_status.side_effect = e
31 def test_handle_business_exception(self):
32 e = BusinessException("Business Exception Description")
33 resp = osdfapp.handle_business_exception(e)
34 assert resp.status_code == 400
36 def test_handle_request_exception(self):
37 e = self.dummy_request_exception()
38 resp = osdfapp.handle_request_exception(e)
39 assert resp.status_code == 400
41 def test_handle_data_error(self):
42 e = DataError({"A1": "A1 Data Error"})
43 resp = osdfapp.handle_data_error(e)
44 assert resp.status_code == 400
46 def test_internal_failure(self):
47 e = Exception("An Internal Error")
48 resp = osdfapp.internal_failure(e)
49 assert resp.status_code == 500
51 def test_getOptions_default(self):
52 opts = osdfapp.getOptions(["PROG"]) # ensure nothing breaks
55 if __name__ == "__main__":