Removed TransRealMixin.

Fixed #21688. Refs https://github.com/django/django/pull/1147.
This commit is contained in:
Aymeric Augustin 2014-01-26 15:28:33 +01:00
parent b951a7fbc1
commit b87bc461c8
3 changed files with 31 additions and 41 deletions

View File

@ -42,6 +42,9 @@ def update_installed_apps(**kwargs):
# Rebuild app_template_dirs cache. # Rebuild app_template_dirs cache.
from django.template.loaders import app_directories as mod from django.template.loaders import app_directories as mod
mod.app_template_dirs = mod.calculate_app_template_dirs() mod.app_template_dirs = mod.calculate_app_template_dirs()
# Rebuild translations cache.
from django.utils.translation import trans_real
trans_real._translations = {}
@receiver(setting_changed) @receiver(setting_changed)
@ -97,11 +100,14 @@ def clear_serializers_cache(**kwargs):
@receiver(setting_changed) @receiver(setting_changed)
def language_changed(**kwargs): def language_changed(**kwargs):
if kwargs['setting'] in ('LOCALE_PATHS', 'LANGUAGE_CODE'): if kwargs['setting'] in {'LANGUAGES', 'LANGUAGE_CODE', 'LOCALE_PATHS'}:
from django.utils.translation import trans_real from django.utils.translation import trans_real
trans_real._default = None trans_real._default = None
if kwargs['setting'] == 'LOCALE_PATHS': trans_real._active = threading.local()
trans_real._translations = {} if kwargs['setting'] in {'LANGUAGES', 'LOCALE_PATHS'}:
from django.utils.translation import trans_real
trans_real._translations = {}
trans_real.check_for_language.cache_clear()
@receiver(setting_changed) @receiver(setting_changed)

View File

@ -2,7 +2,6 @@ from contextlib import contextmanager
import logging import logging
import re import re
import sys import sys
from threading import local
import time import time
from unittest import skipUnless from unittest import skipUnless
import warnings import warnings
@ -502,22 +501,6 @@ def patch_logger(logger_name, log_level):
setattr(logger, log_level, orig) setattr(logger, log_level, orig)
class TransRealMixin(object):
"""This is the only way to reset the translation machinery. Otherwise
the test suite occasionally fails because of global state pollution
between tests."""
def flush_caches(self):
from django.utils.translation import trans_real
trans_real._translations = {}
trans_real._active = local()
trans_real._default = None
trans_real.check_for_language.cache_clear()
def tearDown(self):
self.flush_caches()
super(TransRealMixin, self).tearDown()
# On OSes that don't provide tzset (Windows), we can't set the timezone # On OSes that don't provide tzset (Windows), we can't set the timezone
# in which the program runs. As a consequence, we must skip tests that # in which the program runs. As a consequence, we must skip tests that
# don't enforce a specific timezone (with timezone.override or equivalent), # don't enforce a specific timezone (with timezone.override or equivalent),

View File

