test_.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(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_start_end_fail(start, end):
  47. with pytest.raises(TypeError):
  48. ioex.DatePeriod(start = start, end = end)
  49. @pytest.mark.parametrize(('start', 'end', 'iso'), [
  50. [
  51. datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  52. datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 130000, tzinfo = pytz.utc),
  53. '2016-07-24T12:20:00.0255/2016-07-24T12:21:00.13Z',
  54. ],
  55. ])
  56. def test_dateperiod_init_isoformat(start, end, iso):
  57. p = ioex.DatePeriod(isoformat = iso)
  58. assert p.start == start
  59. assert p.end == end
  60. @pytest.mark.parametrize(('params'), [
  61. {
  62. 'start': datetime.datetime(2016, 7, 24, 12, 20, 0),
  63. 'end': datetime.datetime(2016, 7, 24, 12, 21, 0),
  64. 'isoformat': '2016-07-24T12:20:00Z/2016-07-24T12:21:00Z',
  65. },
  66. {
  67. 'start': datetime.datetime(2016, 7, 24, 12, 20, 0),
  68. 'isoformat': '2016-07-24T12:20:00Z/2016-07-24T12:21:00Z',
  69. },
  70. {
  71. 'end': datetime.datetime(2016, 7, 24, 12, 21, 0),
  72. 'isoformat': '2016-07-24T12:20:00Z/2016-07-24T12:21:00Z',
  73. },
  74. ])
  75. def test_dateperiod_init_param_fail(params):
  76. with pytest.raises(StandardError):
  77. ioex.DatePeriod(**params)
  78. @pytest.mark.parametrize(('start'), [
  79. datetime.datetime(2016, 7, 24, 12, 21),
  80. None,
  81. ])
  82. def test_dateperiod_set_start(start):
  83. p = ioex.DatePeriod()
  84. assert p.start is None
  85. p.start = start
  86. assert p.start == start
  87. @pytest.mark.parametrize(('start'), [
  88. ':-/',
  89. ])
  90. def test_dateperiod_set_start_fail(start):
  91. p = ioex.DatePeriod()
  92. with pytest.raises(TypeError):
  93. p.start = start
  94. @pytest.mark.parametrize(('end'), [
  95. datetime.datetime(2016, 7, 24, 12, 21),
  96. None,
  97. ])
  98. def test_dateperiod_set_end(end):
  99. p = ioex.DatePeriod()
  100. assert p.end is None
  101. p.end = end
  102. assert p.end == end
  103. @pytest.mark.parametrize(('end'), [
  104. ':-/',
  105. ])
  106. def test_dateperiod_set_end_fail(end):
  107. p = ioex.DatePeriod()
  108. with pytest.raises(TypeError):
  109. p.end = end
  110. @pytest.mark.parametrize(('start', 'end', 'iso'), [
  111. [
  112. datetime.datetime(2016, 7, 24, 12, 21, 0),
  113. datetime.datetime(2016, 7, 24, 12, 22, 13),
  114. '2016-07-24T12:21:00/2016-07-24T12:22:13',
  115. ],
  116. [
  117. datetime.datetime(2016, 7, 24, 12, 21, 0, tzinfo = pytz.utc),
  118. datetime.datetime(2016, 7, 24, 12, 22, 13, tzinfo = pytz.utc),
  119. '2016-07-24T12:21:00Z/2016-07-24T12:22:13Z',
  120. ],
  121. [
  122. datetime.datetime(2016, 7, 24, 12, 21, 0, tzinfo = pytz.utc),
  123. pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 24, 12, 22, 13)),
  124. '2016-07-24T12:21:00Z/2016-07-24T12:22:13+02:00',
  125. ],
  126. [
  127. pytz.timezone('US/Pacific').localize(datetime.datetime(2016, 1, 12, 12, 22, 13)),
  128. pytz.timezone('Europe/London').localize(datetime.datetime(2016, 1, 24, 12, 22, 13)),
  129. '2016-01-12T12:22:13-08:00/2016-01-24T12:22:13Z',
  130. ],
  131. [
  132. datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  133. datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13, tzinfo = pytz.utc),
  134. '2016-07-24T12:20:00.025500/2016-07-24T12:21:00.000013Z',
  135. ],
  136. ])
  137. def test_dateperiod_get_isoformat(start, end, iso):
  138. p = ioex.DatePeriod(start = start, end = end)
  139. assert p.isoformat == iso
  140. @pytest.mark.parametrize(('start', 'end', 'iso'), [
  141. [
  142. datetime.datetime(2016, 7, 24, 12, 21, 0),
  143. datetime.datetime(2016, 7, 24, 12, 22, 13),
  144. '2016-07-24T12:21:00/2016-07-24T12:22:13',
  145. ],
  146. [
  147. datetime.datetime(2016, 7, 24, 12, 21, 0, tzinfo = pytz.utc),
  148. datetime.datetime(2016, 7, 24, 12, 22, 13, tzinfo = pytz.utc),
  149. '2016-07-24T12:21:00Z/2016-07-24T12:22:13Z',
  150. ],
  151. [
  152. datetime.datetime(2016, 7, 24, 12, 21, 0, tzinfo = pytz.utc),
  153. pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 24, 12, 22, 13)),
  154. '2016-07-24T12:21:00Z/2016-07-24T12:22:13+02:00',
  155. ],
  156. [
  157. pytz.timezone('US/Pacific').localize(datetime.datetime(2016, 1, 12, 12, 22, 13)),
  158. pytz.timezone('Europe/London').localize(datetime.datetime(2016, 1, 24, 12, 22, 13)),
  159. '2016-01-12T12:22:13-08:00/2016-01-24T12:22:13Z',
  160. ],
  161. [
  162. datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  163. datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13, tzinfo = pytz.utc),
  164. '2016-07-24T12:20:00.025500/2016-07-24T12:21:00.000013Z',
  165. ],
  166. [
  167. datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
  168. datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 130000, tzinfo = pytz.utc),
  169. '2016-07-24T12:20:00.0255/2016-07-24T12:21:00.13Z',
  170. ],
  171. ])
  172. def test_dateperiod_set_isoformat(start, end, iso):
  173. p = ioex.DatePeriod()
  174. p.isoformat = iso
  175. assert p.start == start
  176. assert p.end == end
  177. @pytest.mark.parametrize(('iso'), [
  178. '2016-07-24T12:20:0<INVALID>0.0255/2016-07-24T12:21:00.13Z',
  179. ])
  180. def test_dateperiod_set_isoformat_fail(iso):
  181. p = ioex.DatePeriod()
  182. with pytest.raises(ValueError):
  183. p.isoformat = iso