2016-11-15 07:48:02 +08:00
|
|
|
import os
|
2019-11-07 17:26:22 +08:00
|
|
|
from pathlib import Path
|
2017-01-20 01:16:04 +08:00
|
|
|
from unittest import mock
|
2016-11-15 07:48:02 +08:00
|
|
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2017-01-20 01:16:04 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls.resolvers import LocaleRegexDescriptor, RegexPattern
|
2016-11-15 07:48:02 +08:00
|
|
|
from django.utils import translation
|
|
|
|
|
2017-01-20 21:01:02 +08:00
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
2016-11-15 07:48:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
@override_settings(LOCALE_PATHS=[os.path.join(here, "translations", "locale")])
|
2016-10-21 01:29:04 +08:00
|
|
|
class LocaleRegexDescriptorTests(SimpleTestCase):
|
2016-11-15 07:48:02 +08:00
|
|
|
def setUp(self):
|
|
|
|
translation.trans_real._translations = {}
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
translation.trans_real._translations = {}
|
|
|
|
|
|
|
|
def test_translated_regex_compiled_per_language(self):
|
2016-10-21 01:29:04 +08:00
|
|
|
provider = RegexPattern(translation.gettext_lazy("^foo/$"))
|
2016-11-15 07:48:02 +08:00
|
|
|
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)
|
|
|
|
|
2016-11-06 20:52:07 +08:00
|
|
|
def test_nontranslated_regex_compiled_once(self):
|
2016-10-21 01:29:04 +08:00
|
|
|
provider = RegexPattern("^foo/$")
|
2016-11-06 20:52:07 +08:00
|
|
|
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/$")
|
|
|
|
|
2016-11-15 07:48:02 +08:00
|
|
|
def test_regex_compile_error(self):
|
|
|
|
"""Regex errors are re-raised as ImproperlyConfigured."""
|
2016-10-21 01:29:04 +08:00
|
|
|
provider = RegexPattern("*")
|
2016-11-15 07:48:02 +08:00
|
|
|
msg = '"*" is not a valid regular expression: nothing to repeat'
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
provider.regex
|
2016-11-06 20:52:07 +08:00
|
|
|
|
|
|
|
def test_access_locale_regex_descriptor(self):
|
2016-10-21 01:29:04 +08:00
|
|
|
self.assertIsInstance(RegexPattern.regex, LocaleRegexDescriptor)
|
2019-11-07 17:26:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
@override_settings(LOCALE_PATHS=[Path(here) / "translations" / "locale"])
|
|
|
|
class LocaleRegexDescriptorPathLibTests(LocaleRegexDescriptorTests):
|
|
|
|
pass
|