test_time.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import pytest
  2. import pytz
  3. from datetime import datetime, timedelta, tzinfo
  4. from pytz import AmbiguousTimeError
  5. from case import Mock
  6. from celery.utils.time import (
  7. delta_resolution,
  8. humanize_seconds,
  9. maybe_iso8601,
  10. maybe_timedelta,
  11. timezone,
  12. rate,
  13. remaining,
  14. make_aware,
  15. maybe_make_aware,
  16. localize,
  17. LocalTimezone,
  18. ffwd,
  19. utcoffset,
  20. )
  21. from celery.utils.iso8601 import parse_iso8601
  22. class test_LocalTimezone:
  23. def test_daylight(self, patching):
  24. time = patching('celery.utils.time._time')
  25. time.timezone = 3600
  26. time.daylight = False
  27. x = LocalTimezone()
  28. assert x.STDOFFSET == timedelta(seconds=-3600)
  29. assert x.DSTOFFSET == x.STDOFFSET
  30. time.daylight = True
  31. time.altzone = 3600
  32. y = LocalTimezone()
  33. assert y.STDOFFSET == timedelta(seconds=-3600)
  34. assert y.DSTOFFSET == timedelta(seconds=-3600)
  35. assert repr(y)
  36. y._isdst = Mock()
  37. y._isdst.return_value = True
  38. assert y.utcoffset(datetime.now())
  39. assert not y.dst(datetime.now())
  40. y._isdst.return_value = False
  41. assert y.utcoffset(datetime.now())
  42. assert not y.dst(datetime.now())
  43. assert y.tzname(datetime.now())
  44. class test_iso8601:
  45. def test_parse_with_timezone(self):
  46. d = datetime.utcnow().replace(tzinfo=pytz.utc)
  47. assert parse_iso8601(d.isoformat()) == d
  48. # 2013-06-07T20:12:51.775877+00:00
  49. iso = d.isoformat()
  50. iso1 = iso.replace('+00:00', '-01:00')
  51. d1 = parse_iso8601(iso1)
  52. assert d1.tzinfo._minutes == -60
  53. iso2 = iso.replace('+00:00', '+01:00')
  54. d2 = parse_iso8601(iso2)
  55. assert d2.tzinfo._minutes == +60
  56. iso3 = iso.replace('+00:00', 'Z')
  57. d3 = parse_iso8601(iso3)
  58. assert d3.tzinfo == pytz.UTC
  59. @pytest.mark.parametrize('delta,expected', [
  60. (timedelta(days=2), datetime(2010, 3, 30, 0, 0)),
  61. (timedelta(hours=2), datetime(2010, 3, 30, 11, 0)),
  62. (timedelta(minutes=2), datetime(2010, 3, 30, 11, 50)),
  63. (timedelta(seconds=2), None),
  64. ])
  65. def test_delta_resolution(delta, expected):
  66. dt = datetime(2010, 3, 30, 11, 50, 58, 41065)
  67. assert delta_resolution(dt, delta) == expected or dt
  68. @pytest.mark.parametrize('seconds,expected', [
  69. (4 * 60 * 60 * 24, '4.00 days'),
  70. (1 * 60 * 60 * 24, '1.00 day'),
  71. (4 * 60 * 60, '4.00 hours'),
  72. (1 * 60 * 60, '1.00 hour'),
  73. (4 * 60, '4.00 minutes'),
  74. (1 * 60, '1.00 minute'),
  75. (4, '4.00 seconds'),
  76. (1, '1.00 second'),
  77. (4.3567631221, '4.36 seconds'),
  78. (0, 'now'),
  79. ])
  80. def test_humanize_seconds(seconds, expected):
  81. assert humanize_seconds(seconds) == expected
  82. def test_humanize_seconds__prefix():
  83. assert humanize_seconds(4, prefix='about ') == 'about 4.00 seconds'
  84. def test_maybe_iso8601_datetime():
  85. now = datetime.now()
  86. assert maybe_iso8601(now) is now
  87. @pytest.mark.parametrize('arg,expected', [
  88. (30, timedelta(seconds=30)),
  89. (30.6, timedelta(seconds=30.6)),
  90. (timedelta(days=2), timedelta(days=2)),
  91. ])
  92. def test_maybe_timedelta(arg, expected):
  93. assert maybe_timedelta(arg) == expected
  94. def test_remaining_relative():
  95. remaining(datetime.utcnow(), timedelta(hours=1), relative=True)
  96. class test_timezone:
  97. def test_get_timezone_with_pytz(self):
  98. assert timezone.get_timezone('UTC')
  99. def test_tz_or_local(self):
  100. assert timezone.tz_or_local() == timezone.local
  101. assert timezone.tz_or_local(timezone.utc)
  102. def test_to_local(self):
  103. assert timezone.to_local(make_aware(datetime.utcnow(), timezone.utc))
  104. assert timezone.to_local(datetime.utcnow())
  105. def test_to_local_fallback(self):
  106. assert timezone.to_local_fallback(
  107. make_aware(datetime.utcnow(), timezone.utc))
  108. assert timezone.to_local_fallback(datetime.utcnow())
  109. class test_make_aware:
  110. def test_tz_without_localize(self):
  111. tz = tzinfo()
  112. assert not hasattr(tz, 'localize')
  113. wtz = make_aware(datetime.utcnow(), tz)
  114. assert wtz.tzinfo == tz
  115. def test_when_has_localize(self):
  116. class tzz(tzinfo):
  117. raises = False
  118. def localize(self, dt, is_dst=None):
  119. self.localized = True
  120. if self.raises and is_dst is None:
  121. self.raised = True
  122. raise AmbiguousTimeError()
  123. return 1 # needed by min() in Python 3 (None not hashable)
  124. tz = tzz()
  125. make_aware(datetime.utcnow(), tz)
  126. assert tz.localized
  127. tz2 = tzz()
  128. tz2.raises = True
  129. make_aware(datetime.utcnow(), tz2)
  130. assert tz2.localized
  131. assert tz2.raised
  132. def test_maybe_make_aware(self):
  133. aware = datetime.utcnow().replace(tzinfo=timezone.utc)
  134. assert maybe_make_aware(aware)
  135. naive = datetime.utcnow()
  136. assert maybe_make_aware(naive)
  137. class test_localize:
  138. def test_tz_without_normalize(self):
  139. tz = tzinfo()
  140. assert not hasattr(tz, 'normalize')
  141. assert localize(make_aware(datetime.utcnow(), tz), tz)
  142. def test_when_has_normalize(self):
  143. class tzz(tzinfo):
  144. raises = None
  145. def normalize(self, dt, **kwargs):
  146. self.normalized = True
  147. if self.raises and kwargs and kwargs.get('is_dst') is None:
  148. self.raised = True
  149. raise self.raises
  150. return 1 # needed by min() in Python 3 (None not hashable)
  151. tz = tzz()
  152. localize(make_aware(datetime.utcnow(), tz), tz)
  153. assert tz.normalized
  154. tz2 = tzz()
  155. tz2.raises = AmbiguousTimeError()
  156. localize(make_aware(datetime.utcnow(), tz2), tz2)
  157. assert tz2.normalized
  158. assert tz2.raised
  159. tz3 = tzz()
  160. tz3.raises = TypeError()
  161. localize(make_aware(datetime.utcnow(), tz3), tz3)
  162. assert tz3.normalized
  163. assert tz3.raised
  164. @pytest.mark.parametrize('s,expected', [
  165. (999, 999),
  166. (7.5, 7.5),
  167. ('2.5/s', 2.5),
  168. ('1456/s', 1456),
  169. ('100/m', 100 / 60.0),
  170. ('10/h', 10 / 60.0 / 60.0),
  171. (0, 0),
  172. (None, 0),
  173. ('0/m', 0),
  174. ('0/h', 0),
  175. ('0/s', 0),
  176. ('0.0/s', 0),
  177. ])
  178. def test_rate_limit_string(s, expected):
  179. assert rate(s) == expected
  180. class test_ffwd:
  181. def test_repr(self):
  182. x = ffwd(year=2012)
  183. assert repr(x)
  184. def test_radd_with_unknown_gives_NotImplemented(self):
  185. x = ffwd(year=2012)
  186. assert x.__radd__(object()) == NotImplemented
  187. class test_utcoffset:
  188. def test_utcoffset(self, patching):
  189. _time = patching('celery.utils.time._time')
  190. _time.daylight = True
  191. assert utcoffset(time=_time) is not None
  192. _time.daylight = False
  193. assert utcoffset(time=_time) is not None