|
| 1 | +import importlib |
| 2 | +import types |
| 3 | + |
1 | 4 | from django.core import exceptions as django_exceptions |
2 | 5 | from django.http import Http404 |
3 | 6 | from rest_framework import exceptions as drf_exceptions |
|
10 | 13 | class TestApplyExtraHandlers: |
11 | 14 | """Test cases for the apply_extra_handlers function in handlers module.""" |
12 | 15 |
|
13 | | - def test_extra_handlers_called(self, mocker): |
| 16 | + @pytest.fixture |
| 17 | + def mock_importlib_import_module(self, mocker): |
| 18 | + """Mock importlib.import_module to control the import behavior in tests.""" |
| 19 | + return mocker.patch.object(importlib, "import_module") |
| 20 | + |
| 21 | + def test_calls_expected_default_handlers(self, mocker): |
| 22 | + """Test that all the expected default handlers are called. |
| 23 | +
|
| 24 | + This is a sanity check to ensure that the default handlers are always called. |
| 25 | + """ |
| 26 | + # Always make sure that the settings handlers are empty for this test |
| 27 | + # to isolate the default handlers. |
| 28 | + mocker.patch("drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS", []) |
| 29 | + |
| 30 | + extra_handlers_applied = handlers.apply_extra_handlers(Exception()) |
| 31 | + |
| 32 | + assert extra_handlers_applied == len(handlers.DEFAULT_EXTRA_HANDLERS) |
| 33 | + |
| 34 | + def test_extra_handlers_calls_string_import( |
| 35 | + self, monkeypatch, mocker, mock_importlib_import_module |
| 36 | + ): |
| 37 | + """Test that extra handlers with string imports are called correctly.""" |
| 38 | + # Set default handlers to empty for this test to isolate the settings one. |
| 39 | + monkeypatch.setattr(handlers, "DEFAULT_EXTRA_HANDLERS", []) |
| 40 | + # Make importlib.import_module return the test mock handler when called |
| 41 | + mock_handler = mocker.MagicMock() |
| 42 | + dummy_module = types.SimpleNamespace(mock_handler=mock_handler) |
| 43 | + mock_importlib_import_module.return_value = dummy_module |
| 44 | + # Patch the settings to include the test mock handler |
| 45 | + monkeypatch.setattr( |
| 46 | + "drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS", |
| 47 | + # Add dummy path to have at least one mock handler to import from a string |
| 48 | + ["path.to.module.mock_handler"], |
| 49 | + ) |
| 50 | + |
| 51 | + # exc = mocker.MagicMock() |
| 52 | + exc = Exception() |
| 53 | + extra_handlers_applied = handlers.apply_extra_handlers(exc) |
| 54 | + |
| 55 | + assert extra_handlers_applied == 1 |
| 56 | + mock_handler.assert_called_once_with(exc) |
| 57 | + |
| 58 | + def test_extra_handlers_calls_multiple( |
| 59 | + self, monkeypatch, mocker, mock_importlib_import_module |
| 60 | + ): |
| 61 | + """ |
| 62 | + Test that both default and settings extra handlers are called correctly. |
| 63 | + """ |
| 64 | + # Make importlib.import_module return the test mock handler when called |
| 65 | + mock_handler = mocker.MagicMock() |
| 66 | + dummy_module = types.SimpleNamespace(mock_handler=mock_handler) |
| 67 | + mock_importlib_import_module.return_value = dummy_module |
| 68 | + # Patch the settings to include the test mock handler |
| 69 | + monkeypatch.setattr( |
| 70 | + "drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS", |
| 71 | + # Add dummy path to have at least one mock handler to import from a string |
| 72 | + ["path.to.module.mock_handler"], |
| 73 | + ) |
| 74 | + |
| 75 | + # exc = mocker.MagicMock() |
| 76 | + exc = Exception() |
| 77 | + extra_handlers_applied = handlers.apply_extra_handlers(exc) |
| 78 | + |
| 79 | + assert ( |
| 80 | + extra_handlers_applied == len(handlers.DEFAULT_EXTRA_HANDLERS) + 1 |
| 81 | + ) # 1 for the settings handler |
| 82 | + mock_handler.assert_called_once_with(exc) |
| 83 | + |
| 84 | + def test_extra_handlers_value_error_on_non_string_import(self, monkeypatch, mocker): |
14 | 85 | """ |
15 | | - Test that extra handlers are called with the exception. |
| 86 | + Test that a ValueError is raised when EXTRA_HANDLERS contains |
| 87 | + a non-string import. |
| 88 | + """ |
| 89 | + monkeypatch.setattr( |
| 90 | + "drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS", |
| 91 | + [mocker.MagicMock()], |
| 92 | + ) |
| 93 | + |
| 94 | + with pytest.raises(ValueError) as e: |
| 95 | + handlers.apply_extra_handlers(Exception()) |
16 | 96 |
|
17 | | - Testing with multiple handlers to ensure all are invoked, and this gives |
18 | | - confidence one or more than two handlers can be used correctly. |
| 97 | + assert "EXTRA_HANDLERS must be a list of strings" in str(e.value) |
| 98 | + |
| 99 | + def test_extra_handlers_value_error_when_path_to_extra_handler_not_found( |
| 100 | + self, monkeypatch, mocker |
| 101 | + ): |
| 102 | + """ |
| 103 | + Test that a ValueError is raised when the path to the extra handler |
| 104 | + does not exist. |
19 | 105 | """ |
20 | | - mock_extra_handler = mocker.MagicMock() |
21 | | - mock_another_extra_handler = mocker.MagicMock() |
| 106 | + monkeypatch.setattr( |
| 107 | + "drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS", |
| 108 | + ["path.to.non_existent_handler"], |
| 109 | + ) |
22 | 110 |
|
23 | | - mocker.patch( |
| 111 | + with pytest.raises(ValueError) as e: |
| 112 | + handlers.apply_extra_handlers(Exception()) |
| 113 | + |
| 114 | + assert "Path path.to.non_existent_handler not found." in str(e.value) |
| 115 | + |
| 116 | + def test_extra_handlers_value_error_when_extra_handler_is_none( |
| 117 | + self, monkeypatch, mocker, mock_importlib_import_module |
| 118 | + ): |
| 119 | + """ |
| 120 | + Test that a ValueError is raised when the extra handler is None. |
| 121 | + """ |
| 122 | + # Make importlib.import_module return a module without the expected attribute |
| 123 | + dummy_module = types.SimpleNamespace() |
| 124 | + mock_importlib_import_module.return_value = dummy_module |
| 125 | + monkeypatch.setattr( |
24 | 126 | "drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS", |
25 | | - [mock_extra_handler, mock_another_extra_handler], |
| 127 | + ["path.to.module.non_existent_handler"], |
26 | 128 | ) |
27 | 129 |
|
28 | | - exc = mocker.MagicMock() |
29 | | - handlers.apply_extra_handlers(exc) |
| 130 | + with pytest.raises(ValueError) as e: |
| 131 | + handlers.apply_extra_handlers(Exception()) |
30 | 132 |
|
31 | | - mock_extra_handler.assert_called_once_with(exc) |
32 | | - mock_another_extra_handler.assert_called_once_with(exc) |
| 133 | + assert "Handler non_existent_handler not found." in str(e.value) |
33 | 134 |
|
34 | | - def test_no_extra_handlers(self, mocker): |
35 | | - """Test that no extra handlers are called when EXTRA_HANDLERS is empty.""" |
36 | | - mock_extra_handler = mocker.MagicMock() |
| 135 | + def test_extra_handlers_value_error_when_extra_handler_not_callable( |
| 136 | + self, monkeypatch, mocker, mock_importlib_import_module |
| 137 | + ): |
| 138 | + """ |
| 139 | + Test that a ValueError is raised when the extra handler is not callable. |
| 140 | + """ |
| 141 | + # Make importlib.import_module return a non-callable attribute |
| 142 | + dummy_module = types.SimpleNamespace(mock_handler="not_a_function") |
| 143 | + mock_importlib_import_module.return_value = dummy_module |
| 144 | + monkeypatch.setattr( |
| 145 | + "drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS", |
| 146 | + ["path.to.module.mock_handler"], |
| 147 | + ) |
37 | 148 |
|
38 | | - exc = mocker.MagicMock() |
39 | | - handlers.apply_extra_handlers(exc) |
| 149 | + with pytest.raises(ValueError) as e: |
| 150 | + handlers.apply_extra_handlers(Exception()) |
40 | 151 |
|
41 | | - mock_extra_handler.assert_not_called() |
| 152 | + assert "Handler mock_handler is not callable." in str(e.value) |
42 | 153 |
|
43 | 154 |
|
44 | 155 | class TestConvertDjangoExcToDRFAPIExc: |
|
0 commit comments