2
0

test_formats.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """Tests for ``django/utils/formats.py``."""
  2. from django.test import TestCase
  3. from django.utils.formats import iter_format_modules
  4. class IterFormatModulesTestCase(TestCase):
  5. """Tests for the ``iter_format_modules`` method."""
  6. longMessage = True
  7. def test_returns_correct_default(self):
  8. """
  9. Should return default module when FORMAT_MODULE_PATH is not set.
  10. """
  11. result = list(iter_format_modules('en'))
  12. self.assertEqual(len(result), 1, msg=(
  13. "Should return only Django's default formats module."))
  14. self.assertEqual(
  15. result[0].__name__, 'django.conf.locale.en.formats', msg=(
  16. 'Should have added the language to the module path'))
  17. def test_with_setting_as_basestring(self):
  18. """
  19. Before ticket #20477 FORMAT_MODULE_PATH was supposed to be a string.
  20. This test ensures backwards compatibility.
  21. """
  22. with self.settings(
  23. FORMAT_MODULE_PATH='utils_tests.test_module.formats'):
  24. result = list(iter_format_modules('en'))
  25. self.assertEqual(len(result), 2, msg=(
  26. 'Should return both, the default value and the one from the'
  27. ' setting'))
  28. self.assertEqual(
  29. result[0].__name__,
  30. 'utils_tests.test_module.formats.en.formats',
  31. msg=('Should return the module from the setting first and'
  32. ' should have added the language to the module path'))
  33. def test_with_setting_as_list_of_strings(self):
  34. """
  35. After ticket #20477 FORMAT_MODULE_PATH can also be a list of strings.
  36. This tests verifies the new functionality.
  37. """
  38. FORMAT_MODULE_PATH = [
  39. 'utils_tests.test_module.formats',
  40. 'utils_tests.test_module.formats2',
  41. ]
  42. with self.settings(
  43. FORMAT_MODULE_PATH=FORMAT_MODULE_PATH):
  44. result = list(iter_format_modules('en'))
  45. self.assertEqual(len(result), 3, msg=(
  46. 'Should return the default value and the two values from the'
  47. ' setting'))
  48. self.assertEqual(
  49. result[0].__name__,
  50. 'utils_tests.test_module.formats.en.formats',
  51. msg=('Should return the values from the setting and add the'
  52. ' language to the module path'))