mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Added patch_formats utility for i18n formatting tests
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| # -*- encoding: utf-8 -*- | ||||
| from __future__ import unicode_literals | ||||
|  | ||||
| from contextlib import contextmanager | ||||
| import datetime | ||||
| import decimal | ||||
| from importlib import import_module | ||||
| @@ -42,6 +43,18 @@ extended_locale_paths = settings.LOCALE_PATHS + ( | ||||
|     os.path.join(here, 'other', 'locale'), | ||||
| ) | ||||
|  | ||||
| @contextmanager | ||||
| def patch_formats(lang, **settings): | ||||
|     from django.utils.formats import _format_cache | ||||
|  | ||||
|     # Populate _format_cache with temporary values | ||||
|     for key, value in settings.items(): | ||||
|         _format_cache[(key, lang)] = value | ||||
|     try: | ||||
|         yield | ||||
|     finally: | ||||
|         reset_format_cache() | ||||
|  | ||||
|  | ||||
| class TranslationTests(TestCase): | ||||
|  | ||||
| @@ -482,31 +495,17 @@ class FormattingTests(TestCase): | ||||
|         conditional test (e.g. 0 or empty string). | ||||
|         Refs #16938. | ||||
|         """ | ||||
|         from django.conf.locale.fr import formats as fr_formats | ||||
|         with patch_formats('fr', THOUSAND_SEPARATOR='', FIRST_DAY_OF_WEEK=0): | ||||
|             with translation.override('fr'): | ||||
|                 with self.settings(USE_THOUSAND_SEPARATOR=True, THOUSAND_SEPARATOR='!'): | ||||
|                     self.assertEqual('', get_format('THOUSAND_SEPARATOR')) | ||||
|                     # Even a second time (after the format has been cached)... | ||||
|                     self.assertEqual('', get_format('THOUSAND_SEPARATOR')) | ||||
|  | ||||
|         # Back up original formats | ||||
|         backup_THOUSAND_SEPARATOR = fr_formats.THOUSAND_SEPARATOR | ||||
|         backup_FIRST_DAY_OF_WEEK = fr_formats.FIRST_DAY_OF_WEEK | ||||
|  | ||||
|         # Set formats that would get interpreted as False in a conditional test | ||||
|         fr_formats.THOUSAND_SEPARATOR = '' | ||||
|         fr_formats.FIRST_DAY_OF_WEEK = 0 | ||||
|  | ||||
|         reset_format_cache() | ||||
|         with translation.override('fr'): | ||||
|             with self.settings(USE_THOUSAND_SEPARATOR=True, THOUSAND_SEPARATOR='!'): | ||||
|                 self.assertEqual('', get_format('THOUSAND_SEPARATOR')) | ||||
|                 # Even a second time (after the format has been cached)... | ||||
|                 self.assertEqual('', get_format('THOUSAND_SEPARATOR')) | ||||
|  | ||||
|             with self.settings(FIRST_DAY_OF_WEEK=1): | ||||
|                 self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK')) | ||||
|                 # Even a second time (after the format has been cached)... | ||||
|                 self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK')) | ||||
|  | ||||
|         # Restore original formats | ||||
|         fr_formats.THOUSAND_SEPARATOR = backup_THOUSAND_SEPARATOR | ||||
|         fr_formats.FIRST_DAY_OF_WEEK = backup_FIRST_DAY_OF_WEEK | ||||
|                 with self.settings(FIRST_DAY_OF_WEEK=1): | ||||
|                     self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK')) | ||||
|                     # Even a second time (after the format has been cached)... | ||||
|                     self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK')) | ||||
|  | ||||
|     def test_l10n_enabled(self): | ||||
|         self.maxDiff = 3000 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user