From e454db3eeeb7be33875181955b543f9d6a3d329a Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Mon, 14 Nov 2016 18:48:02 -0500 Subject: [PATCH] Added tests for LocaleRegexProvider. --- .../test_localeregexprovider.py | 41 ++++++++++++++++++ .../translations/__init__.py | 0 .../translations/locale/__init__.py | 0 .../locale/de/LC_MESSAGES/django.mo | Bin 0 -> 450 bytes .../locale/de/LC_MESSAGES/django.po | 20 +++++++++ .../translations/locale/de/__init__.py | 0 .../locale/fr/LC_MESSAGES/django.mo | Bin 0 -> 426 bytes .../locale/fr/LC_MESSAGES/django.po | 20 +++++++++ .../translations/locale/fr/__init__.py | 0 9 files changed, 81 insertions(+) create mode 100644 tests/urlpatterns_reverse/test_localeregexprovider.py create mode 100644 tests/urlpatterns_reverse/translations/__init__.py create mode 100644 tests/urlpatterns_reverse/translations/locale/__init__.py create mode 100644 tests/urlpatterns_reverse/translations/locale/de/LC_MESSAGES/django.mo create mode 100644 tests/urlpatterns_reverse/translations/locale/de/LC_MESSAGES/django.po create mode 100644 tests/urlpatterns_reverse/translations/locale/de/__init__.py create mode 100644 tests/urlpatterns_reverse/translations/locale/fr/LC_MESSAGES/django.mo create mode 100644 tests/urlpatterns_reverse/translations/locale/fr/LC_MESSAGES/django.po create mode 100644 tests/urlpatterns_reverse/translations/locale/fr/__init__.py diff --git a/tests/urlpatterns_reverse/test_localeregexprovider.py b/tests/urlpatterns_reverse/test_localeregexprovider.py new file mode 100644 index 00000000000..9da8df656c6 --- /dev/null +++ b/tests/urlpatterns_reverse/test_localeregexprovider.py @@ -0,0 +1,41 @@ +from __future__ import unicode_literals + +import os + +from django.core.exceptions import ImproperlyConfigured +from django.test import SimpleTestCase, mock, override_settings +from django.urls import LocaleRegexProvider +from django.utils import translation +from django.utils._os import upath + +here = os.path.dirname(upath(os.path.abspath(__file__))) + + +@override_settings(LOCALE_PATHS=[os.path.join(here, 'translations', 'locale')]) +class LocaleRegexProviderTests(SimpleTestCase): + def setUp(self): + translation.trans_real._translations = {} + + def tearDown(self): + translation.trans_real._translations = {} + + def test_translated_regex_compiled_per_language(self): + provider = LocaleRegexProvider(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_regex_compile_error(self): + """Regex errors are re-raised as ImproperlyConfigured.""" + provider = LocaleRegexProvider('*') + msg = '"*" is not a valid regular expression: nothing to repeat' + with self.assertRaisesMessage(ImproperlyConfigured, msg): + provider.regex diff --git a/tests/urlpatterns_reverse/translations/__init__.py b/tests/urlpatterns_reverse/translations/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/urlpatterns_reverse/translations/locale/__init__.py b/tests/urlpatterns_reverse/translations/locale/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/urlpatterns_reverse/translations/locale/de/LC_MESSAGES/django.mo b/tests/urlpatterns_reverse/translations/locale/de/LC_MESSAGES/django.mo new file mode 100644 index 0000000000000000000000000000000000000000..fc53234553fdc7c38b72e9d143c0002ba23b9a8f GIT binary patch literal 450 zcmYL_!AiqG5Qd|uV2++Wtl&Y>X|svYk~XDSTc}u!C5k6yZKiQcx*K-4q7UM`_%uF? zlX}RoA2UD9?EX8yM|#RS=N*f0=TgV))H-&{v$IMUfGZ+~ZjwawnIE+~s z@Bo4k_!Z-wIEpSv2h<32{E+p{O&^DVL&#ak?MrwiHgAnB5Acq-A#~ zb1Ye(JdqrhTiKFosRuNpij61`G8Aj_jjW&Ju#_!rih)0y&c@BBcq{NnS~*no%vVKz z9e%oYRSKzkZ1FCPMVCBg6X-U5wVfpz7^{WOr7B|9eUr{l%Z(8wOte|sn5k+jZ4&m_ Uf0>*qc9JlD?tAr`AxGNuACWtHBLDyZ literal 0 HcmV?d00001 diff --git a/tests/urlpatterns_reverse/translations/locale/de/LC_MESSAGES/django.po b/tests/urlpatterns_reverse/translations/locale/de/LC_MESSAGES/django.po new file mode 100644 index 00000000000..a4dde5ff1ba --- /dev/null +++ b/tests/urlpatterns_reverse/translations/locale/de/LC_MESSAGES/django.po @@ -0,0 +1,20 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: django tests\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2010-02-14 17:33+0100\n" +"PO-Revision-Date: 2011-01-21 21:37-0300\n" +"Last-Translator: Carl Meyer\n" +"Language-Team: de \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1)\n" + +msgid "^foo/$" +msgstr "^foo-de/$" diff --git a/tests/urlpatterns_reverse/translations/locale/de/__init__.py b/tests/urlpatterns_reverse/translations/locale/de/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/urlpatterns_reverse/translations/locale/fr/LC_MESSAGES/django.mo b/tests/urlpatterns_reverse/translations/locale/fr/LC_MESSAGES/django.mo new file mode 100644 index 0000000000000000000000000000000000000000..e755e5baeade0069f91f3aafead4a676033a9475 GIT binary patch literal 426 zcmYL_%}T>S5XYmaV2++WjNn1hX|svYvTa4ItrQH#62+6SHk+7|?uOm1*az|Mlld%8 z>LJ7am>KpzAI$I3-nU1#OB@rI#6B@38cT=+;)%H5@w`{^hr|!X^tN^6T6@d2?w)#y z(M!2-ILq)w8e3|`AzKPnXmHXx>(6DajlIdsU3v__zy>C3Qk7A&V8>rC+3DO F`Uk01aZCUJ literal 0 HcmV?d00001 diff --git a/tests/urlpatterns_reverse/translations/locale/fr/LC_MESSAGES/django.po b/tests/urlpatterns_reverse/translations/locale/fr/LC_MESSAGES/django.po new file mode 100644 index 00000000000..de1b96611ce --- /dev/null +++ b/tests/urlpatterns_reverse/translations/locale/fr/LC_MESSAGES/django.po @@ -0,0 +1,20 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: django tests\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2010-02-14 17:33+0100\n" +"PO-Revision-Date: 2011-01-21 21:37-0300\n" +"Last-Translator: Carl Meyer\n" +"Language-Team: fr \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1)\n" + +msgid "^foo/$" +msgstr "^foo-fr/$" diff --git a/tests/urlpatterns_reverse/translations/locale/fr/__init__.py b/tests/urlpatterns_reverse/translations/locale/fr/__init__.py new file mode 100644 index 00000000000..e69de29bb2d