Ticket #15561: 15561-i18n-tests.diff

File 15561-i18n-tests.diff, 11.3 KB (added by Claude Paroz, 14 years ago)

As an example, apply new test utils to i18n tests

  • tests/regressiontests/i18n/test_warnings.py

    diff --git a/tests/regressiontests/i18n/test_warnings.py b/tests/regressiontests/i18n/test_warnings.py
    index 72255e2..d6f2174 100644
    a b import warnings  
    33
    44import django
    55from django.conf import settings
    6 from django.test.utils import get_warnings_state, restore_warnings_state
     6from django.test.utils import get_warnings_state, restore_warnings_state, alter_django_settings, restore_django_settings
    77from django.utils.translation import _trans
    88from django.utils.unittest import TestCase
    99
    class DeprecationWarningTests(TestCase):  
    1212
    1313    def setUp(self):
    1414        self.warning_state = get_warnings_state()
    15         self.old_settings_module = settings.SETTINGS_MODULE
    16         settings.SETTINGS_MODULE = 'regressiontests'
    17         self.old_locale_paths = settings.LOCALE_PATHS
     15        self.settings_state = alter_django_settings(SETTINGS_MODULE='regressiontests', LOCALE_PATHS=settings.LOCALE_PATHS)
    1816
    1917    def tearDown(self):
    2018        restore_warnings_state(self.warning_state)
    21         settings.SETTINGS_MODULE = self.old_settings_module
    22         settings.LOCALE_PATHS = self.old_locale_paths
     19        restore_django_settings(self.settings_state)
    2320
    2421    def test_warn_if_project_has_locale_subdir(self):
    2522        """Test that PendingDeprecationWarning is generated when a deprecated project level locale/ subdir is present."""
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index 88c1c13..7ba861c 100644
    a b from threading import local  
    88
    99from django.conf import settings
    1010from django.template import Template, Context
     11from django.test.decorators import with_django_settings
     12from django.test.utils import alter_django_settings, restore_django_settings
    1113from django.utils.formats import (get_format, date_format, time_format,
    1214    localize, localize_input, iter_format_modules, get_format_modules)
    1315from django.utils.importlib import import_module
    class TranslationTests(TestCase):  
    5961        s2 = pickle.loads(pickle.dumps(s1))
    6062        self.assertEqual(unicode(s2), "test")
    6163
     64    @with_django_settings(LOCALE_PATHS=settings.LOCALE_PATHS + (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),))
    6265    def test_pgettext(self):
    6366        # Reset translation catalog to include other/locale/de
    64         self.old_locale_paths = settings.LOCALE_PATHS
    65         settings.LOCALE_PATHS += (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),)
    6667        from django.utils.translation import trans_real
    6768        trans_real._active = local()
    6869        trans_real._translations = {}
    class TranslationTests(TestCase):  
    7374        self.assertEqual(pgettext("verb", "May"), u"Kann")
    7475        self.assertEqual(npgettext("search", "%d result", "%d results", 4) % 4, u"4 Resultate")
    7576
    76         settings.LOCALE_PATHS = self.old_locale_paths
    77 
    7877    def test_string_concat(self):
    7978        """
    8079        unicode(string_concat(...)) should not raise a TypeError - #4796
    class TranslationTests(TestCase):  
    135134class FormattingTests(TestCase):
    136135
    137136    def setUp(self):
    138         self.use_i18n = settings.USE_I18N
    139         self.use_l10n = settings.USE_L10N
    140         self.use_thousand_separator = settings.USE_THOUSAND_SEPARATOR
    141         self.thousand_separator = settings.THOUSAND_SEPARATOR
    142         self.number_grouping = settings.NUMBER_GROUPING
    143137        self.n = decimal.Decimal('66666.666')
    144138        self.f = 99999.999
    145139        self.d = datetime.date(2009, 12, 31)
    class FormattingTests(TestCase):  
    155149            'l': self.l,
    156150        })
    157151
    158     def tearDown(self):
    159         # Restore defaults
    160         settings.USE_I18N = self.use_i18n
    161         settings.USE_L10N = self.use_l10n
    162         settings.USE_THOUSAND_SEPARATOR = self.use_thousand_separator
    163         settings.THOUSAND_SEPARATOR = self.thousand_separator
    164         settings.NUMBER_GROUPING = self.number_grouping
    165 
     152    @with_django_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=settings.USE_THOUSAND_SEPARATOR)
    166153    def test_locale_independent(self):
    167154        """
    168155        Localization of numbers
    169156        """
    170         settings.USE_L10N = True
    171157        settings.USE_THOUSAND_SEPARATOR = False
    172158        self.assertEqual(u'66666.66', nformat(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=','))
    173159        self.assertEqual(u'66666A6', nformat(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B'))
    class FormattingTests(TestCase):  
    183169        self.assertEqual(u'31.12.2009 в 20:50', Template('{{ dt|date:"d.m.Y в H:i" }}').render(self.ctxt))
    184170        self.assertEqual(u'⌚ 10:15', Template('{{ t|time:"⌚ H:i" }}').render(self.ctxt))
    185171
     172    @with_django_settings(USE_L10N=False)
    186173    def test_l10n_disabled(self):
    187174        """
    188175        Catalan locale with format i18n disabled translations will be used,
    189176        but not formats
    190177        """
    191         settings.USE_L10N = False
    192178        activate('ca')
    193179        try:
    194180            self.assertEqual(u'N j, Y', get_format('DATE_FORMAT'))
    class FormattingTests(TestCase):  
    253239        finally:
    254240            deactivate()
    255241
     242    @with_django_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=settings.USE_THOUSAND_SEPARATOR)
    256243    def test_l10n_enabled(self):
    257         settings.USE_L10N = True
    258244        # Catalan locale
    259245        activate('ca')
    260246        try:
    class FormattingTests(TestCase):  
    420406        finally:
    421407            deactivate()
    422408
     409    @with_django_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True)
    423410    def test_sub_locales(self):
    424411        """
    425412        Check if sublocales fall back to the main locale
    426413        """
    427         settings.USE_L10N = True
    428414        activate('de-at')
    429         settings.USE_THOUSAND_SEPARATOR = True
    430415        try:
    431416            self.assertEqual(u'66.666,666', Template('{{ n }}').render(self.ctxt))
    432417        finally:
    class FormattingTests(TestCase):  
    438423        finally:
    439424            deactivate()
    440425
     426    @with_django_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=settings.USE_THOUSAND_SEPARATOR)
    441427    def test_localized_input(self):
    442428        """
    443429        Tests if form input is correctly localized
    444430        """
    445         settings.USE_L10N = True
    446431        activate('de-at')
    447432        try:
    448433            form6 = CompanyForm({
    class FormattingTests(TestCase):  
    464449        finally:
    465450            deactivate()
    466451
     452    @with_django_settings(USE_L10N=True, FORMAT_MODULE_PATH=settings.FORMAT_MODULE_PATH)
    467453    def test_iter_format_modules(self):
    468454        """
    469455        Tests the iter_format_modules function.
    470456        """
    471457        activate('de-at')
    472         old_format_module_path = settings.FORMAT_MODULE_PATH
    473458        try:
    474             settings.USE_L10N = True
    475459            de_format_mod = import_module('django.conf.locale.de.formats')
    476460            self.assertEqual(list(iter_format_modules('de')), [de_format_mod])
    477461            settings.FORMAT_MODULE_PATH = 'regressiontests.i18n.other.locale'
    478462            test_de_format_mod = import_module('regressiontests.i18n.other.locale.de.formats')
    479463            self.assertEqual(list(iter_format_modules('de')), [test_de_format_mod, de_format_mod])
    480464        finally:
    481             settings.FORMAT_MODULE_PATH = old_format_module_path
    482465            deactivate()
    483466
     467    @with_django_settings(USE_L10N=True)
    484468    def test_iter_format_modules_stability(self):
    485469        """
    486470        Tests the iter_format_modules function always yields format modules in
    487471        a stable and correct order in presence of both base ll and ll_CC formats.
    488472        """
    489         settings.USE_L10N = True
    490473        en_format_mod = import_module('django.conf.locale.en.formats')
    491474        en_gb_format_mod = import_module('django.conf.locale.en_GB.formats')
    492475        self.assertEqual(list(iter_format_modules('en-gb')), [en_gb_format_mod, en_format_mod])
    493476
     477    @with_django_settings(USE_L10N=True, FORMAT_MODULE_PATH='regressiontests.i18n.other.locale')
    494478    def test_get_format_modules_stability(self):
    495479        activate('de')
    496         old_format_module_path = settings.FORMAT_MODULE_PATH
    497         settings.FORMAT_MODULE_PATH = 'regressiontests.i18n.other.locale'
    498480        try:
    499             settings.USE_L10N = True
    500481            old = "%r" % get_format_modules(reverse=True)
    501482            new = "%r" % get_format_modules(reverse=True) # second try
    502483            self.assertEqual(new, old, 'Value returned by get_formats_modules() must be preserved between calls.')
    503484        finally:
    504             settings.FORMAT_MODULE_PATH = old_format_module_path
    505485            deactivate()
    506486
    507487    def test_localize_templatetag_and_filter(self):
    class ResolutionOrderI18NTests(TestCase):  
    675655class AppResolutionOrderI18NTests(ResolutionOrderI18NTests):
    676656
    677657    def setUp(self):
    678         self.old_installed_apps = settings.INSTALLED_APPS
    679         settings.INSTALLED_APPS = ['regressiontests.i18n.resolution'] + list(settings.INSTALLED_APPS)
     658        self.settings_state = alter_django_settings(
     659            INSTALLED_APPS=['regressiontests.i18n.resolution'] + list(settings.INSTALLED_APPS))
    680660        super(AppResolutionOrderI18NTests, self).setUp()
    681661
    682662    def tearDown(self):
    683         settings.INSTALLED_APPS = self.old_installed_apps
     663        restore_django_settings(self.settings_state)
    684664        super(AppResolutionOrderI18NTests, self).tearDown()
    685665
    686666    def test_app_translation(self):
    class AppResolutionOrderI18NTests(ResolutionOrderI18NTests):  
    689669class LocalePathsResolutionOrderI18NTests(ResolutionOrderI18NTests):
    690670
    691671    def setUp(self):
    692         self.old_locale_paths = settings.LOCALE_PATHS
    693         settings.LOCALE_PATHS += (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),)
     672        self.settings_state = alter_django_settings(
     673            LOCALE_PATHS=settings.LOCALE_PATHS + (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),),
     674            INSTALLED_APPS=list(settings.INSTALLED_APPS) + ['regressiontests.i18n.resolution'])
    694675        super(LocalePathsResolutionOrderI18NTests, self).setUp()
    695676
    696677    def tearDown(self):
    697         settings.LOCALE_PATHS = self.old_locale_paths
     678        restore_django_settings(self.settings_state)
    698679        super(LocalePathsResolutionOrderI18NTests, self).tearDown()
    699680
    700681    def test_locale_paths_translation(self):
    701682        self.assertUgettext('Time', 'LOCALE_PATHS')
    702683
    703684    def test_locale_paths_override_app_translation(self):
    704         old_installed_apps = settings.INSTALLED_APPS
    705         settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) + ['regressiontests.i18n.resolution']
    706         try:
    707             self.assertUgettext('Time', 'LOCALE_PATHS')
    708         finally:
    709             settings.INSTALLED_APPS = old_installed_apps
     685        self.assertUgettext('Time', 'LOCALE_PATHS')
    710686
     687    @with_django_settings(SETTINGS_MODULE='regressiontests')
    711688    def test_locale_paths_override_project_translation(self):
    712         old_settings_module = settings.SETTINGS_MODULE
    713         settings.SETTINGS_MODULE = 'regressiontests'
    714         try:
    715             self.assertUgettext('Date/time', 'LOCALE_PATHS')
    716         finally:
    717             settings.SETTINGS_MODULE = old_settings_module
     689        self.assertUgettext('Date/time', 'LOCALE_PATHS')
    718690
    719691class ProjectResolutionOrderI18NTests(ResolutionOrderI18NTests):
    720692
Back to Top