Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'djdt' is not a registered namespace #1405 #1889

Merged
merged 30 commits into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5b2b969
changing the default position of the toolbar to the top half instead …
VeldaKiara Jan 23, 2024
2edf71e
updated the change to the changes.rst file
VeldaKiara Jan 23, 2024
3312069
fixing the position of the toolbar button to the upper top
VeldaKiara Jan 24, 2024
83e9739
solution to djdt registered namespace,update docs, system checks and …
VeldaKiara Feb 27, 2024
1193e0c
Merge branch 'main' into main
VeldaKiara Feb 27, 2024
3affaea
renaming the check to RUNNING_TESTS
VeldaKiara Feb 28, 2024
36cf020
renaming the check to RUNNING_TESTS
VeldaKiara Feb 28, 2024
8bf25e9
removing test in the if statements
VeldaKiara Feb 29, 2024
46f64c4
Add basic test to example app and example_test to make commands.
tim-schilling Feb 29, 2024
5941710
Update check for toolbar and tests.
tim-schilling Feb 29, 2024
df02d33
update check using with, combine the four tests to one, update defaul…
VeldaKiara Mar 8, 2024
18eaab5
update installation files and remove patch from namespace check
VeldaKiara Mar 11, 2024
2c21976
fix additional text on step 3 on example app
VeldaKiara Mar 11, 2024
e944857
Merge branch 'main' into main
VeldaKiara Mar 11, 2024
7f5da54
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 11, 2024
713c4cf
Remove .vscode/settings.json from tracking
VeldaKiara Mar 11, 2024
65bf1cb
remove unnecessary files
VeldaKiara Mar 11, 2024
c7ad698
Remove vscode.settings.json
VeldaKiara Mar 11, 2024
18ead4e
simple fix
VeldaKiara Mar 11, 2024
aef39b2
Merge branch 'main' into main
VeldaKiara Mar 25, 2024
a515cc1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 25, 2024
4820a9a
fix the assertion error
VeldaKiara Apr 26, 2024
424bf02
Delete .vscode/settings.json
tim-schilling Apr 26, 2024
c620b7c
Remove space from docs make file
tim-schilling Apr 26, 2024
effccff
Clean-up the changelog
tim-schilling Apr 26, 2024
902665c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 26, 2024
3cb2f7c
Add docs for the IS_RUNNING_TESTS setting
tim-schilling Apr 26, 2024
a182401
Apply suggestions from code review
tim-schilling Apr 26, 2024
9d906fb
Change the code for the toolbar testing error to E001
tim-schilling Apr 26, 2024
0e90884
Reduce number of .settings calls and document config update.
tim-schilling Apr 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
@@ -0,0 +1,3 @@
{
"esbonio.sphinx.confDir": ""
}
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -6,6 +6,9 @@ example:
--noinput --username="$(USER)" --email="$(USER)@mailinator.com"
python example/manage.py runserver

example_test:
python example/manage.py test example

test:
DJANGO_SETTINGS_MODULE=tests.settings \
python -m django test $${TEST_ARGS:-tests}
Expand Down
24 changes: 23 additions & 1 deletion debug_toolbar/apps.py
Expand Up @@ -3,7 +3,7 @@

from django.apps import AppConfig
from django.conf import settings
from django.core.checks import Warning, register
from django.core.checks import Error, Warning, register
from django.middleware.gzip import GZipMiddleware
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -206,3 +206,25 @@ def js_mimetype_check(app_configs, **kwargs):
)
]
return []


@register()
def debug_toolbar_installed_when_running_tests_check(app_configs, **kwargs):
"""
Check that the toolbar is not being used when tests are running
"""
if not settings.DEBUG and dt_settings.get_config()["IS_RUNNING_TESTS"]:
return [
Error(
"The Django Debug Toolbar can't be used with tests",
hint="Django changes the DEBUG setting to False when running "
"tests. By default the Django Debug Toolbar is installed because "
"DEBUG is set to True. For most cases, you need to avoid installing "
"the toolbar when running tests. If you feel this check is in error, "
"you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to "
"bypass this check.",
id="debug_toolbar.W008",
)
]
else:
return []
2 changes: 2 additions & 0 deletions debug_toolbar/settings.py
@@ -1,3 +1,4 @@
import sys
import warnings
from functools import lru_cache

