Permalink
Browse files

Make use of new translation and settings context manager in the tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16167 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 71ec87f commit 7dd72360a2ab3a3a7ca74922da670c7361a9abb3 @jezdez jezdez committed May 6, 2011
@@ -22,12 +22,11 @@
import django.template.context
from django.template.response import TemplateResponse
from django.test import TestCase
-from django.utils import formats
+from django.utils import formats, translation
from django.utils.cache import get_max_age
from django.utils.encoding import iri_to_uri
from django.utils.html import escape
from django.utils.http import urlencode
-from django.utils.translation import activate, deactivate
from django.utils import unittest
# local test models
@@ -361,42 +360,31 @@ def testI18NLanguageNonEnglishDefault(self):
if the default language is non-English but the selected language
is English. See #13388 and #3594 for more details.
"""
- try:
- settings.LANGUAGE_CODE = 'fr'
- activate('en-us')
- response = self.client.get('/test_admin/admin/jsi18n/')
- self.assertNotContains(response, 'Choisir une heure')
- finally:
- deactivate()
+ with self.settings(LANGUAGE_CODE='fr'):
+ with translation.override('en-us'):
+ response = self.client.get('/test_admin/admin/jsi18n/')
+ self.assertNotContains(response, 'Choisir une heure')
def testI18NLanguageNonEnglishFallback(self):
"""
Makes sure that the fallback language is still working properly
in cases where the selected language cannot be found.
"""
- try:
- settings.LANGUAGE_CODE = 'fr'
- activate('none')
- response = self.client.get('/test_admin/admin/jsi18n/')
- self.assertContains(response, 'Choisir une heure')
- finally:
- deactivate()
+ with self.settings(LANGUAGE_CODE='fr'):
+ with translation.override('none'):
+ response = self.client.get('/test_admin/admin/jsi18n/')
+ self.assertContains(response, 'Choisir une heure')
def testL10NDeactivated(self):
"""
Check if L10N is deactivated, the Javascript i18n view doesn't
return localized date/time formats. Refs #14824.
"""
- try:
- settings.LANGUAGE_CODE = 'ru'
- settings.USE_L10N = False
- activate('ru')
- response = self.client.get('/test_admin/admin/jsi18n/')
- self.assertNotContains(response, '%d.%m.%Y %H:%M:%S')
- self.assertContains(response, '%Y-%m-%d %H:%M:%S')
- finally:
- deactivate()
-
+ with self.settings(LANGUAGE_CODE='ru', USE_L10N=False):
+ with translation.override('none'):
+ response = self.client.get('/test_admin/admin/jsi18n/')
+ self.assertNotContains(response, '%d.%m.%Y %H:%M:%S')
+ self.assertContains(response, '%Y-%m-%d %H:%M:%S')
def test_disallowed_filtering(self):
self.assertRaises(SuspiciousOperation,
@@ -14,8 +14,8 @@
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.models import DateField
from django.test import TestCase as DjangoTestCase
+from django.utils import translation
from django.utils.html import conditional_escape
-from django.utils.translation import activate, deactivate
from django.utils.unittest import TestCase
import models
@@ -204,7 +204,7 @@ def test_stacked_render(self):
)
-class AdminSplitDateTimeWidgetTest(TestCase):
+class AdminSplitDateTimeWidgetTest(DjangoTestCase):
def test_render(self):
w = AdminSplitDateTime()
self.assertEqual(
@@ -215,18 +215,13 @@ def test_render(self):
def test_localization(self):
w = AdminSplitDateTime()
- activate('de-at')
- old_USE_L10N = settings.USE_L10N
- try:
- settings.USE_L10N = True
- w.is_localized = True
- self.assertEqual(
- conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
- '<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>',
- )
- finally:
- deactivate()
- settings.USE_L10N = old_USE_L10N
+ with self.settings(USE_L10N=True):
+ with translation.override('de-at'):
+ w.is_localized = True
+ self.assertEqual(
+ conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
+ '<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>',
+ )
class AdminFileWidgetTest(DjangoTestCase):
@@ -1,10 +1,11 @@
# -*- coding: utf-8 -*-
import datetime
-from django.utils import unittest
+from django.test import TestCase
+from django.utils import unittest, translation
from django.template.defaultfilters import *
-class DefaultFiltersTests(unittest.TestCase):
+class DefaultFiltersTests(TestCase):
def test_floatformat(self):
self.assertEqual(floatformat(7.7), u'7.7')
@@ -458,31 +459,25 @@ def test_filesizeformat(self):
u'0 bytes')
def test_localized_filesizeformat(self):
- from django.utils.translation import activate, deactivate
- old_localize = settings.USE_L10N
- try:
- activate('de')
- settings.USE_L10N = True
- self.assertEqual(filesizeformat(1023), u'1023 Bytes')
- self.assertEqual(filesizeformat(1024), u'1,0 KB')
- self.assertEqual(filesizeformat(10*1024), u'10,0 KB')
- self.assertEqual(filesizeformat(1024*1024-1), u'1024,0 KB')
- self.assertEqual(filesizeformat(1024*1024), u'1,0 MB')
- self.assertEqual(filesizeformat(1024*1024*50), u'50,0 MB')
- self.assertEqual(filesizeformat(1024*1024*1024-1), u'1024,0 MB')
- self.assertEqual(filesizeformat(1024*1024*1024), u'1,0 GB')
- self.assertEqual(filesizeformat(1024*1024*1024*1024), u'1,0 TB')
- self.assertEqual(filesizeformat(1024*1024*1024*1024*1024),
- u'1,0 PB')
- self.assertEqual(filesizeformat(1024*1024*1024*1024*1024*2000),
- u'2000,0 PB')
- self.assertEqual(filesizeformat(complex(1,-1)), u'0 Bytes')
- self.assertEqual(filesizeformat(""), u'0 Bytes')
- self.assertEqual(filesizeformat(u"\N{GREEK SMALL LETTER ALPHA}"),
- u'0 Bytes')
- finally:
- deactivate()
- settings.USE_L10N = old_localize
+ with self.settings(USE_L10N=True):
+ with translation.override('de', deactivate=True):
+ self.assertEqual(filesizeformat(1023), u'1023 Bytes')
+ self.assertEqual(filesizeformat(1024), u'1,0 KB')
+ self.assertEqual(filesizeformat(10*1024), u'10,0 KB')
+ self.assertEqual(filesizeformat(1024*1024-1), u'1024,0 KB')
+ self.assertEqual(filesizeformat(1024*1024), u'1,0 MB')
+ self.assertEqual(filesizeformat(1024*1024*50), u'50,0 MB')
+ self.assertEqual(filesizeformat(1024*1024*1024-1), u'1024,0 MB')
+ self.assertEqual(filesizeformat(1024*1024*1024), u'1,0 GB')
+ self.assertEqual(filesizeformat(1024*1024*1024*1024), u'1,0 TB')
+ self.assertEqual(filesizeformat(1024*1024*1024*1024*1024),
+ u'1,0 PB')
+ self.assertEqual(filesizeformat(1024*1024*1024*1024*1024*2000),
+ u'2000,0 PB')
+ self.assertEqual(filesizeformat(complex(1,-1)), u'0 Bytes')
+ self.assertEqual(filesizeformat(""), u'0 Bytes')
+ self.assertEqual(filesizeformat(u"\N{GREEK SMALL LETTER ALPHA}"),
+ u'0 Bytes')
def test_pluralize(self):
self.assertEqual(pluralize(1), u'')
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from django.forms import *
from django.utils.unittest import TestCase
-from django.utils.translation import ugettext_lazy, activate, deactivate
+from django.utils.translation import ugettext_lazy, override
from regressiontests.forms.models import Cheese
@@ -28,11 +28,10 @@ class SomeForm(Form):
self.assertEqual(f.as_p(), '<p><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>')
# Translations are done at rendering time, so multi-lingual apps can define forms)
- activate('de')
- self.assertEqual(f.as_p(), '<p><label for="id_username">Benutzername:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>')
- activate('pl')
- self.assertEqual(f.as_p(), u'<p><label for="id_username">Nazwa u\u017cytkownika:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>')
- deactivate()
+ with override('de'):
+ self.assertEqual(f.as_p(), '<p><label for="id_username">Benutzername:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>')
+ with override('pl', deactivate=True):
+ self.assertEqual(f.as_p(), u'<p><label for="id_username">Nazwa u\u017cytkownika:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>')
def test_regression_5216(self):
# There was some problems with form translations in #5216
@@ -61,10 +60,9 @@ class SomeForm(Form):
self.assertEqual(f.clean('\xd1\x88\xd1\x82.'), u'\u0448\u0442.')
# Translated error messages used to be buggy.
- activate('ru')
- f = SomeForm({})
- self.assertEqual(f.as_p(), u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>')
- deactivate()
+ with override('ru'):
+ f = SomeForm({})
+ self.assertEqual(f.as_p(), u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>')
# Deep copying translated text shouldn't raise an error)
from django.utils.translation import gettext_lazy
Oops, something went wrong.

0 comments on commit 7dd7236

Please sign in to comment.