|
1 | 1 | from unittest.mock import patch
|
2 | 2 |
|
3 |
| -from django.core.checks import Error, Warning, run_checks |
| 3 | +from django.core.checks import Warning, run_checks |
4 | 4 | from django.test import SimpleTestCase, override_settings
|
| 5 | +from django.urls import NoReverseMatch |
5 | 6 |
|
6 |
| -from debug_toolbar import settings as dt_settings |
7 | 7 | from debug_toolbar.apps import debug_toolbar_installed_when_running_tests_check
|
8 | 8 |
|
9 | 9 |
|
@@ -239,39 +239,70 @@ def test_check_w007_invalid(self, mocked_guess_type):
|
239 | 239 | ],
|
240 | 240 | )
|
241 | 241 |
|
242 |
| - def test_debug_toolbar_installed_when_running_tests(self): |
243 |
| - with self.settings(DEBUG=True): |
244 |
| - # Update the config options because self.settings() |
245 |
| - # would require redefining DEBUG_TOOLBAR_CONFIG entirely. |
246 |
| - dt_settings.get_config()["IS_RUNNING_TESTS"] = True |
247 |
| - errors = debug_toolbar_installed_when_running_tests_check(None) |
248 |
| - self.assertEqual(len(errors), 0) |
249 |
| - |
250 |
| - dt_settings.get_config()["IS_RUNNING_TESTS"] = False |
251 |
| - errors = debug_toolbar_installed_when_running_tests_check(None) |
252 |
| - self.assertEqual(len(errors), 0) |
253 |
| - with self.settings(DEBUG=False): |
254 |
| - dt_settings.get_config()["IS_RUNNING_TESTS"] = False |
255 |
| - errors = debug_toolbar_installed_when_running_tests_check(None) |
256 |
| - self.assertEqual(len(errors), 0) |
| 242 | + @patch("debug_toolbar.apps.reverse") |
| 243 | + def test_debug_toolbar_installed_when_running_tests(self, reverse): |
| 244 | + params = [ |
| 245 | + { |
| 246 | + "debug": True, |
| 247 | + "running_tests": True, |
| 248 | + "show_callback_changed": True, |
| 249 | + "urls_installed": False, |
| 250 | + "errors": False, |
| 251 | + }, |
| 252 | + { |
| 253 | + "debug": False, |
| 254 | + "running_tests": False, |
| 255 | + "show_callback_changed": True, |
| 256 | + "urls_installed": False, |
| 257 | + "errors": False, |
| 258 | + }, |
| 259 | + { |
| 260 | + "debug": False, |
| 261 | + "running_tests": True, |
| 262 | + "show_callback_changed": False, |
| 263 | + "urls_installed": False, |
| 264 | + "errors": False, |
| 265 | + }, |
| 266 | + { |
| 267 | + "debug": False, |
| 268 | + "running_tests": True, |
| 269 | + "show_callback_changed": True, |
| 270 | + "urls_installed": True, |
| 271 | + "errors": False, |
| 272 | + }, |
| 273 | + { |
| 274 | + "debug": False, |
| 275 | + "running_tests": True, |
| 276 | + "show_callback_changed": True, |
| 277 | + "urls_installed": False, |
| 278 | + "errors": True, |
| 279 | + }, |
| 280 | + ] |
| 281 | + for config in params: |
| 282 | + with self.subTest(**config): |
| 283 | + config_setting = { |
| 284 | + "RENDER_PANELS": False, |
| 285 | + "IS_RUNNING_TESTS": config["running_tests"], |
| 286 | + "SHOW_TOOLBAR_CALLBACK": ( |
| 287 | + (lambda *args: True) |
| 288 | + if config["show_callback_changed"] |
| 289 | + else "debug_toolbar.middleware.show_toolbar" |
| 290 | + ), |
| 291 | + } |
| 292 | + if config["urls_installed"]: |
| 293 | + reverse.side_effect = lambda *args: None |
| 294 | + else: |
| 295 | + reverse.side_effect = NoReverseMatch() |
257 | 296 |
|
258 |
| - dt_settings.get_config()["IS_RUNNING_TESTS"] = True |
259 |
| - errors = debug_toolbar_installed_when_running_tests_check(None) |
260 |
| - self.assertEqual( |
261 |
| - errors, |
262 |
| - [ |
263 |
| - Error( |
264 |
| - "The Django Debug Toolbar can't be used with tests", |
265 |
| - hint="Django changes the DEBUG setting to False when running " |
266 |
| - "tests. By default the Django Debug Toolbar is installed because " |
267 |
| - "DEBUG is set to True. For most cases, you need to avoid installing " |
268 |
| - "the toolbar when running tests. If you feel this check is in error, " |
269 |
| - "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " |
270 |
| - "bypass this check.", |
271 |
| - id="debug_toolbar.E001", |
272 |
| - ) |
273 |
| - ], |
274 |
| - ) |
| 297 | + with self.settings( |
| 298 | + DEBUG=config["debug"], DEBUG_TOOLBAR_CONFIG=config_setting |
| 299 | + ): |
| 300 | + errors = debug_toolbar_installed_when_running_tests_check(None) |
| 301 | + if config["errors"]: |
| 302 | + self.assertEqual(len(errors), 1) |
| 303 | + self.assertEqual(errors[0].id, "debug_toolbar.E001") |
| 304 | + else: |
| 305 | + self.assertEqual(len(errors), 0) |
275 | 306 |
|
276 | 307 | @override_settings(
|
277 | 308 | DEBUG_TOOLBAR_CONFIG={
|
|
0 commit comments