test_.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import pytz
  4. import ioex
  5. import locale
  6. import datetime
  7. @pytest.mark.parametrize(('locale_code'), [
  8. 'unknown_??.utf8',
  9. ])
  10. def test_setlocale_unsupported(locale_code):
  11. with pytest.raises(ioex.UnsupportedLocaleSettingError):
  12. with ioex.setlocale(locale_code):
  13. pass
  14. def test_setlocale_unsupported_inheritance():
  15. assert issubclass(ioex.UnsupportedLocaleSettingError, locale.Error)
  16. @pytest.mark.parametrize(('dt', 'dt_format', 'locale_code', 'expected_string'), [
  17. [datetime.datetime(2016, 07, 23, 1, 7, 12), '%x', 'de_DE.utf8', u'23.07.2016'],
  18. [datetime.datetime(2016, 07, 23, 1, 7, 12), '%X', 'de_DE.utf8', u'01:07:12'],
  19. [datetime.datetime(2016, 07, 23, 1, 7, 12), '%x', 'en_US.utf8', u'07/23/2016'],
  20. [datetime.datetime(2016, 07, 23, 1, 7, 12), '%X', 'en_US.utf8', u'01:07:12 AM'],
  21. [datetime.datetime(2016, 07, 23, 1, 7, 12), '%x', 'it_IT.utf8', u'23/07/2016'],
  22. [datetime.datetime(2016, 07, 23, 1, 7, 12), '%X', 'it_IT.utf8', u'01:07:12'],
  23. [datetime.datetime(2016, 07, 23, 1, 7, 12), '%x', 'zh_CN.utf8', u'2016年07月23日'],
  24. [datetime.datetime(2016, 07, 23, 1, 7, 12), '%X', 'zh_CN.utf8', u'01时07分12秒'],
  25. ])
  26. def test_setlocale_strtime(dt, dt_format, locale_code, expected_string):
  27. try:
  28. with ioex.setlocale(locale_code):
  29. assert dt.strftime(dt_format).decode('utf-8') == expected_string
  30. except ioex.UnsupportedLocaleSettingError, ex:
  31. pytest.skip('locale %s unsupported' % locale_code)
  32. @pytest.mark.parametrize(('start', 'end'), [
  33. [datetime.datetime(2016, 7, 24, 12, 21), datetime.datetime(2016, 7, 24, 12, 22)],
  34. [None, datetime.datetime(2016, 7, 24, 12, 22)],
  35. [datetime.datetime(2016, 7, 24, 12, 21), None],
  36. [None, None],
  37. ])
  38. def test_dateperiod_init(start, end):
  39. p = ioex.DatePeriod(start = start, end = end)
  40. assert p.start == start
  41. assert p.end == end
  42. @pytest.mark.parametrize(('start', 'end'), [
  43. [';-)', datetime.datetime(2016, 7, 24, 12, 22)],
  44. [datetime.datetime(2016, 7, 24, 12, 22), ';-)'],
  45. ])
  46. def test_dateperiod_init_fail(start, end):
  47. with pytest.raises(TypeError):
  48. ioex.DatePeriod(start = start, end = end)
  49. @pytest.mark.parametrize(('start'), [
  50. datetime.datetime(2016, 7, 24, 12, 21),
  51. None,
  52. ])
  53. def test_dateperiod_set_start(start):
  54. p = ioex.DatePeriod()
  55. assert p.start is None
  56. p.start = start
  57. assert p.start == start
  58. @pytest.mark.parametrize(('start'), [
  59. ':-/',
  60. ])
  61. def test_dateperiod_set_start_fail(start):
  62. p = ioex.DatePeriod()
  63. with pytest.raises(TypeError):
  64. p.start = start
  65. @pytest.mark.parametrize(('end'), [
  66. datetime.datetime(2016, 7, 24, 12, 21),
  67. None,
  68. ])
  69. def test_dateperiod_set_end(end):
  70. p = ioex.DatePeriod()
  71. assert p.end is None
  72. p.end = end
  73. assert p.end == end
  74. @pytest.mark.parametrize(('end'), [
  75. ':-/',
  76. ])
  77. def test_dateperiod_set_end_fail(end):
  78. p = ioex.DatePeriod()
  79. with pytest.raises(TypeError):
  80. p.end = end
  81. @pytest.mark.parametrize(('start', 'end', 'iso'), [
  82. [
  83. datetime.datetime(2016, 7, 24, 12, 21, 0),
  84. datetime.datetime(2016, 7, 24, 12, 22, 13),
  85. '2016-07-24T12:21:00/2016-07-24T12:22:13',
  86. ],
  87. [
  88. datetime.datetime(2016, 7, 24, 12, 21, 0, tzinfo = pytz.utc),
  89. datetime.datetime(2016, 7, 24, 12, 22, 13, tzinfo = pytz.utc),
  90. '2016-07-24T12:21:00Z/2016-07-24T12:22:13Z',
  91. ],
  92. [
  93. datetime.datetime(2016, 7, 24, 12, 21, 0, tzinfo = pytz.utc),
  94. pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 24, 12, 22, 13)),
  95. '2016-07-24T12:21:00Z/2016-07-24T12:22:13+02:00',
  96. ],
  97. [
  98. pytz.timezone('US/Pacific').localize(datetime.datetime(2016, 1, 12, 12, 22, 13)),
  99. pytz.timezone('Europe/London').localize(datetime.datetime(2016, 1, 24, 12, 22, 13)),
  100. '2016-01-12T12:22:13-08:00/2016-01-24T12:22:13Z',
  101. ],
  102. [
  103. datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  104. datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13, tzinfo = pytz.utc),
  105. '2016-07-24T12:20:00.025500/2016-07-24T12:21:00.000013Z',
  106. ],
  107. ])
  108. def test_dateperiod_get_isoformat(start, end, iso):
  109. p = ioex.DatePeriod(start = start, end = end)
  110. assert p.isoformat == iso