Expand Down Expand Up @@ -42,6 +43,7 @@
"SQL_WARNING_THRESHOLD": 500, # milliseconds
"OBSERVE_REQUEST_CALLBACK": "debug_toolbar.toolbar.observe_request",
"TOOLBAR_LANGUAGE": None,
"IS_RUNNING_TESTS": "test" in sys.argv,
"UPDATE_ON_FETCH": False,
}

Expand Down
1 change: 1 addition & 0 deletions docs/Makefile
Expand Up @@ -8,6 +8,7 @@ SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build


# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
Expand Down
5 changes: 5 additions & 0 deletions docs/changes.rst
Expand Up @@ -29,6 +29,11 @@ Pending
<https://astral.sh/blog/the-ruff-formatter>`__.
* Changed the default position of the toolbar from top to the upper top
position.
* Added a link in the installation docs to a more complete installation
example in the example app.
* Added check to prevent the toolbar from being installed when tests
are running.
* Added test to example app and command to run the example app's tests.
* Added the setting, ``UPDATE_ON_FETCH`` to control whether the
toolbar automatically updates to the latest AJAX request or not.
It defaults to ``False``.
Expand Down
6 changes: 6 additions & 0 deletions docs/installation.rst
Expand Up @@ -81,6 +81,11 @@ Add ``"debug_toolbar"`` to your ``INSTALLED_APPS`` setting:
"debug_toolbar",
# ...
]
.. note:: Check out the configuration example in the
`example app
<https://github.com/jazzband/django-debug-toolbar/tree/main/example>`_
to learn how to set up the toolbar to function smoothly while running
your tests.

4. Add the URLs
^^^^^^^^^^^^^^^
Expand All @@ -99,6 +104,7 @@ Add django-debug-toolbar's URLs to your project's URLconf:
This example uses the ``__debug__`` prefix, but you can use any prefix that
doesn't clash with your application's URLs.


5. Add the Middleware
^^^^^^^^^^^^^^^^^^^^^

Expand Down
20 changes: 17 additions & 3 deletions example/settings.py
@@ -1,12 +1,14 @@
"""Django settings for example project."""

import os
import sys

BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production


SECRET_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

DEBUG = True
Expand All @@ -22,11 +24,9 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"debug_toolbar",
]

MIDDLEWARE = [
"debug_toolbar.middleware.DebugToolbarMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
Expand Down Expand Up @@ -61,7 +61,6 @@

WSGI_APPLICATION = "example.wsgi.application"

DEBUG_TOOLBAR_CONFIG = {"ROOT_TAG_EXTRA_ATTRS": "data-turbo-permanent hx-preserve"}

# Cache and database

Expand Down Expand Up @@ -97,3 +96,18 @@
}

STATICFILES_DIRS = [os.path.join(BASE_DIR, "example", "static")]


# Only enable the toolbar when we're in debug mode and we're
# not running tests. Django will change DEBUG to be False for
# tests, so we can't rely on DEBUG alone.
ENABLE_DEBUG_TOOLBAR = DEBUG and "test" not in sys.argv
if ENABLE_DEBUG_TOOLBAR:
INSTALLED_APPS += [
"debug_toolbar",
]
MIDDLEWARE += [
"debug_toolbar.middleware.DebugToolbarMiddleware",
]
# Customize the config to support turbo and htmx boosting.
DEBUG_TOOLBAR_CONFIG = {"ROOT_TAG_EXTRA_ATTRS": "data-turbo-permanent hx-preserve"}
12 changes: 12 additions & 0 deletions example/test_views.py
@@ -0,0 +1,12 @@
# Add tests to example app to check how the toolbar is used
# when running tests for a project.
# See https://github.com/jazzband/django-debug-toolbar/issues/1405

from django.test import TestCase
from django.urls import reverse


class ViewTestCase(TestCase):
def test_index(self):
response = self.client.get(reverse("home"))
assert response.status_code == 200
7 changes: 6 additions & 1 deletion example/urls.py
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib import admin
from django.urls import include, path
from django.views.generic import TemplateView
Expand Down Expand Up @@ -33,5 +34,9 @@
),
path("admin/", admin.site.urls),
path("ajax/increment", increment, name="ajax_increment"),
path("__debug__/", include("debug_toolbar.urls")),
]

if settings.ENABLE_DEBUG_TOOLBAR:
urlpatterns += [
path("__debug__/", include("debug_toolbar.urls")),
]
3 changes: 2 additions & 1 deletion tests/settings.py
Expand Up @@ -126,5 +126,6 @@

