Skip to content

Commit 4ad97f1

Browse files
committed
Fix tests
1 parent 7176414 commit 4ad97f1

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

tests/test_hadlers.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_extra_handlers_calls_string_import(
4242
dummy_module = types.SimpleNamespace(mock_handler=mock_handler)
4343
mock_importlib_import_module.return_value = dummy_module
4444
# Patch the settings to include the test mock handler
45-
mocker.patch(
45+
monkeypatch.setattr(
4646
"drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS",
4747
# Add dummy path to have at least one mock handler to import from a string
4848
["path.to.module.mock_handler"],
@@ -55,7 +55,9 @@ def test_extra_handlers_calls_string_import(
5555
assert extra_handlers_applied == 1
5656
mock_handler.assert_called_once_with(exc)
5757

58-
def test_extra_handlers_calls_multiple(self, mocker, mock_importlib_import_module):
58+
def test_extra_handlers_calls_multiple(
59+
self, monkeypatch, mocker, mock_importlib_import_module
60+
):
5961
"""
6062
Test that both default and settings extra handlers are called correctly.
6163
"""
@@ -64,7 +66,7 @@ def test_extra_handlers_calls_multiple(self, mocker, mock_importlib_import_modul
6466
dummy_module = types.SimpleNamespace(mock_handler=mock_handler)
6567
mock_importlib_import_module.return_value = dummy_module
6668
# Patch the settings to include the test mock handler
67-
mocker.patch(
69+
monkeypatch.setattr(
6870
"drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS",
6971
# Add dummy path to have at least one mock handler to import from a string
7072
["path.to.module.mock_handler"],
@@ -79,12 +81,12 @@ def test_extra_handlers_calls_multiple(self, mocker, mock_importlib_import_modul
7981
) # 1 for the settings handler
8082
mock_handler.assert_called_once_with(exc)
8183

82-
def test_extra_handlers_value_error_on_non_string_import(self, mocker):
84+
def test_extra_handlers_value_error_on_non_string_import(self, monkeypatch, mocker):
8385
"""
8486
Test that a ValueError is raised when EXTRA_HANDLERS contains
8587
a non-string import.
8688
"""
87-
mocker.patch(
89+
monkeypatch.setattr(
8890
"drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS",
8991
[mocker.MagicMock()],
9092
)
@@ -95,13 +97,13 @@ def test_extra_handlers_value_error_on_non_string_import(self, mocker):
9597
assert "EXTRA_HANDLERS must be a list of strings" in str(e.value)
9698

9799
def test_extra_handlers_value_error_when_path_to_extra_handler_not_found(
98-
self, mocker
100+
self, monkeypatch, mocker
99101
):
100102
"""
101103
Test that a ValueError is raised when the path to the extra handler
102104
does not exist.
103105
"""
104-
mocker.patch(
106+
monkeypatch.setattr(
105107
"drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS",
106108
["path.to.non_existent_handler"],
107109
)
@@ -112,15 +114,15 @@ def test_extra_handlers_value_error_when_path_to_extra_handler_not_found(
112114
assert "Path path.to.non_existent_handler not found." in str(e.value)
113115

114116
def test_extra_handlers_value_error_when_extra_handler_is_none(
115-
self, mocker, mock_importlib_import_module
117+
self, monkeypatch, mocker, mock_importlib_import_module
116118
):
117119
"""
118120
Test that a ValueError is raised when the extra handler is None.
119121
"""
120122
# Make importlib.import_module return a module without the expected attribute
121123
dummy_module = types.SimpleNamespace()
122124
mock_importlib_import_module.return_value = dummy_module
123-
mocker.patch(
125+
monkeypatch.setattr(
124126
"drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS",
125127
["path.to.module.non_existent_handler"],
126128
)
@@ -131,15 +133,15 @@ def test_extra_handlers_value_error_when_extra_handler_is_none(
131133
assert "Handler non_existent_handler not found." in str(e.value)
132134

133135
def test_extra_handlers_value_error_when_extra_handler_not_callable(
134-
self, mocker, mock_importlib_import_module
136+
self, monkeypatch, mocker, mock_importlib_import_module
135137
):
136138
"""
137139
Test that a ValueError is raised when the extra handler is not callable.
138140
"""
139141
# Make importlib.import_module return a non-callable attribute
140142
dummy_module = types.SimpleNamespace(mock_handler="not_a_function")
141143
mock_importlib_import_module.return_value = dummy_module
142-
mocker.patch(
144+
monkeypatch.setattr(
143145
"drf_simple_api_errors.settings.api_settings.EXTRA_HANDLERS",
144146
["path.to.module.mock_handler"],
145147
)

0 commit comments

Comments
 (0)