test_schedules.py 28 KB

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