DEBUG_TOOLBAR_CONFIG = {
# Django's test client sets wsgi.multiprocess to True inappropriately
"RENDER_PANELS": False
"RENDER_PANELS": False,
"IS_RUNNING_TESTS": False,
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved
}
82 changes: 80 additions & 2 deletions tests/test_checks.py
Expand Up @@ -4,9 +4,12 @@

import django
from django.conf import settings
from django.core.checks import Warning, run_checks
from django.core.checks import Error, Warning, run_checks
from django.test import SimpleTestCase, override_settings

from debug_toolbar import settings as dt_settings
from debug_toolbar.apps import debug_toolbar_installed_when_running_tests_check

PATH_DOES_NOT_EXIST = os.path.join(settings.BASE_DIR, "tests", "invalid_static")


Expand Down Expand Up @@ -120,7 +123,17 @@ def test_panels_is_empty(self):
hint="Set DEBUG_TOOLBAR_PANELS to a non-empty list in your "
"settings.py.",
id="debug_toolbar.W005",
)
),
Error(
"The Django Debug Toolbar can't be used with tests",
hint="Django changes the DEBUG setting to False when running "
"tests. By default the Django Debug Toolbar is installed because "
"DEBUG is set to True. For most cases, you need to avoid installing "
"the toolbar when running tests. If you feel this check is in error, "
"you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to "
"bypass this check.",
id="debug_toolbar.W008",
),
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved
],
)

Expand Down Expand Up @@ -258,3 +271,68 @@ def test_check_w007_invalid(self, mocked_guess_type):
)
],
)

# def test_debug_toolbar_installed_when_running_tests(self, mocked_get_config):
# with self.settings(DEBUG=False):
# dt_settings.get_config()["IS_RUNNING_TESTS"] = False
# errors = debug_toolbar_installed_when_running_tests_check(None)
# self.assertEqual(len(errors), 0)
# with self.settings(DEBUG=True):
# dt_settings.get_config()["IS_RUNNING_TESTS"] = True
# errors = debug_toolbar_installed_when_running_tests_check(None)
# self.assertEqual(len(errors), 0)
# with self.settings(DEBUG=True):
# dt_settings.get_config()["IS_RUNNING_TESTS"] = False
# errors = debug_toolbar_installed_when_running_tests_check(None)
# self.assertEqual(len(errors), 0)
# with self.settings(DEBUG=False):
# dt_settings.get_config()["IS_RUNNING_TESTS"] = True
# errors = debug_toolbar_installed_when_running_tests_check(None)
# self.assertEqual(
# errors,
# [
# Error(
# "The Django Debug Toolbar can't be used with tests",
# hint="Django changes the DEBUG setting to False when running "
# "tests. By default the Django Debug Toolbar is installed because "
# "DEBUG is set to True. For most cases, you need to avoid installing "
# "the toolbar when running tests. If you feel this check is in error, "
# "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to "
# "bypass this check.",
# id="debug_toolbar.W008",
# )
# ],

# )
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved

def test_debug_toolbar_installed_when_running_tests(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why you're modifying the dt_settings.get_config() return value instead of using e.g. with override_settings(...)? I suspect that there's a good reason for it; if there is, please add a short docstring.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were to use override_settings, we'd have to define the entire DEBUG_TOOLBAR_CONFIG dictionary. And I don't think there's a merge settings method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know what you think of the new comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! No further comments :-)

with self.settings(DEBUG=False):
dt_settings.get_config()["IS_RUNNING_TESTS"] = False
errors = debug_toolbar_installed_when_running_tests_check(None)
self.assertEqual(len(errors), 0)
with self.settings(DEBUG=True):
dt_settings.get_config()["IS_RUNNING_TESTS"] = True
errors = debug_toolbar_installed_when_running_tests_check(None)
self.assertEqual(len(errors), 0)
with self.settings(DEBUG=True):
dt_settings.get_config()["IS_RUNNING_TESTS"] = False
errors = debug_toolbar_installed_when_running_tests_check(None)
self.assertEqual(len(errors), 0)
with self.settings(DEBUG=False):
dt_settings.get_config()["IS_RUNNING_TESTS"] = True
errors = debug_toolbar_installed_when_running_tests_check(None)
self.assertEqual(
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved
errors,
[
Error(
"The Django Debug Toolbar can't be used with tests",
hint="Django changes the DEBUG setting to False when running "
"tests. By default the Django Debug Toolbar is installed because "
"DEBUG is set to True. For most cases, you need to avoid installing "
"the toolbar when running tests. If you feel this check is in error, "
"you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to "
"bypass this check.",
id="debug_toolbar.W008",
)
],
)