mirror of
https://github.com/django/django.git
synced 2025-08-03 02:23:12 +00:00
Fixed #28593 -- Added a simplified URL routing syntax per DEP 0201.
Thanks Aymeric Augustin for shepherding the DEP and patch review. Thanks Marten Kenbeek and Tim Graham for contributing to the code. Thanks Tom Christie, Shai Berger, and Tim Graham for the docs.
This commit is contained in:
parent
c4c128d67c
commit
df41b5a05d
77 changed files with 1663 additions and 1105 deletions
54
tests/urlpatterns_reverse/test_localeregexdescriptor.py
Normal file
54
tests/urlpatterns_reverse/test_localeregexdescriptor.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
import os
|
||||
from unittest import mock
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.test import SimpleTestCase, override_settings
|
||||
from django.urls.resolvers import LocaleRegexDescriptor, RegexPattern
|
||||
from django.utils import translation
|
||||
|
||||
here = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
@override_settings(LOCALE_PATHS=[os.path.join(here, 'translations', 'locale')])
|
||||
class LocaleRegexDescriptorTests(SimpleTestCase):
|
||||
def setUp(self):
|
||||
translation.trans_real._translations = {}
|
||||
|
||||
def tearDown(self):
|
||||
translation.trans_real._translations = {}
|
||||
|
||||
def test_translated_regex_compiled_per_language(self):
|
||||
provider = RegexPattern(translation.gettext_lazy('^foo/$'))
|
||||
with translation.override('de'):
|
||||
de_compiled = provider.regex
|
||||
# compiled only once per language
|
||||
error = AssertionError('tried to compile url regex twice for the same language')
|
||||
with mock.patch('django.urls.resolvers.re.compile', side_effect=error):
|
||||
de_compiled_2 = provider.regex
|
||||
with translation.override('fr'):
|
||||
fr_compiled = provider.regex
|
||||
self.assertEqual(fr_compiled.pattern, '^foo-fr/$')
|
||||
self.assertEqual(de_compiled.pattern, '^foo-de/$')
|
||||
self.assertEqual(de_compiled, de_compiled_2)
|
||||
|
||||
def test_nontranslated_regex_compiled_once(self):
|
||||
provider = RegexPattern('^foo/$')
|
||||
with translation.override('de'):
|
||||
de_compiled = provider.regex
|
||||
with translation.override('fr'):
|
||||
# compiled only once, regardless of language
|
||||
error = AssertionError('tried to compile non-translated url regex twice')
|
||||
with mock.patch('django.urls.resolvers.re.compile', side_effect=error):
|
||||
fr_compiled = provider.regex
|
||||
self.assertEqual(de_compiled.pattern, '^foo/$')
|
||||
self.assertEqual(fr_compiled.pattern, '^foo/$')
|
||||
|
||||
def test_regex_compile_error(self):
|
||||
"""Regex errors are re-raised as ImproperlyConfigured."""
|
||||
provider = RegexPattern('*')
|
||||
msg = '"*" is not a valid regular expression: nothing to repeat'
|
||||
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
||||
provider.regex
|
||||
|
||||
def test_access_locale_regex_descriptor(self):
|
||||
self.assertIsInstance(RegexPattern.regex, LocaleRegexDescriptor)
|
Loading…
Add table
Add a link
Reference in a new issue