From 51d2e1fb233b5507bf14300787939717c4d93834 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Tue, 22 Oct 2013 20:38:11 -0300 Subject: [PATCH] Fixed #21307 -- Moved TransRealMixin to django.test.utils. --- django/contrib/humanize/tests.py | 4 +--- django/test/utils.py | 18 ++++++++++++++++++ tests/defaultfilters/tests.py | 3 +-- tests/forms_tests/tests/test_regressions.py | 2 +- tests/i18n/__init__.py | 18 ------------------ tests/i18n/contenttypes/tests.py | 4 +--- tests/i18n/tests.py | 3 +-- tests/template_tests/tests.py | 4 +--- 8 files changed, 24 insertions(+), 32 deletions(-) diff --git a/django/contrib/humanize/tests.py b/django/contrib/humanize/tests.py index 556c2ca2d0..d2bb4c034e 100644 --- a/django/contrib/humanize/tests.py +++ b/django/contrib/humanize/tests.py @@ -12,14 +12,12 @@ from django.conf import settings from django.contrib.humanize.templatetags import humanize from django.template import Template, Context, defaultfilters from django.test import TestCase -from django.test.utils import override_settings +from django.test.utils import override_settings, TransRealMixin from django.utils.html import escape from django.utils.timezone import utc, get_fixed_timezone from django.utils import translation from django.utils.translation import ugettext as _ -from i18n import TransRealMixin - # Mock out datetime in some tests so they don't fail occasionally when they # run too slow. Use a fixed datetime for datetime.now(). DST change in diff --git a/django/test/utils.py b/django/test/utils.py index ae045a5283..9126142cf2 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -2,6 +2,7 @@ from contextlib import contextmanager import logging import re import sys +from threading import local import time from unittest import skipUnless import warnings @@ -415,6 +416,23 @@ def patch_logger(logger_name, log_level): 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._accepted = {} + trans_real._checked_languages = {} + + def tearDown(self): + self.flush_caches() + super(TransRealMixin, self).tearDown() + + # 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 # don't enforce a specific timezone (with timezone.override or equivalent), diff --git a/tests/defaultfilters/tests.py b/tests/defaultfilters/tests.py index 763628fe0e..c3288162e0 100644 --- a/tests/defaultfilters/tests.py +++ b/tests/defaultfilters/tests.py @@ -17,13 +17,12 @@ 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 from django.utils.encoding import python_2_unicode_compatible -from i18n import TransRealMixin - class DefaultFiltersTests(TestCase): diff --git a/tests/forms_tests/tests/test_regressions.py b/tests/forms_tests/tests/test_regressions.py index e41e929fab..39459aa472 100644 --- a/tests/forms_tests/tests/test_regressions.py +++ b/tests/forms_tests/tests/test_regressions.py @@ -12,7 +12,7 @@ from django.test import TestCase from django.utils.translation import ugettext_lazy, override from forms_tests.models import Cheese -from i18n import TransRealMixin +from django.test.utils import TransRealMixin class FormsRegressionsTestCase(TransRealMixin, TestCase): diff --git a/tests/i18n/__init__.py b/tests/i18n/__init__.py index c5aaa31fe3..e69de29bb2 100644 --- a/tests/i18n/__init__.py +++ b/tests/i18n/__init__.py @@ -1,18 +0,0 @@ -from threading import local - - -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._accepted = {} - trans_real._checked_languages = {} - - def tearDown(self): - self.flush_caches() - super(TransRealMixin, self).tearDown() diff --git a/tests/i18n/contenttypes/tests.py b/tests/i18n/contenttypes/tests.py index cbac9ec5da..c48f8fc6dd 100644 --- a/tests/i18n/contenttypes/tests.py +++ b/tests/i18n/contenttypes/tests.py @@ -5,13 +5,11 @@ import os from django.contrib.contenttypes.models import ContentType from django.test import TestCase -from django.test.utils import override_settings +from django.test.utils import override_settings, TransRealMixin from django.utils._os import upath from django.utils import six from django.utils import translation -from i18n import TransRealMixin - @override_settings( USE_I18N=True, diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 1e16caeaad..1dbd502ed8 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -13,7 +13,7 @@ from django.core.management.utils import find_command from django.template import Template, Context from django.template.base import TemplateSyntaxError from django.test import TestCase, RequestFactory -from django.test.utils import override_settings +from django.test.utils import override_settings, TransRealMixin from django.utils import translation from django.utils.formats import (get_format, date_format, time_format, localize, localize_input, iter_format_modules, get_format_modules, @@ -44,7 +44,6 @@ if find_command('msgfmt'): from .commands.compilation import (PoFileTests, PoFileContentsTests, PercentRenderingTests, MultipleLocaleCompilationTests, CompilationErrorHandling) -from . import TransRealMixin from .forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm from .models import Company, TestModel diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index ad6b4433da..55bb2e7b26 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -22,7 +22,7 @@ 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) + restore_template_loaders, override_settings, TransRealMixin) from django.utils.encoding import python_2_unicode_compatible from django.utils.formats import date_format from django.utils._os import upath @@ -31,8 +31,6 @@ from django.utils.safestring import mark_safe from django.utils import six from django.utils.six.moves.urllib.parse import urljoin -from i18n import TransRealMixin - # NumPy installed? try: import numpy