|
1 | 1 | from unittest import IsolatedAsyncioTestCase
|
2 | 2 | from unittest.mock import patch, MagicMock
|
3 | 3 |
|
| 4 | +from fastapi import HTTPException |
| 5 | + |
4 | 6 | from daemon import daemon
|
5 | 7 | from tests._utils import import_module, AsyncMock, mock_dict
|
6 | 8 |
|
@@ -119,3 +121,83 @@ async def test__make_exception(self, httpexception_patch: MagicMock, jsonrespons
|
119 | 121 | status_code,
|
120 | 122 | )
|
121 | 123 | 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