test_time.py 7.0 KB

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