Skip to content

Commit

Permalink
tests: Improve TestNav
Browse files Browse the repository at this point in the history
using reverse prevents us from adding views with parameters in
active_view_names, which is needed to add GPS views in the nav menu

the named_urls function was cherry-picked from #5017
  • Loading branch information
tonial committed Feb 10, 2025
1 parent ee5873e commit 7c7bc0c
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions tests/utils/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.core.management import call_command
from django.template import Context, Template
from django.test import override_settings
from django.urls import reverse
from django.urls import URLPattern, URLResolver, get_resolver

from itou.users.enums import UserKind
from itou.users.models import User
Expand Down Expand Up @@ -89,10 +89,29 @@ def test_itou_buttons_mandatory_fields_mention(self, snapshot):


class TestNav:
def test_active_view_names(self):
@pytest.fixture(scope="class")
def named_urls(self):
resolver = get_resolver()
known_urls = set()
for resolver in resolver.url_patterns:
if isinstance(resolver, URLResolver):
prefix = f"{resolver.namespace}:" if resolver.namespace else ""
for urlname in resolver.reverse_dict:
if isinstance(urlname, str):
qualname = f"{prefix}{urlname}"
known_urls.add(qualname)
elif isinstance(resolver, URLPattern):
if resolver.name:
known_urls.add(resolver.name)
else:
raise ValueError
return frozenset(known_urls)

def test_active_view_names(self, named_urls):
# build all valid view names
for entry in NAV_ENTRIES.values():
for view_name in entry.active_view_names:
reverse(view_name)
assert view_name in named_urls


class TestThemeInclusion:
Expand Down

0 comments on commit 7c7bc0c

Please sign in to comment.