diff --git a/tests/regressiontests/i18n/test_warnings.py b/tests/regressiontests/i18n/test_warnings.py
index 72255e2..d6f2174 100644
a
|
b
|
import warnings
|
3 | 3 | |
4 | 4 | import django |
5 | 5 | from django.conf import settings |
6 | | from django.test.utils import get_warnings_state, restore_warnings_state |
| 6 | from django.test.utils import get_warnings_state, restore_warnings_state, alter_django_settings, restore_django_settings |
7 | 7 | from django.utils.translation import _trans |
8 | 8 | from django.utils.unittest import TestCase |
9 | 9 | |
… |
… |
class DeprecationWarningTests(TestCase):
|
12 | 12 | |
13 | 13 | def setUp(self): |
14 | 14 | 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) |
18 | 16 | |
19 | 17 | def tearDown(self): |
20 | 18 | 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) |
23 | 20 | |
24 | 21 | def test_warn_if_project_has_locale_subdir(self): |
25 | 22 | """Test that PendingDeprecationWarning is generated when a deprecated project level locale/ subdir is present.""" |
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
index 88c1c13..7ba861c 100644
a
|
b
|
from threading import local
|
8 | 8 | |
9 | 9 | from django.conf import settings |
10 | 10 | from django.template import Template, Context |
| 11 | from django.test.decorators import with_django_settings |
| 12 | from django.test.utils import alter_django_settings, restore_django_settings |
11 | 13 | from django.utils.formats import (get_format, date_format, time_format, |
12 | 14 | localize, localize_input, iter_format_modules, get_format_modules) |
13 | 15 | from django.utils.importlib import import_module |
… |
… |
class TranslationTests(TestCase):
|
59 | 61 | s2 = pickle.loads(pickle.dumps(s1)) |
60 | 62 | self.assertEqual(unicode(s2), "test") |
61 | 63 | |
| 64 | @with_django_settings(LOCALE_PATHS=settings.LOCALE_PATHS + (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),)) |
62 | 65 | def test_pgettext(self): |
63 | 66 | # 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'),) |
66 | 67 | from django.utils.translation import trans_real |
67 | 68 | trans_real._active = local() |
68 | 69 | trans_real._translations = {} |
… |
… |
class TranslationTests(TestCase):
|
73 | 74 | self.assertEqual(pgettext("verb", "May"), u"Kann") |
74 | 75 | self.assertEqual(npgettext("search", "%d result", "%d results", 4) % 4, u"4 Resultate") |
75 | 76 | |
76 | | settings.LOCALE_PATHS = self.old_locale_paths |
77 | | |
78 | 77 | def test_string_concat(self): |
79 | 78 | """ |
80 | 79 | unicode(string_concat(...)) should not raise a TypeError - #4796 |
… |
… |
class TranslationTests(TestCase):
|
135 | 134 | class FormattingTests(TestCase): |
136 | 135 | |
137 | 136 | 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 |
143 | 137 | self.n = decimal.Decimal('66666.666') |
144 | 138 | self.f = 99999.999 |
145 | 139 | self.d = datetime.date(2009, 12, 31) |
… |
… |
class FormattingTests(TestCase):
|
155 | 149 | 'l': self.l, |
156 | 150 | }) |
157 | 151 | |
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) |
166 | 153 | def test_locale_independent(self): |
167 | 154 | """ |
168 | 155 | Localization of numbers |
169 | 156 | """ |
170 | | settings.USE_L10N = True |
171 | 157 | settings.USE_THOUSAND_SEPARATOR = False |
172 | 158 | self.assertEqual(u'66666.66', nformat(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=',')) |
173 | 159 | self.assertEqual(u'66666A6', nformat(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B')) |
… |
… |
class FormattingTests(TestCase):
|
183 | 169 | self.assertEqual(u'31.12.2009 в 20:50', Template('{{ dt|date:"d.m.Y в H:i" }}').render(self.ctxt)) |
184 | 170 | self.assertEqual(u'⌚ 10:15', Template('{{ t|time:"⌚ H:i" }}').render(self.ctxt)) |
185 | 171 | |
| 172 | @with_django_settings(USE_L10N=False) |
186 | 173 | def test_l10n_disabled(self): |
187 | 174 | """ |
188 | 175 | Catalan locale with format i18n disabled translations will be used, |
189 | 176 | but not formats |
190 | 177 | """ |
191 | | settings.USE_L10N = False |
192 | 178 | activate('ca') |
193 | 179 | try: |
194 | 180 | self.assertEqual(u'N j, Y', get_format('DATE_FORMAT')) |
… |
… |
class FormattingTests(TestCase):
|
253 | 239 | finally: |
254 | 240 | deactivate() |
255 | 241 | |
| 242 | @with_django_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=settings.USE_THOUSAND_SEPARATOR) |
256 | 243 | def test_l10n_enabled(self): |
257 | | settings.USE_L10N = True |
258 | 244 | # Catalan locale |
259 | 245 | activate('ca') |
260 | 246 | try: |
… |
… |
class FormattingTests(TestCase):
|
420 | 406 | finally: |
421 | 407 | deactivate() |
422 | 408 | |
| 409 | @with_django_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True) |
423 | 410 | def test_sub_locales(self): |
424 | 411 | """ |
425 | 412 | Check if sublocales fall back to the main locale |
426 | 413 | """ |
427 | | settings.USE_L10N = True |
428 | 414 | activate('de-at') |
429 | | settings.USE_THOUSAND_SEPARATOR = True |
430 | 415 | try: |
431 | 416 | self.assertEqual(u'66.666,666', Template('{{ n }}').render(self.ctxt)) |
432 | 417 | finally: |
… |
… |
class FormattingTests(TestCase):
|
438 | 423 | finally: |
439 | 424 | deactivate() |
440 | 425 | |
| 426 | @with_django_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=settings.USE_THOUSAND_SEPARATOR) |
441 | 427 | def test_localized_input(self): |
442 | 428 | """ |
443 | 429 | Tests if form input is correctly localized |
444 | 430 | """ |
445 | | settings.USE_L10N = True |
446 | 431 | activate('de-at') |
447 | 432 | try: |
448 | 433 | form6 = CompanyForm({ |
… |
… |
class FormattingTests(TestCase):
|
464 | 449 | finally: |
465 | 450 | deactivate() |
466 | 451 | |
| 452 | @with_django_settings(USE_L10N=True, FORMAT_MODULE_PATH=settings.FORMAT_MODULE_PATH) |
467 | 453 | def test_iter_format_modules(self): |
468 | 454 | """ |
469 | 455 | Tests the iter_format_modules function. |
470 | 456 | """ |
471 | 457 | activate('de-at') |
472 | | old_format_module_path = settings.FORMAT_MODULE_PATH |
473 | 458 | try: |
474 | | settings.USE_L10N = True |
475 | 459 | de_format_mod = import_module('django.conf.locale.de.formats') |
476 | 460 | self.assertEqual(list(iter_format_modules('de')), [de_format_mod]) |
477 | 461 | settings.FORMAT_MODULE_PATH = 'regressiontests.i18n.other.locale' |
478 | 462 | test_de_format_mod = import_module('regressiontests.i18n.other.locale.de.formats') |
479 | 463 | self.assertEqual(list(iter_format_modules('de')), [test_de_format_mod, de_format_mod]) |
480 | 464 | finally: |
481 | | settings.FORMAT_MODULE_PATH = old_format_module_path |
482 | 465 | deactivate() |
483 | 466 | |
| 467 | @with_django_settings(USE_L10N=True) |
484 | 468 | def test_iter_format_modules_stability(self): |
485 | 469 | """ |
486 | 470 | Tests the iter_format_modules function always yields format modules in |
487 | 471 | a stable and correct order in presence of both base ll and ll_CC formats. |
488 | 472 | """ |
489 | | settings.USE_L10N = True |
490 | 473 | en_format_mod = import_module('django.conf.locale.en.formats') |
491 | 474 | en_gb_format_mod = import_module('django.conf.locale.en_GB.formats') |
492 | 475 | self.assertEqual(list(iter_format_modules('en-gb')), [en_gb_format_mod, en_format_mod]) |
493 | 476 | |
| 477 | @with_django_settings(USE_L10N=True, FORMAT_MODULE_PATH='regressiontests.i18n.other.locale') |
494 | 478 | def test_get_format_modules_stability(self): |
495 | 479 | activate('de') |
496 | | old_format_module_path = settings.FORMAT_MODULE_PATH |
497 | | settings.FORMAT_MODULE_PATH = 'regressiontests.i18n.other.locale' |
498 | 480 | try: |
499 | | settings.USE_L10N = True |
500 | 481 | old = "%r" % get_format_modules(reverse=True) |
501 | 482 | new = "%r" % get_format_modules(reverse=True) # second try |
502 | 483 | self.assertEqual(new, old, 'Value returned by get_formats_modules() must be preserved between calls.') |
503 | 484 | finally: |
504 | | settings.FORMAT_MODULE_PATH = old_format_module_path |
505 | 485 | deactivate() |
506 | 486 | |
507 | 487 | def test_localize_templatetag_and_filter(self): |
… |
… |
class ResolutionOrderI18NTests(TestCase):
|
675 | 655 | class AppResolutionOrderI18NTests(ResolutionOrderI18NTests): |
676 | 656 | |
677 | 657 | 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)) |
680 | 660 | super(AppResolutionOrderI18NTests, self).setUp() |
681 | 661 | |
682 | 662 | def tearDown(self): |
683 | | settings.INSTALLED_APPS = self.old_installed_apps |
| 663 | restore_django_settings(self.settings_state) |
684 | 664 | super(AppResolutionOrderI18NTests, self).tearDown() |
685 | 665 | |
686 | 666 | def test_app_translation(self): |
… |
… |
class AppResolutionOrderI18NTests(ResolutionOrderI18NTests):
|
689 | 669 | class LocalePathsResolutionOrderI18NTests(ResolutionOrderI18NTests): |
690 | 670 | |
691 | 671 | 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']) |
694 | 675 | super(LocalePathsResolutionOrderI18NTests, self).setUp() |
695 | 676 | |
696 | 677 | def tearDown(self): |
697 | | settings.LOCALE_PATHS = self.old_locale_paths |
| 678 | restore_django_settings(self.settings_state) |
698 | 679 | super(LocalePathsResolutionOrderI18NTests, self).tearDown() |
699 | 680 | |
700 | 681 | def test_locale_paths_translation(self): |
701 | 682 | self.assertUgettext('Time', 'LOCALE_PATHS') |
702 | 683 | |
703 | 684 | 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') |
710 | 686 | |
| 687 | @with_django_settings(SETTINGS_MODULE='regressiontests') |
711 | 688 | 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') |
718 | 690 | |
719 | 691 | class ProjectResolutionOrderI18NTests(ResolutionOrderI18NTests): |
720 | 692 | |