@ -12,7 +12,6 @@ from django.conf import settings
from django.template import Template, Context from django.template import Template, Context
from django.template.base import TemplateSyntaxError from django.template.base import TemplateSyntaxError
from django.test import TestCase, RequestFactory, override_settings from django.test import TestCase, RequestFactory, override_settings
from django.test.utils import TransRealMixin
from django.utils import translation from django.utils import translation
from django.utils.formats import (get_format, date_format, time_format, from django.utils.formats import (get_format, date_format, time_format,
localize, localize_input, iter_format_modules, get_format_modules, localize, localize_input, iter_format_modules, get_format_modules,
@ -31,7 +30,8 @@ from django.utils.translation import (activate, deactivate,
ungettext_lazy, ungettext_lazy,
pgettext, pgettext,
npgettext, npgettext_lazy, npgettext, npgettext_lazy,
check_for_language) check_for_language,
string_concat)
from .forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm from .forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm
from .models import Company, TestModel from .models import Company, TestModel
@ -43,17 +43,19 @@ extended_locale_paths = settings.LOCALE_PATHS + (
) )
class TranslationTests(TransRealMixin, TestCase): class TranslationTests(TestCase):
def test_override(self): def test_override(self):
activate('de') activate('de')
with translation.override('pl'): try:
self.assertEqual(get_language(), 'pl') with translation.override('pl'):
self.assertEqual(get_language(), 'de') self.assertEqual(get_language(), 'pl')
with translation.override(None): self.assertEqual(get_language(), 'de')
self.assertEqual(get_language(), settings.LANGUAGE_CODE) with translation.override(None):
self.assertEqual(get_language(), 'de') self.assertEqual(get_language(), settings.LANGUAGE_CODE)
deactivate() self.assertEqual(get_language(), 'de')
finally:
deactivate()
def test_lazy_objects(self): def test_lazy_objects(self):
""" """
@ -265,8 +267,7 @@ class TranslationTests(TransRealMixin, TestCase):
""" """
six.text_type(string_concat(...)) should not raise a TypeError - #4796 six.text_type(string_concat(...)) should not raise a TypeError - #4796
""" """
import django.utils.translation self.assertEqual('django', six.text_type(string_concat("dja", "ngo")))
self.assertEqual('django', six.text_type(django.utils.translation.string_concat("dja", "ngo")))
def test_safe_status(self): def test_safe_status(self):
""" """
@ -335,7 +336,6 @@ class TranslationTests(TransRealMixin, TestCase):
class TranslationThreadSafetyTests(TestCase): class TranslationThreadSafetyTests(TestCase):
"""Specifically not using TransRealMixin here to test threading."""
def setUp(self): def setUp(self):
self._old_language = get_language() self._old_language = get_language()
@ -367,7 +367,7 @@ class TranslationThreadSafetyTests(TestCase):
@override_settings(USE_L10N=True) @override_settings(USE_L10N=True)
class FormattingTests(TransRealMixin, TestCase): class FormattingTests(TestCase):
def setUp(self): def setUp(self):
super(FormattingTests, self).setUp() super(FormattingTests, self).setUp()
@ -806,7 +806,7 @@ class FormattingTests(TransRealMixin, TestCase):
) )
class MiscTests(TransRealMixin, TestCase): class MiscTests(TestCase):
def setUp(self): def setUp(self):
super(MiscTests, self).setUp() super(MiscTests, self).setUp()
@ -1019,7 +1019,7 @@ class MiscTests(TransRealMixin, TestCase):
self.assertNotEqual('pt-br', g(r)) self.assertNotEqual('pt-br', g(r))
class ResolutionOrderI18NTests(TransRealMixin, TestCase): class ResolutionOrderI18NTests(TestCase):
def setUp(self): def setUp(self):
super(ResolutionOrderI18NTests, self).setUp() super(ResolutionOrderI18NTests, self).setUp()
@ -1037,20 +1037,21 @@ class ResolutionOrderI18NTests(TransRealMixin, TestCase):
class AppResolutionOrderI18NTests(ResolutionOrderI18NTests): class AppResolutionOrderI18NTests(ResolutionOrderI18NTests):
@override_settings(LANGUAGE_CODE='de')
def test_app_translation(self): def test_app_translation(self):
# Original translation. # Original translation.
self.assertUgettext('Date/time', 'Datum/Zeit') self.assertUgettext('Date/time', 'Datum/Zeit')
# Different translation. # Different translation.
with self.modify_settings(INSTALLED_APPS={'append': 'i18n.resolution'}): with self.modify_settings(INSTALLED_APPS={'append': 'i18n.resolution'}):
self.flush_caches() # Force refreshing translations.
activate('de') activate('de')
# Doesn't work because it's added later in the list. # Doesn't work because it's added later in the list.
self.assertUgettext('Date/time', 'Datum/Zeit') self.assertUgettext('Date/time', 'Datum/Zeit')
with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.admin.apps.SimpleAdminConfig'}): with self.modify_settings(INSTALLED_APPS={'remove': 'django.contrib.admin.apps.SimpleAdminConfig'}):
self.flush_caches() # Force refreshing translations.
activate('de') activate('de')
# Unless the original is removed from the list. # Unless the original is removed from the list.
@ -1109,7 +1110,7 @@ class TestLanguageInfo(TestCase):
six.assertRaisesRegex(self, KeyError, r"Unknown language code xx-xx and xx\.", get_language_info, 'xx-xx') six.assertRaisesRegex(self, KeyError, r"Unknown language code xx-xx and xx\.", get_language_info, 'xx-xx')
class MultipleLocaleActivationTests(TransRealMixin, TestCase): class MultipleLocaleActivationTests(TestCase):
""" """
Tests for template rendering behavior when multiple locales are activated Tests for template rendering behavior when multiple locales are activated
during the lifetime of the same process. during the lifetime of the same process.
@ -1243,7 +1244,7 @@ class MultipleLocaleActivationTests(TransRealMixin, TestCase):
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
), ),
) )
class LocaleMiddlewareTests(TransRealMixin, TestCase): class LocaleMiddlewareTests(TestCase):
urls = 'i18n.urls' urls = 'i18n.urls'
@ -1281,7 +1282,7 @@ class LocaleMiddlewareTests(TransRealMixin, TestCase):
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
), ),
) )
class CountrySpecificLanguageTests(TransRealMixin, TestCase): class CountrySpecificLanguageTests(TestCase):
urls = 'i18n.urls' urls = 'i18n.urls'