Removed superfluous uses of TransRealMixin.

The translation.override context manager cleans up after itself. As a
consequence this mixin isn't needed any more in many cases.
This commit is contained in:
Aymeric Augustin 2014-01-26 14:29:04 +01:00
parent 3145ae06be
commit b951a7fbc1
5 changed files with 48 additions and 61 deletions

View File

@ -12,7 +12,6 @@ from django.conf import settings
from django.contrib.humanize.templatetags import humanize
from django.template import Template, Context, defaultfilters
from django.test import TestCase, override_settings
from django.test.utils import TransRealMixin
from django.utils.html import escape
from django.utils.timezone import utc, get_fixed_timezone
from django.utils import translation
@ -36,7 +35,7 @@ class MockDateTime(datetime.datetime):
return now.replace(tzinfo=tz) + tz.utcoffset(now)
class HumanizeTests(TransRealMixin, TestCase):
class HumanizeTests(TestCase):
def humanize_tester(self, test_list, result_list, method, normalize_result_func=escape):
for test_content, result in zip(test_list, result_list):

View File

@ -17,7 +17,6 @@ from django.template.defaultfilters import (
urlize, urlizetrunc, wordcount, wordwrap, yesno,
)
from django.test import TestCase
from django.test.utils import TransRealMixin
from django.utils import six
from django.utils import translation
from django.utils.safestring import SafeData
@ -683,11 +682,11 @@ class DefaultFiltersTests(TestCase):
self.assertEqual(striptags(123), '123')
class DefaultFiltersI18NTests(TransRealMixin, TestCase):
class DefaultFiltersI18NTests(TestCase):
def test_localized_filesizeformat(self):
# NOTE: \xa0 avoids wrapping between value and unit
with self.settings(USE_L10N=True), translation.override('de', deactivate=True):
with self.settings(USE_L10N=True), translation.override('de'):
self.assertEqual(filesizeformat(1023), '1023\xa0Bytes')
self.assertEqual(filesizeformat(1024), '1,0\xa0KB')
self.assertEqual(filesizeformat(10 * 1024), '10,0\xa0KB')

View File

@ -9,13 +9,13 @@ from django.forms import (
TextInput,
)
from django.test import TestCase
from django.utils.translation import ugettext_lazy, override
from django.utils import translation
from django.utils.translation import gettext_lazy, ugettext_lazy
from forms_tests.models import Cheese
from django.test.utils import TransRealMixin
class FormsRegressionsTestCase(TransRealMixin, TestCase):
class FormsRegressionsTestCase(TestCase):
def test_class(self):
# Tests to prevent against recurrences of earlier bugs.
extra_attrs = {'class': 'special'}
@ -37,9 +37,9 @@ class FormsRegressionsTestCase(TransRealMixin, TestCase):
self.assertHTMLEqual(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)
with override('de'):
with translation.override('de'):
self.assertHTMLEqual(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):
with translation.override('pl'):
self.assertHTMLEqual(f.as_p(), '<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):
@ -73,13 +73,11 @@ class FormsRegressionsTestCase(TransRealMixin, TestCase):
self.assertEqual(f.clean(b'\xd1\x88\xd1\x82.'), '\u0448\u0442.')
# Translated error messages used to be buggy.
with override('ru'):
with translation.override('ru'):
f = SomeForm({})
self.assertHTMLEqual(f.as_p(), '<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 id="id_somechoice">\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
class CopyForm(Form):
degree = IntegerField(widget=Select(choices=((1, gettext_lazy('test')),)))

View File

@ -5,7 +5,6 @@ import os
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase, override_settings
from django.test.utils import TransRealMixin
from django.utils._os import upath
from django.utils import six
from django.utils import translation
@ -22,7 +21,7 @@ from django.utils import translation
('fr', 'French'),
),
)
class ContentTypeTests(TransRealMixin, TestCase):
class ContentTypeTests(TestCase):
def test_verbose_name(self):
company_type = ContentType.objects.get(app_label='i18n', model='company')
with translation.override('en'):

View File

