Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 01fb59b

Browse files
committed
Added unit tests for exception handlers (#12)
1 parent ee79830 commit 01fb59b

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

Diff for: daemon/daemon.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from fastapi import FastAPI, status, Request
2-
from fastapi.exceptions import HTTPException
3-
from fastapi.exceptions import RequestValidationError
2+
from fastapi.exceptions import HTTPException, RequestValidationError
43
from fastapi.params import Depends
54
from fastapi.responses import JSONResponse
65

Diff for: tests/test_daemon.py

+82
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from unittest import IsolatedAsyncioTestCase
22
from unittest.mock import patch, MagicMock
33

4+
from fastapi import HTTPException
5+
46
from daemon import daemon
57
from tests._utils import import_module, AsyncMock, mock_dict
68

@@ -119,3 +121,83 @@ async def test__make_exception(self, httpexception_patch: MagicMock, jsonrespons
119121
status_code,
120122
)
121123
self.assertEqual(jsonresponse_patch(), result)
124+
125+
@patch("daemon.exceptions.api_exception.APIException")
126+
@patch("fastapi.FastAPI")
127+
async def test__handle_api_exception(self, fastapi_patch: MagicMock, apiexception_patch: MagicMock):
128+
_, handle_api_exception = self.get_decorated_function(fastapi_patch, "exception_handler", apiexception_patch)
129+
exception = MagicMock()
130+
131+
result = await handle_api_exception(..., exception)
132+
133+
exception.make_response.assert_called_once_with()
134+
self.assertEqual(exception.make_response(), result)
135+
136+
@patch("fastapi.exceptions.HTTPException")
137+
@patch("fastapi.FastAPI")
138+
async def test__handle_http_exception(self, fastapi_patch: MagicMock, httpexception_patch: MagicMock):
139+
module, handle_http_exception = self.get_decorated_function(
140+
fastapi_patch,
141+
"exception_handler",
142+
httpexception_patch,
143+
)
144+
exception = MagicMock()
145+
_make_exception, module._make_exception = module._make_exception, MagicMock()
146+
147+
result = await handle_http_exception(..., exception)
148+
149+
module._make_exception.assert_called_once_with(exception.status_code)
150+
self.assertEqual(module._make_exception(), result)
151+
module._make_exception = _make_exception
152+
153+
@patch("fastapi.exceptions.RequestValidationError")
154+
@patch("fastapi.FastAPI")
155+
async def test__handle_unprocessable_entity(
156+
self,
157+
fastapi_patch: MagicMock,
158+
request_validation_error_patch: MagicMock,
159+
):
160+
module, handle_unprocessable_entity = self.get_decorated_function(
161+
fastapi_patch,
162+
"exception_handler",
163+
request_validation_error_patch,
164+
)
165+
exception = MagicMock()
166+
_make_exception, module._make_exception = module._make_exception, MagicMock()
167+
168+
result = await handle_unprocessable_entity(..., exception)
169+
170+
exception.errors.assert_called_once_with()
171+
module._make_exception.assert_called_once_with(422, detail=exception.errors())
172+
self.assertEqual(module._make_exception(), result)
173+
module._make_exception = _make_exception
174+
175+
@patch("fastapi.FastAPI")
176+
async def test__handle_internal_server_error(self, fastapi_patch: MagicMock):
177+
module, handle_internal_server_error = self.get_decorated_function(
178+
fastapi_patch,
179+
"exception_handler",
180+
Exception,
181+
)
182+
exception = MagicMock()
183+
_make_exception, module._make_exception = module._make_exception, MagicMock()
184+
185+
result = await handle_internal_server_error(..., exception)
186+
187+
module._make_exception.assert_called_once_with(500)
188+
self.assertEqual(module._make_exception(), result)
189+
module._make_exception = _make_exception
190+
191+
@patch("fastapi.FastAPI")
192+
async def test__handle_not_found(self, fastapi_patch: MagicMock):
193+
module, handle_not_found = self.get_decorated_function(
194+
fastapi_patch,
195+
"get",
196+
"/{_:path}",
197+
include_in_schema=False,
198+
)
199+
200+
with self.assertRaises(HTTPException) as context:
201+
await handle_not_found()
202+
203+
self.assertEqual(404, context.exception.status_code)

0 commit comments

Comments
 (0)