test_schedules.py 28 KB

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