test_schedules.py 27 KB

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