@ -22,15 +22,14 @@ from django.template import (base as template_base, loader, Context,
from django.template.loaders import app_directories, filesystem, cached
from django.test import RequestFactory, TestCase
from django.test.utils import (setup_test_template_loader,
restore_template_loaders, override_settings, TransRealMixin,
extend_sys_path)
restore_template_loaders, override_settings, extend_sys_path)
from django.utils.encoding import python_2_unicode_compatible
from django.utils.formats import date_format
from django.utils._os import upath
from django.utils.translation import activate, deactivate
from django.utils.safestring import mark_safe
from django.utils import six
from django.utils.six.moves.urllib.parse import urljoin
from django.utils import translation
# NumPy installed?
try:
@ -550,7 +549,7 @@ class TemplateRegressionTests(TestCase):
TEMPLATE_DEBUG=False, ALLOWED_INCLUDE_ROOTS=(
os.path.dirname(os.path.abspath(upath(__file__))),),
)
class TemplateTests(TransRealMixin, TestCase):
class TemplateTests(TestCase):
urls = 'template_tests.urls'
def test_templates(self):
@ -600,54 +599,47 @@ class TemplateTests(TransRealMixin, TestCase):
invalid_string_result = vals[2]
template_debug_result = vals[2]
if 'LANGUAGE_CODE' in vals[1]:
activate(vals[1]['LANGUAGE_CODE'])
else:
activate('en-us')
with translation.override(vals[1].get('LANGUAGE_CODE', 'en-us')):
for invalid_str, template_debug, result in [
('', False, normal_string_result),
(expected_invalid_str, False, invalid_string_result),
('', True, template_debug_result)
]:
with override_settings(TEMPLATE_STRING_IF_INVALID=invalid_str,
TEMPLATE_DEBUG=template_debug):
for is_cached in (False, True):
try:
for invalid_str, template_debug, result in [
('', False, normal_string_result),
(expected_invalid_str, False, invalid_string_result),
('', True, template_debug_result)
]:
with override_settings(TEMPLATE_STRING_IF_INVALID=invalid_str,
TEMPLATE_DEBUG=template_debug):
for is_cached in (False, True):
try:
with warnings.catch_warnings():
# Ignore pending deprecations of the old syntax of the 'cycle' and 'firstof' tags.
warnings.filterwarnings("ignore", category=DeprecationWarning, module='django.template.base')
test_template = loader.get_template(name)
except ShouldNotExecuteException:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Template loading invoked method that shouldn't have been invoked." % (is_cached, invalid_str, template_debug, name))
try:
with warnings.catch_warnings():
# Ignore pending deprecations of the old syntax of the 'cycle' and 'firstof' tags.
warnings.filterwarnings("ignore", category=DeprecationWarning, module='django.template.base')
test_template = loader.get_template(name)
except ShouldNotExecuteException:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Template loading invoked method that shouldn't have been invoked." % (is_cached, invalid_str, template_debug, name))
try:
output = self.render(test_template, vals)
except ShouldNotExecuteException:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Template rendering invoked method that shouldn't have been invoked." % (is_cached, invalid_str, template_debug, name))
except ContextStackException:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Context stack was left imbalanced" % (is_cached, invalid_str, template_debug, name))
continue
except Exception:
exc_type, exc_value, exc_tb = sys.exc_info()
if exc_type != result:
tb = '\n'.join(traceback.format_exception(exc_type, exc_value, exc_tb))
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Got %s, exception: %s\n%s" % (is_cached, invalid_str, template_debug, name, exc_type, exc_value, tb))
continue
if output != result:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Expected %r, got %r" % (is_cached, invalid_str, template_debug, name, result, output))
cache_loader.reset()
try:
output = self.render(test_template, vals)
except ShouldNotExecuteException:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Template rendering invoked method that shouldn't have been invoked." % (is_cached, invalid_str, template_debug, name))
except ContextStackException:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Context stack was left imbalanced" % (is_cached, invalid_str, template_debug, name))
continue
except Exception:
exc_type, exc_value, exc_tb = sys.exc_info()
if exc_type != result:
tb = '\n'.join(traceback.format_exception(exc_type, exc_value, exc_tb))
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Got %s, exception: %s\n%s" % (is_cached, invalid_str, template_debug, name, exc_type, exc_value, tb))
continue
if output != result:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Expected %r, got %r" % (is_cached, invalid_str, template_debug, name, result, output))
cache_loader.reset()
if 'LANGUAGE_CODE' in vals[1]:
deactivate()
if template_base.invalid_var_format_string:
expected_invalid_str = 'INVALID'
template_base.invalid_var_format_string = False
if template_base.invalid_var_format_string:
expected_invalid_str = 'INVALID'
template_base.invalid_var_format_string = False
restore_template_loaders()
deactivate()
self.assertEqual(failures, [], "Tests failed:\n%s\n%s" %
('-' * 70, ("\n%s\n" % ('-' * 70)).join(failures)))