Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added TransRealMixin to fix i18n global state pollution in the test s…

…uite
  • Loading branch information...
commit e80636b6683893d40e171745a4ef0a2f1a09cc82 1 parent 413735b
Łukasz Langa authored May 19, 2013
17  tests/i18n/__init__.py
... ...
@@ -0,0 +1,17 @@
  1
+from threading import local
  2
+
  3
+
  4
+class TransRealMixin(object):
  5
+    """This is the only way to reset the translation machinery. Otherwise
  6
+    the test suite occasionally fails because of global state pollution
  7
+    between tests."""
  8
+    def flush_caches(self):
  9
+        from django.utils.translation import trans_real
  10
+        trans_real._translations = {}
  11
+        trans_real._active = local()
  12
+        trans_real._default = None
  13
+        trans_real._accepted = {}
  14
+
  15
+    def tearDown(self):
  16
+        self.flush_caches()
  17
+        super(TransRealMixin, self).tearDown()
4  tests/i18n/contenttypes/tests.py
@@ -10,6 +10,8 @@
10 10
 from django.utils import six
11 11
 from django.utils import translation
12 12
 
  13
+from i18n import TransRealMixin
  14
+
13 15
 
14 16
 @override_settings(
15 17
     USE_I18N=True,
@@ -22,7 +24,7 @@
22 24
         ('fr', 'French'),
23 25
     ),
24 26
 )
25  
-class ContentTypeTests(TestCase):
  27
+class ContentTypeTests(TransRealMixin, TestCase):
26 28
     def test_verbose_name(self):
27 29
         company_type = ContentType.objects.get(app_label='i18n', model='company')
28 30
         with translation.override('en'):
31  tests/i18n/tests.py
@@ -44,6 +44,7 @@
44 44
     from .commands.compilation import (PoFileTests, PoFileContentsTests,
45 45
         PercentRenderingTests, MultipleLocaleCompilationTests,
46 46
         CompilationErrorHandling)
  47
+from . import TransRealMixin
47 48
 from .forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm
48 49
 from .models import Company, TestModel
49 50
 
@@ -53,7 +54,8 @@
53 54
     os.path.join(here, 'other', 'locale'),
54 55
 )
55 56
 
56  
-class TranslationTests(TestCase):
  57
+
  58
+class TranslationTests(TransRealMixin, TestCase):
57 59
 
58 60
     def test_override(self):
59 61
         activate('de')
@@ -335,6 +337,8 @@ def test_bad_placeholder_2(self):
335 337
 
336 338
 
337 339
 class TranslationThreadSafetyTests(TestCase):
  340
+    """Specifically not using TransRealMixin here to test threading."""
  341
+
338 342
     def setUp(self):
339 343
         self._old_language = get_language()
340 344
         self._translations = trans_real._translations
@@ -365,9 +369,10 @@ def test_bug14894_translation_activate_thread_safety(self):
365 369
 
366 370
 
367 371
 @override_settings(USE_L10N=True)
368  
-class FormattingTests(TestCase):
  372
+class FormattingTests(TransRealMixin, TestCase):
369 373
 
370 374
     def setUp(self):
  375
+        super(FormattingTests, self).setUp()
371 376
         self.n = decimal.Decimal('66666.666')
372 377
         self.f = 99999.999
373 378
         self.d = datetime.date(2009, 12, 31)
@@ -769,9 +774,10 @@ def test_localize_templatetag_and_filter(self):
769 774
                 self.assertEqual(template2.render(context), output2)
770 775
                 self.assertEqual(template3.render(context), output3)
771 776
 
772  
-class MiscTests(TestCase):
  777
+class MiscTests(TransRealMixin, TestCase):
773 778
 
774 779
     def setUp(self):
  780
+        super(MiscTests, self).setUp()
775 781
         self.rf = RequestFactory()
776 782
 
777 783
     def test_parse_spec_http_header(self):
@@ -915,17 +921,15 @@ def test_percent_formatting_in_blocktrans(self):
915 921
             self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 4})), '%(percent)s% represents 4 objects')
916 922
 
917 923
 
918  
-class ResolutionOrderI18NTests(TestCase):
  924
+class ResolutionOrderI18NTests(TransRealMixin, TestCase):
919 925
 
920 926
     def setUp(self):
921  
-        # Okay, this is brutal, but we have no other choice to fully reset
922  
-        # the translation framework
923  
-        trans_real._active = local()
924  
-        trans_real._translations = {}
  927
+        super(ResolutionOrderI18NTests, self).setUp()
925 928
         activate('de')
926 929
 
927 930
     def tearDown(self):
928 931
         deactivate()
  932
+        super(ResolutionOrderI18NTests, self).tearDown()
929 933
 
930 934
     def assertUgettext(self, msgid, msgstr):
931 935
         result = ugettext(msgid)
@@ -998,15 +1002,17 @@ def test_unknown_language_code_and_country_code(self):
998 1002
         six.assertRaisesRegex(self, KeyError, r"Unknown language code xx-xx and xx\.", get_language_info, 'xx-xx')
999 1003
 
1000 1004
 
1001  
-class MultipleLocaleActivationTests(TestCase):
  1005
+class MultipleLocaleActivationTests(TransRealMixin, TestCase):
1002 1006
     """
1003 1007
     Tests for template rendering behavior when multiple locales are activated
1004 1008
     during the lifetime of the same process.
1005 1009
     """
1006 1010
     def setUp(self):
  1011
+        super(MultipleLocaleActivationTests, self).setUp()
1007 1012
         self._old_language = get_language()
1008 1013
 
1009 1014
     def tearDown(self):
  1015
+        super(MultipleLocaleActivationTests, self).tearDown()
1010 1016
         activate(self._old_language)
1011 1017
 
1012 1018
     def test_single_locale_activation(self):
@@ -1135,7 +1141,7 @@ def test_multiple_locale_direct_switch_btrans(self):
1135 1141
         'django.middleware.common.CommonMiddleware',
1136 1142
     ),
1137 1143
 )
1138  
-class LocaleMiddlewareTests(TestCase):
  1144
+class LocaleMiddlewareTests(TransRealMixin, TestCase):
1139 1145
 
1140 1146
     urls = 'i18n.urls'
1141 1147
 
@@ -1157,12 +1163,12 @@ def test_streaming_response(self):
1157 1163
         'django.middleware.common.CommonMiddleware',
1158 1164
     ),
1159 1165
 )
1160  
-class CountrySpecificLanguageTests(TestCase):
  1166
+class CountrySpecificLanguageTests(TransRealMixin, TestCase):
1161 1167
 
1162 1168
     urls = 'i18n.urls'
1163 1169
 
1164 1170
     def setUp(self):
1165  
-        trans_real._accepted = {}
  1171
+        super(CountrySpecificLanguageTests, self).setUp()
1166 1172
         self.rf = RequestFactory()
1167 1173
 
1168 1174
     def test_check_for_language(self):
@@ -1172,6 +1178,7 @@ def test_check_for_language(self):
1172 1178
 
1173 1179
 
1174 1180
     def test_get_language_from_request(self):
  1181
+        # issue 19919
1175 1182
         r = self.rf.get('/')
1176 1183
         r.COOKIES = {}
1177 1184
         r.META = {'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,bg;q=0.6,ru;q=0.4'}

0 notes on commit e80636b

Please sign in to comment.
Something went wrong with that request. Please try again.