test_schedules.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. from __future__ import absolute_import, unicode_literals
  2. import time
  3. from contextlib import contextmanager
  4. from datetime import datetime, timedelta
  5. from pickle import dumps, loads
  6. import pytest
  7. import pytz
  8. from case import Case, Mock, skip
  9. from celery.five import items
  10. from celery.schedules import (ParseException, crontab, crontab_parser,
  11. schedule, solar)
  12. assertions = Case('__init__')
  13. @contextmanager
  14. def patch_crontab_nowfun(cls, retval):
  15. prev_nowfun = cls.nowfun
  16. cls.nowfun = lambda: retval
  17. try:
  18. yield
  19. finally:
  20. cls.nowfun = prev_nowfun
  21. @skip.unless_module('ephem')
  22. class test_solar:
  23. def setup(self):
  24. self.s = solar('sunrise', 60, 30, app=self.app)
  25. def test_reduce(self):
  26. fun, args = self.s.__reduce__()
  27. assert fun(*args) == self.s
  28. def test_eq(self):
  29. assert self.s == solar('sunrise', 60, 30, app=self.app)
  30. assert self.s != solar('sunset', 60, 30, app=self.app)
  31. assert self.s != schedule(10)
  32. def test_repr(self):
  33. assert repr(self.s)
  34. def test_is_due(self):
  35. self.s.remaining_estimate = Mock(name='rem')
  36. self.s.remaining_estimate.return_value = timedelta(seconds=0)
  37. assert self.s.is_due(datetime.utcnow()).is_due
  38. def test_is_due__not_due(self):
  39. self.s.remaining_estimate = Mock(name='rem')
  40. self.s.remaining_estimate.return_value = timedelta(hours=10)
  41. assert not self.s.is_due(datetime.utcnow()).is_due
  42. def test_remaining_estimate(self):
  43. self.s.cal = Mock(name='cal')
  44. self.s.cal.next_rising().datetime.return_value = datetime.utcnow()
  45. self.s.remaining_estimate(datetime.utcnow())
  46. def test_coordinates(self):
  47. with pytest.raises(ValueError):
  48. solar('sunrise', -120, 60, app=self.app)
  49. with pytest.raises(ValueError):
  50. solar('sunrise', 120, 60, app=self.app)
  51. with pytest.raises(ValueError):
  52. solar('sunrise', 60, -200, app=self.app)
  53. with pytest.raises(ValueError):
  54. solar('sunrise', 60, 200, app=self.app)
  55. def test_invalid_event(self):
  56. with pytest.raises(ValueError):
  57. solar('asdqwewqew', 60, 60, app=self.app)
  58. def test_event_uses_center(self):
  59. s = solar('solar_noon', 60, 60, app=self.app)
  60. for ev, is_center in s._use_center_l.items():
  61. s.method = s._methods[ev]
  62. s.is_center = s._use_center_l[ev]
  63. try:
  64. s.remaining_estimate(datetime.utcnow())
  65. except TypeError:
  66. pytest.fail("{0} was called with 'use_center' which is not a \
  67. valid keyword for the function.".format(s.method))
  68. class test_schedule:
  69. def test_ne(self):
  70. s1 = schedule(10, app=self.app)
  71. s2 = schedule(12, app=self.app)
  72. s3 = schedule(10, app=self.app)
  73. assert s1 == s3
  74. assert s1 != s2
  75. def test_pickle(self):
  76. s1 = schedule(10, app=self.app)
  77. fun, args = s1.__reduce__()
  78. s2 = fun(*args)
  79. assert s1 == s2
  80. # This is needed for test_crontab_parser because datetime.utcnow doesn't pickle
  81. # in python 2
  82. def utcnow():
  83. return datetime.utcnow()
  84. class test_crontab_parser:
  85. def crontab(self, *args, **kwargs):
  86. return crontab(*args, **dict(kwargs, app=self.app))
  87. def test_crontab_reduce(self):
  88. c = self.crontab('*')
  89. assert c == loads(dumps(c))
  90. c = self.crontab(
  91. minute='1',
  92. hour='2',
  93. day_of_week='3',
  94. day_of_month='4',
  95. month_of_year='5',
  96. nowfun=utcnow)
  97. assert c == loads(dumps(c))
  98. def test_range_steps_not_enough(self):
  99. with pytest.raises(crontab_parser.ParseException):
  100. crontab_parser(24)._range_steps([1])
  101. def test_parse_star(self):
  102. assert crontab_parser(24).parse('*') == set(range(24))
  103. assert crontab_parser(60).parse('*') == set(range(60))
  104. assert crontab_parser(7).parse('*') == set(range(7))
  105. assert crontab_parser(31, 1).parse('*') == set(range(1, 31 + 1))
  106. assert crontab_parser(12, 1).parse('*') == set(range(1, 12 + 1))
  107. def test_parse_range(self):
  108. assert crontab_parser(60).parse('1-10') == set(range(1, 10 + 1))
  109. assert crontab_parser(24).parse('0-20') == set(range(0, 20 + 1))
  110. assert crontab_parser().parse('2-10') == set(range(2, 10 + 1))
  111. assert crontab_parser(60, 1).parse('1-10') == set(range(1, 10 + 1))
  112. def test_parse_range_wraps(self):
  113. assert crontab_parser(12).parse('11-1') == {11, 0, 1}
  114. assert crontab_parser(60, 1).parse('2-1') == set(range(1, 60 + 1))
  115. def test_parse_groups(self):
  116. assert crontab_parser().parse('1,2,3,4') == {1, 2, 3, 4}
  117. assert crontab_parser().parse('0,15,30,45') == {0, 15, 30, 45}
  118. assert crontab_parser(min_=1).parse('1,2,3,4') == {1, 2, 3, 4}
  119. def test_parse_steps(self):
  120. assert crontab_parser(8).parse('*/2') == {0, 2, 4, 6}
  121. assert crontab_parser().parse('*/2') == {i * 2 for i in range(30)}
  122. assert crontab_parser().parse('*/3') == {i * 3 for i in range(20)}
  123. assert crontab_parser(8, 1).parse('*/2') == {1, 3, 5, 7}
  124. assert crontab_parser(min_=1).parse('*/2') == {
  125. i * 2 + 1 for i in range(30)
  126. }
  127. assert crontab_parser(min_=1).parse('*/3') == {
  128. i * 3 + 1 for i in range(20)
  129. }
  130. def test_parse_composite(self):
  131. assert crontab_parser(8).parse('*/2') == {0, 2, 4, 6}
  132. assert crontab_parser().parse('2-9/5') == {2, 7}
  133. assert crontab_parser().parse('2-10/5') == {2, 7}
  134. assert crontab_parser(min_=1).parse('55-5/3') == {55, 58, 1, 4}
  135. assert crontab_parser().parse('2-11/5,3') == {2, 3, 7}
  136. assert crontab_parser().parse('2-4/3,*/5,0-21/4') == {
  137. 0, 2, 4, 5, 8, 10, 12, 15, 16, 20, 25, 30, 35, 40, 45, 50, 55,
  138. }
  139. assert crontab_parser().parse('1-9/2') == {1, 3, 5, 7, 9}
  140. assert crontab_parser(8, 1).parse('*/2') == {1, 3, 5, 7}
  141. assert crontab_parser(min_=1).parse('2-9/5') == {2, 7}
  142. assert crontab_parser(min_=1).parse('2-10/5') == {2, 7}
  143. assert crontab_parser(min_=1).parse('2-11/5,3') == {2, 3, 7}
  144. assert crontab_parser(min_=1).parse('2-4/3,*/5,1-21/4') == {
  145. 1, 2, 5, 6, 9, 11, 13, 16, 17, 21, 26, 31, 36, 41, 46, 51, 56,
  146. }
  147. assert crontab_parser(min_=1).parse('1-9/2') == {1, 3, 5, 7, 9}
  148. def test_parse_errors_on_empty_string(self):
  149. with pytest.raises(ParseException):
  150. crontab_parser(60).parse('')
  151. def test_parse_errors_on_empty_group(self):
  152. with pytest.raises(ParseException):
  153. crontab_parser(60).parse('1,,2')
  154. def test_parse_errors_on_empty_steps(self):
  155. with pytest.raises(ParseException):
  156. crontab_parser(60).parse('*/')
  157. def test_parse_errors_on_negative_number(self):
  158. with pytest.raises(ParseException):
  159. crontab_parser(60).parse('-20')
  160. def test_parse_errors_on_lt_min(self):
  161. crontab_parser(min_=1).parse('1')
  162. with pytest.raises(ValueError):
  163. crontab_parser(12, 1).parse('0')
  164. with pytest.raises(ValueError):
  165. crontab_parser(24, 1).parse('12-0')
  166. def test_parse_errors_on_gt_max(self):
  167. crontab_parser(1).parse('0')
  168. with pytest.raises(ValueError):
  169. crontab_parser(1).parse('1')
  170. with pytest.raises(ValueError):
  171. crontab_parser(60).parse('61-0')
  172. def test_expand_cronspec_eats_iterables(self):
  173. assert crontab._expand_cronspec(iter([1, 2, 3]), 100) == {1, 2, 3}
  174. assert crontab._expand_cronspec(iter([1, 2, 3]), 100, 1) == {1, 2, 3}
  175. def test_expand_cronspec_invalid_type(self):
  176. with pytest.raises(TypeError):
  177. crontab._expand_cronspec(object(), 100)
  178. def test_repr(self):
  179. assert '*' in repr(self.crontab('*'))
  180. def test_eq(self):
  181. assert (self.crontab(day_of_week='1, 2') ==
  182. self.crontab(day_of_week='1-2'))
  183. assert (self.crontab(day_of_month='1, 16, 31') ==
  184. self.crontab(day_of_month='*/15'))
  185. assert (
  186. self.crontab(
  187. minute='1', hour='2', day_of_week='5',
  188. day_of_month='10', month_of_year='5') ==
  189. self.crontab(
  190. minute='1', hour='2', day_of_week='5',
  191. day_of_month='10', month_of_year='5'))
  192. assert crontab(minute='1') != crontab(minute='2')
  193. assert (self.crontab(month_of_year='1') !=
  194. self.crontab(month_of_year='2'))
  195. assert object() != self.crontab(minute='1')
  196. assert self.crontab(minute='1') != object()
  197. assert crontab(month_of_year='1') != schedule(10)
  198. class test_crontab_remaining_estimate:
  199. def crontab(self, *args, **kwargs):
  200. return crontab(*args, **dict(kwargs, app=self.app))
  201. def next_ocurrance(self, crontab, now):
  202. crontab.nowfun = lambda: now
  203. return now + crontab.remaining_estimate(now)
  204. def test_next_minute(self):
  205. next = self.next_ocurrance(
  206. self.crontab(), datetime(2010, 9, 11, 14, 30, 15),
  207. )
  208. assert next == datetime(2010, 9, 11, 14, 31)
  209. def test_not_next_minute(self):
  210. next = self.next_ocurrance(
  211. self.crontab(), datetime(2010, 9, 11, 14, 59, 15),
  212. )
  213. assert next == datetime(2010, 9, 11, 15, 0)
  214. def test_this_hour(self):
  215. next = self.next_ocurrance(
  216. self.crontab(minute=[5, 42]), datetime(2010, 9, 11, 14, 30, 15),
  217. )
  218. assert next == datetime(2010, 9, 11, 14, 42)
  219. def test_not_this_hour(self):
  220. next = self.next_ocurrance(
  221. self.crontab(minute=[5, 10, 15]),
  222. datetime(2010, 9, 11, 14, 30, 15),
  223. )
  224. assert next == datetime(2010, 9, 11, 15, 5)
  225. def test_today(self):
  226. next = self.next_ocurrance(
  227. self.crontab(minute=[5, 42], hour=[12, 17]),
  228. datetime(2010, 9, 11, 14, 30, 15),
  229. )
  230. assert next == datetime(2010, 9, 11, 17, 5)
  231. def test_not_today(self):
  232. next = self.next_ocurrance(
  233. self.crontab(minute=[5, 42], hour=[12]),
  234. datetime(2010, 9, 11, 14, 30, 15),
  235. )
  236. assert next == datetime(2010, 9, 12, 12, 5)
  237. def test_weekday(self):
  238. next = self.next_ocurrance(
  239. self.crontab(minute=30, hour=14, day_of_week='sat'),
  240. datetime(2010, 9, 11, 14, 30, 15),
  241. )
  242. assert next == datetime(2010, 9, 18, 14, 30)
  243. def test_not_weekday(self):
  244. next = self.next_ocurrance(
  245. self.crontab(minute=[5, 42], day_of_week='mon-fri'),
  246. datetime(2010, 9, 11, 14, 30, 15),
  247. )
  248. assert next == datetime(2010, 9, 13, 0, 5)
  249. def test_monthday(self):
  250. next = self.next_ocurrance(
  251. self.crontab(minute=30, hour=14, day_of_month=18),
  252. datetime(2010, 9, 11, 14, 30, 15),
  253. )
  254. assert next == datetime(2010, 9, 18, 14, 30)
  255. def test_not_monthday(self):
  256. next = self.next_ocurrance(
  257. self.crontab(minute=[5, 42], day_of_month=29),
  258. datetime(2010, 1, 22, 14, 30, 15),
  259. )
  260. assert next == datetime(2010, 1, 29, 0, 5)
  261. def test_weekday_monthday(self):
  262. next = self.next_ocurrance(
  263. self.crontab(minute=30, hour=14,
  264. day_of_week='mon', day_of_month=18),
  265. datetime(2010, 1, 18, 14, 30, 15),
  266. )
  267. assert next == datetime(2010, 10, 18, 14, 30)
  268. def test_monthday_not_weekday(self):
  269. next = self.next_ocurrance(
  270. self.crontab(minute=[5, 42], day_of_week='sat', day_of_month=29),
  271. datetime(2010, 1, 29, 0, 5, 15),
  272. )
  273. assert next == datetime(2010, 5, 29, 0, 5)
  274. def test_weekday_not_monthday(self):
  275. next = self.next_ocurrance(
  276. self.crontab(minute=[5, 42], day_of_week='mon', day_of_month=18),
  277. datetime(2010, 1, 11, 0, 5, 15),
  278. )
  279. assert next == datetime(2010, 1, 18, 0, 5)
  280. def test_not_weekday_not_monthday(self):
  281. next = self.next_ocurrance(
  282. self.crontab(minute=[5, 42], day_of_week='mon', day_of_month=18),
  283. datetime(2010, 1, 10, 0, 5, 15),
  284. )
  285. assert next == datetime(2010, 1, 18, 0, 5)
  286. def test_leapday(self):
  287. next = self.next_ocurrance(
  288. self.crontab(minute=30, hour=14, day_of_month=29),
  289. datetime(2012, 1, 29, 14, 30, 15),
  290. )
  291. assert next == datetime(2012, 2, 29, 14, 30)
  292. def test_not_leapday(self):
  293. next = self.next_ocurrance(
  294. self.crontab(minute=30, hour=14, day_of_month=29),
  295. datetime(2010, 1, 29, 14, 30, 15),
  296. )
  297. assert next == datetime(2010, 3, 29, 14, 30)
  298. def test_weekmonthdayyear(self):
  299. next = self.next_ocurrance(
  300. self.crontab(minute=30, hour=14, day_of_week='fri',
  301. day_of_month=29, month_of_year=1),
  302. datetime(2010, 1, 22, 14, 30, 15),
  303. )
  304. assert next == datetime(2010, 1, 29, 14, 30)
  305. def test_monthdayyear_not_week(self):
  306. next = self.next_ocurrance(
  307. self.crontab(minute=[5, 42], day_of_week='wed,thu',
  308. day_of_month=29, month_of_year='1,4,7'),
  309. datetime(2010, 1, 29, 14, 30, 15),
  310. )
  311. assert next == datetime(2010, 4, 29, 0, 5)
  312. def test_weekdaymonthyear_not_monthday(self):
  313. next = self.next_ocurrance(
  314. self.crontab(minute=30, hour=14, day_of_week='fri',
  315. day_of_month=29, month_of_year='1-10'),
  316. datetime(2010, 1, 29, 14, 30, 15),
  317. )
  318. assert next == datetime(2010, 10, 29, 14, 30)
  319. def test_weekmonthday_not_monthyear(self):
  320. next = self.next_ocurrance(
  321. self.crontab(minute=[5, 42], day_of_week='fri',
  322. day_of_month=29, month_of_year='2-10'),
  323. datetime(2010, 1, 29, 14, 30, 15),
  324. )
  325. assert next == datetime(2010, 10, 29, 0, 5)
  326. def test_weekday_not_monthdayyear(self):
  327. next = self.next_ocurrance(
  328. self.crontab(minute=[5, 42], day_of_week='mon',
  329. day_of_month=18, month_of_year='2-10'),
  330. datetime(2010, 1, 11, 0, 5, 15),
  331. )
  332. assert next == datetime(2010, 10, 18, 0, 5)
  333. def test_monthday_not_weekdaymonthyear(self):
  334. next = self.next_ocurrance(
  335. self.crontab(minute=[5, 42], day_of_week='mon',
  336. day_of_month=29, month_of_year='2-4'),
  337. datetime(2010, 1, 29, 0, 5, 15),
  338. )
  339. assert next == datetime(2010, 3, 29, 0, 5)
  340. def test_monthyear_not_weekmonthday(self):
  341. next = self.next_ocurrance(
  342. self.crontab(minute=[5, 42], day_of_week='mon',
  343. day_of_month=29, month_of_year='2-4'),
  344. datetime(2010, 2, 28, 0, 5, 15),
  345. )
  346. assert next == datetime(2010, 3, 29, 0, 5)
  347. def test_not_weekmonthdayyear(self):
  348. next = self.next_ocurrance(
  349. self.crontab(minute=[5, 42], day_of_week='fri,sat',
  350. day_of_month=29, month_of_year='2-10'),
  351. datetime(2010, 1, 28, 14, 30, 15),
  352. )
  353. assert next == datetime(2010, 5, 29, 0, 5)
  354. def test_invalid_specification(self):
  355. # *** WARNING ***
  356. # This test triggers an infinite loop in case of a regression
  357. with pytest.raises(RuntimeError):
  358. self.next_ocurrance(
  359. self.crontab(day_of_month=31, month_of_year=4),
  360. datetime(2010, 1, 28, 14, 30, 15),
  361. )
  362. def test_leapyear(self):
  363. next = self.next_ocurrance(
  364. self.crontab(minute=30, hour=14, day_of_month=29, month_of_year=2),
  365. datetime(2012, 2, 29, 14, 30),
  366. )
  367. assert next == datetime(2016, 2, 29, 14, 30)
  368. def test_day_after_dst_end(self):
  369. # Test for #1604 issue with region configuration using DST
  370. tzname = "Europe/Paris"
  371. self.app.timezone = tzname
  372. tz = pytz.timezone(tzname)
  373. crontab = self.crontab(minute=0, hour=9)
  374. # Set last_run_at Before DST end
  375. last_run_at = tz.localize(datetime(2017, 10, 28, 9, 0))
  376. # Set now after DST end
  377. now = tz.localize(datetime(2017, 10, 29, 7, 0))
  378. crontab.nowfun = lambda: now
  379. next = now + crontab.remaining_estimate(last_run_at)
  380. assert next.utcoffset().seconds == 3600
  381. assert next == tz.localize(datetime(2017, 10, 29, 9, 0))
  382. def test_day_after_dst_start(self):
  383. # Test for #1604 issue with region configuration using DST
  384. tzname = "Europe/Paris"
  385. self.app.timezone = tzname
  386. tz = pytz.timezone(tzname)
  387. crontab = self.crontab(minute=0, hour=9)
  388. # Set last_run_at Before DST start
  389. last_run_at = tz.localize(datetime(2017, 3, 25, 9, 0))
  390. # Set now after DST start
  391. now = tz.localize(datetime(2017, 3, 26, 7, 0))
  392. crontab.nowfun = lambda: now
  393. next = now + crontab.remaining_estimate(last_run_at)
  394. assert next.utcoffset().seconds == 7200
  395. assert next == tz.localize(datetime(2017, 3, 26, 9, 0))
  396. class test_crontab_is_due:
  397. def setup(self):
  398. self.now = self.app.now()
  399. self.next_minute = 60 - self.now.second - 1e-6 * self.now.microsecond
  400. self.every_minute = self.crontab()
  401. self.quarterly = self.crontab(minute='*/15')
  402. self.hourly = self.crontab(minute=30)
  403. self.daily = self.crontab(hour=7, minute=30)
  404. self.weekly = self.crontab(hour=7, minute=30, day_of_week='thursday')
  405. self.monthly = self.crontab(
  406. hour=7, minute=30, day_of_week='thursday', day_of_month='8-14',
  407. )
  408. self.monthly_moy = self.crontab(
  409. hour=22, day_of_week='*', month_of_year='2',
  410. day_of_month='26,27,28',
  411. )
  412. self.yearly = self.crontab(
  413. hour=7, minute=30, day_of_week='thursday',
  414. day_of_month='8-14', month_of_year=3,
  415. )
  416. def crontab(self, *args, **kwargs):
  417. return crontab(*args, app=self.app, **kwargs)
  418. def test_default_crontab_spec(self):
  419. c = self.crontab()
  420. assert c.minute == set(range(60))
  421. assert c.hour == set(range(24))
  422. assert c.day_of_week == set(range(7))
  423. assert c.day_of_month == set(range(1, 32))
  424. assert c.month_of_year == set(range(1, 13))
  425. def test_simple_crontab_spec(self):
  426. c = self.crontab(minute=30)
  427. assert c.minute == {30}
  428. assert c.hour == set(range(24))
  429. assert c.day_of_week == set(range(7))
  430. assert c.day_of_month == set(range(1, 32))
  431. assert c.month_of_year == set(range(1, 13))
  432. @pytest.mark.parametrize('minute,expected', [
  433. (30, {30}),
  434. ('30', {30}),
  435. ((30, 40, 50), {30, 40, 50}),
  436. ((30, 40, 50, 51), {30, 40, 50, 51})
  437. ])
  438. def test_crontab_spec_minute_formats(self, minute, expected):
  439. c = self.crontab(minute=minute)
  440. assert c.minute == expected
  441. @pytest.mark.parametrize('minute', [60, '0-100'])
  442. def test_crontab_spec_invalid_minute(self, minute):
  443. with pytest.raises(ValueError):
  444. self.crontab(minute=minute)
  445. @pytest.mark.parametrize('hour,expected', [
  446. (6, {6}),
  447. ('5', {5}),
  448. ((4, 8, 12), {4, 8, 12}),
  449. ])
  450. def test_crontab_spec_hour_formats(self, hour, expected):
  451. c = self.crontab(hour=hour)
  452. assert c.hour == expected
  453. @pytest.mark.parametrize('hour', [24, '0-30'])
  454. def test_crontab_spec_invalid_hour(self, hour):
  455. with pytest.raises(ValueError):
  456. self.crontab(hour=hour)
  457. @pytest.mark.parametrize('day_of_week,expected', [
  458. (5, {5}),
  459. ('5', {5}),
  460. ('fri', {5}),
  461. ('tuesday,sunday,fri', {0, 2, 5}),
  462. ('mon-fri', {1, 2, 3, 4, 5}),
  463. ('*/2', {0, 2, 4, 6}),
  464. ])
  465. def test_crontab_spec_dow_formats(self, day_of_week, expected):
  466. c = self.crontab(day_of_week=day_of_week)
  467. assert c.day_of_week == expected
  468. @pytest.mark.parametrize('day_of_week', [
  469. 'fooday-barday', '1,4,foo', '7', '12',
  470. ])
  471. def test_crontab_spec_invalid_dow(self, day_of_week):
  472. with pytest.raises(ValueError):
  473. self.crontab(day_of_week=day_of_week)
  474. @pytest.mark.parametrize('day_of_month,expected', [
  475. (5, {5}),
  476. ('5', {5}),
  477. ('2,4,6', {2, 4, 6}),
  478. ('*/5', {1, 6, 11, 16, 21, 26, 31}),
  479. ])
  480. def test_crontab_spec_dom_formats(self, day_of_month, expected):
  481. c = self.crontab(day_of_month=day_of_month)
  482. assert c.day_of_month == expected
  483. @pytest.mark.parametrize('day_of_month', [0, '0-10', 32, '31,32'])
  484. def test_crontab_spec_invalid_dom(self, day_of_month):
  485. with pytest.raises(ValueError):
  486. self.crontab(day_of_month=day_of_month)
  487. @pytest.mark.parametrize('month_of_year,expected', [
  488. (1, {1}),
  489. ('1', {1}),
  490. ('2,4,6', {2, 4, 6}),
  491. ('*/2', {1, 3, 5, 7, 9, 11}),
  492. ('2-12/2', {2, 4, 6, 8, 10, 12}),
  493. ])
  494. def test_crontab_spec_moy_formats(self, month_of_year, expected):
  495. c = self.crontab(month_of_year=month_of_year)
  496. assert c.month_of_year == expected
  497. @pytest.mark.parametrize('month_of_year', [0, '0-5', 13, '12,13'])
  498. def test_crontab_spec_invalid_moy(self, month_of_year):
  499. with pytest.raises(ValueError):
  500. self.crontab(month_of_year=month_of_year)
  501. def seconds_almost_equal(self, a, b, precision):
  502. for index, skew in enumerate((+1, -1, 0)):
  503. try:
  504. assertions.assertAlmostEqual(a, b + skew, precision)
  505. except Exception as exc:
  506. # AssertionError != builtins.AssertionError in py.test
  507. if 'AssertionError' in str(exc):
  508. if index + 1 >= 3:
  509. raise
  510. else:
  511. break
  512. def test_every_minute_execution_is_due(self):
  513. last_ran = self.now - timedelta(seconds=61)
  514. due, remaining = self.every_minute.is_due(last_ran)
  515. self.assert_relativedelta(self.every_minute, last_ran)
  516. assert due
  517. self.seconds_almost_equal(remaining, self.next_minute, 1)
  518. def assert_relativedelta(self, due, last_ran):
  519. try:
  520. from dateutil.relativedelta import relativedelta
  521. except ImportError:
  522. return
  523. l1, d1, n1 = due.remaining_delta(last_ran)
  524. l2, d2, n2 = due.remaining_delta(last_ran, ffwd=relativedelta)
  525. if not isinstance(d1, relativedelta):
  526. assert l1 == l2
  527. for field, value in items(d1._fields()):
  528. assert getattr(d1, field) == value
  529. assert not d2.years
  530. assert not d2.months
  531. assert not d2.days
  532. assert not d2.leapdays
  533. assert not d2.hours
  534. assert not d2.minutes
  535. assert not d2.seconds
  536. assert not d2.microseconds
  537. def test_every_minute_execution_is_not_due(self):
  538. last_ran = self.now - timedelta(seconds=self.now.second)
  539. due, remaining = self.every_minute.is_due(last_ran)
  540. assert not due
  541. self.seconds_almost_equal(remaining, self.next_minute, 1)
  542. def test_execution_is_due_on_saturday(self):
  543. # 29th of May 2010 is a saturday
  544. with patch_crontab_nowfun(self.hourly, datetime(2010, 5, 29, 10, 30)):
  545. last_ran = self.now - timedelta(seconds=61)
  546. due, remaining = self.every_minute.is_due(last_ran)
  547. assert due
  548. self.seconds_almost_equal(remaining, self.next_minute, 1)
  549. def test_execution_is_due_on_sunday(self):
  550. # 30th of May 2010 is a sunday
  551. with patch_crontab_nowfun(self.hourly, datetime(2010, 5, 30, 10, 30)):
  552. last_ran = self.now - timedelta(seconds=61)
  553. due, remaining = self.every_minute.is_due(last_ran)
  554. assert due
  555. self.seconds_almost_equal(remaining, self.next_minute, 1)
  556. def test_execution_is_due_on_monday(self):
  557. # 31st of May 2010 is a monday
  558. with patch_crontab_nowfun(self.hourly, datetime(2010, 5, 31, 10, 30)):
  559. last_ran = self.now - timedelta(seconds=61)
  560. due, remaining = self.every_minute.is_due(last_ran)
  561. assert due
  562. self.seconds_almost_equal(remaining, self.next_minute, 1)
  563. def test_every_hour_execution_is_due(self):
  564. with patch_crontab_nowfun(self.hourly, datetime(2010, 5, 10, 10, 30)):
  565. due, remaining = self.hourly.is_due(datetime(2010, 5, 10, 6, 30))
  566. assert due
  567. assert remaining == 60 * 60
  568. def test_every_hour_execution_is_not_due(self):
  569. with patch_crontab_nowfun(self.hourly, datetime(2010, 5, 10, 10, 29)):
  570. due, remaining = self.hourly.is_due(datetime(2010, 5, 10, 9, 30))
  571. assert not due
  572. assert remaining == 60
  573. def test_first_quarter_execution_is_due(self):
  574. with patch_crontab_nowfun(
  575. self.quarterly, datetime(2010, 5, 10, 10, 15)):
  576. due, remaining = self.quarterly.is_due(
  577. datetime(2010, 5, 10, 6, 30),
  578. )
  579. assert due
  580. assert remaining == 15 * 60
  581. def test_second_quarter_execution_is_due(self):
  582. with patch_crontab_nowfun(
  583. self.quarterly, datetime(2010, 5, 10, 10, 30)):
  584. due, remaining = self.quarterly.is_due(
  585. datetime(2010, 5, 10, 6, 30),
  586. )
  587. assert due
  588. assert remaining == 15 * 60
  589. def test_first_quarter_execution_is_not_due(self):
  590. with patch_crontab_nowfun(
  591. self.quarterly, datetime(2010, 5, 10, 10, 14)):
  592. due, remaining = self.quarterly.is_due(
  593. datetime(2010, 5, 10, 10, 0),
  594. )
  595. assert not due
  596. assert remaining == 60
  597. def test_second_quarter_execution_is_not_due(self):
  598. with patch_crontab_nowfun(
  599. self.quarterly, datetime(2010, 5, 10, 10, 29)):
  600. due, remaining = self.quarterly.is_due(
  601. datetime(2010, 5, 10, 10, 15),
  602. )
  603. assert not due
  604. assert remaining == 60
  605. def test_daily_execution_is_due(self):
  606. with patch_crontab_nowfun(self.daily, datetime(2010, 5, 10, 7, 30)):
  607. due, remaining = self.daily.is_due(datetime(2010, 5, 9, 7, 30))
  608. assert due
  609. assert remaining == 24 * 60 * 60
  610. def test_daily_execution_is_not_due(self):
  611. with patch_crontab_nowfun(self.daily, datetime(2010, 5, 10, 10, 30)):
  612. due, remaining = self.daily.is_due(datetime(2010, 5, 10, 7, 30))
  613. assert not due
  614. assert remaining == 21 * 60 * 60
  615. def test_weekly_execution_is_due(self):
  616. with patch_crontab_nowfun(self.weekly, datetime(2010, 5, 6, 7, 30)):
  617. due, remaining = self.weekly.is_due(datetime(2010, 4, 30, 7, 30))
  618. assert due
  619. assert remaining == 7 * 24 * 60 * 60
  620. def test_weekly_execution_is_not_due(self):
  621. with patch_crontab_nowfun(self.weekly, datetime(2010, 5, 7, 10, 30)):
  622. due, remaining = self.weekly.is_due(datetime(2010, 5, 6, 7, 30))
  623. assert not due
  624. assert remaining == 6 * 24 * 60 * 60 - 3 * 60 * 60
  625. def test_monthly_execution_is_due(self):
  626. with patch_crontab_nowfun(self.monthly, datetime(2010, 5, 13, 7, 30)):
  627. due, remaining = self.monthly.is_due(datetime(2010, 4, 8, 7, 30))
  628. assert due
  629. assert remaining == 28 * 24 * 60 * 60
  630. def test_monthly_execution_is_not_due(self):
  631. with patch_crontab_nowfun(self.monthly, datetime(2010, 5, 9, 10, 30)):
  632. due, remaining = self.monthly.is_due(datetime(2010, 4, 8, 7, 30))
  633. assert not due
  634. assert remaining == 4 * 24 * 60 * 60 - 3 * 60 * 60
  635. def test_monthly_moy_execution_is_due(self):
  636. with patch_crontab_nowfun(
  637. self.monthly_moy, datetime(2014, 2, 26, 22, 0)):
  638. due, remaining = self.monthly_moy.is_due(
  639. datetime(2013, 7, 4, 10, 0),
  640. )
  641. assert due
  642. assert remaining == 60.0
  643. @skip.todo('unstable test')
  644. def test_monthly_moy_execution_is_not_due(self):
  645. with patch_crontab_nowfun(
  646. self.monthly_moy, datetime(2013, 6, 28, 14, 30)):
  647. due, remaining = self.monthly_moy.is_due(
  648. datetime(2013, 6, 28, 22, 14),
  649. )
  650. assert not due
  651. attempt = (
  652. time.mktime(datetime(2014, 2, 26, 22, 0).timetuple()) -
  653. time.mktime(datetime(2013, 6, 28, 14, 30).timetuple()) -
  654. 60 * 60
  655. )
  656. assert remaining == attempt
  657. def test_monthly_moy_execution_is_due2(self):
  658. with patch_crontab_nowfun(
  659. self.monthly_moy, datetime(2014, 2, 26, 22, 0)):
  660. due, remaining = self.monthly_moy.is_due(
  661. datetime(2013, 2, 28, 10, 0),
  662. )
  663. assert due
  664. assert remaining == 60.0
  665. def test_monthly_moy_execution_is_not_due2(self):
  666. with patch_crontab_nowfun(
  667. self.monthly_moy, datetime(2014, 2, 26, 21, 0)):
  668. due, remaining = self.monthly_moy.is_due(
  669. datetime(2013, 6, 28, 22, 14),
  670. )
  671. assert not due
  672. attempt = 60 * 60
  673. assert remaining == attempt
  674. def test_yearly_execution_is_due(self):
  675. with patch_crontab_nowfun(self.yearly, datetime(2010, 3, 11, 7, 30)):
  676. due, remaining = self.yearly.is_due(datetime(2009, 3, 12, 7, 30))
  677. assert due
  678. assert remaining == 364 * 24 * 60 * 60
  679. def test_yearly_execution_is_not_due(self):
  680. with patch_crontab_nowfun(self.yearly, datetime(2010, 3, 7, 10, 30)):
  681. due, remaining = self.yearly.is_due(datetime(2009, 3, 12, 7, 30))
  682. assert not due
  683. assert remaining == 4 * 24 * 60 * 60 - 3 * 60 * 60