1
0
mirror of https://github.com/django/django.git synced 2024-12-22 09:05:43 +00:00

Added tests for LocaleRegexProvider.

This commit is contained in:
Carl Meyer 2016-11-14 18:48:02 -05:00 committed by Tim Graham
parent 0a63ef3f61
commit e454db3eee
9 changed files with 81 additions and 0 deletions

View File

@ -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

View File

@ -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 <EMAIL@ADDRESS>, 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 <de@li.org>\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/$"

View File

@ -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 <EMAIL@ADDRESS>, 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 <fr@li.org>\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/$"