test_setlocale.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import ioex
  4. import locale
  5. import datetime
  6. @pytest.mark.parametrize(('locale_code'), [
  7. 'unknown_??.utf8',
  8. ])
  9. def test_setlocale_unsupported(locale_code):
  10. with pytest.raises(ioex.UnsupportedLocaleSettingError):
  11. with ioex.setlocale(locale_code):
  12. pass
  13. def test_setlocale_unsupported_inheritance():
  14. assert issubclass(ioex.UnsupportedLocaleSettingError, locale.Error)
  15. @pytest.mark.parametrize(('dt', 'dt_format', 'locale_code', 'expected_string'), [
  16. [datetime.datetime(2016, 7, 23, 1, 7, 12), '%x', 'de_DE.utf8', '23.07.2016'],
  17. [datetime.datetime(2016, 7, 23, 1, 7, 12), '%X', 'de_DE.utf8', '01:07:12'],
  18. [datetime.datetime(2016, 7, 23, 1, 7, 12), '%x', 'en_US.utf8', '07/23/2016'],
  19. [datetime.datetime(2016, 7, 23, 1, 7, 12), '%X', 'en_US.utf8', '01:07:12 AM'],
  20. [datetime.datetime(2016, 7, 23, 1, 7, 12), '%x', 'it_IT.utf8', '23/07/2016'],
  21. [datetime.datetime(2016, 7, 23, 1, 7, 12), '%X', 'it_IT.utf8', '01:07:12'],
  22. [datetime.datetime(2016, 7, 23, 1, 7, 12), '%x', 'zh_CN.utf8', '2016年07月23日'],
  23. [datetime.datetime(2016, 7, 23, 1, 7, 12), '%X', 'zh_CN.utf8', '01时07分12秒'],
  24. ])
  25. def test_setlocale_strtime(dt, dt_format, locale_code, expected_string):
  26. try:
  27. with ioex.setlocale(locale_code):
  28. assert dt.strftime(dt_format) == expected_string
  29. except ioex.UnsupportedLocaleSettingError as ex:
  30. pytest.skip('locale %s unsupported' % locale_code)
  31. @pytest.mark.parametrize(('source', 'locale_code', 'expected_target'), [
  32. ['12,34', 'de_DE.utf8', 12.34],
  33. ['12.34', 'en_US.utf8', 12.34],
  34. ['12,34', 'it_IT.utf8', 12.34],
  35. ['12.34', 'zh_CN.utf8', 12.34],
  36. ['1.234,56', 'de_DE.utf8', 1234.56],
  37. ['1,234.56', 'en_US.utf8', 1234.56],
  38. ['1.234,56', 'it_IT.utf8', 1234.56],
  39. ['1,234.56', 'zh_CN.utf8', 1234.56],
  40. ])
  41. def test_setlocale_atof(source, locale_code, expected_target):
  42. try:
  43. with ioex.setlocale(locale_code):
  44. assert expected_target == locale.atof(source)
  45. except ioex.UnsupportedLocaleSettingError as ex:
  46. pytest.skip('locale %s unsupported' % locale_code)