test_crontab.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import datetime
  2. from huey import crontab
  3. from huey.tests.base import BaseTestCase
  4. class CrontabTestCase(BaseTestCase):
  5. def test_crontab_month(self):
  6. # validates the following months, 1, 4, 7, 8, 9
  7. valids = [1, 4, 7, 8, 9]
  8. validate_m = crontab(month='1,4,*/6,8-9')
  9. for x in range(1, 13):
  10. res = validate_m(datetime.datetime(2011, x, 1))
  11. self.assertEqual(res, x in valids)
  12. def test_crontab_day(self):
  13. # validates the following days
  14. valids = [1, 4, 7, 8, 9, 13, 19, 25, 31]
  15. validate_d = crontab(day='*/6,1,4,8-9')
  16. for x in range(1, 32):
  17. res = validate_d(datetime.datetime(2011, 1, x))
  18. self.assertEqual(res, x in valids)
  19. def test_crontab_hour(self):
  20. # validates the following hours
  21. valids = [0, 1, 4, 6, 8, 9, 12, 18]
  22. validate_h = crontab(hour='8-9,*/6,1,4')
  23. for x in range(24):
  24. res = validate_h(datetime.datetime(2011, 1, 1, x))
  25. self.assertEqual(res, x in valids)
  26. edge = crontab(hour=0)
  27. self.assertTrue(edge(datetime.datetime(2011, 1, 1, 0, 0)))
  28. self.assertFalse(edge(datetime.datetime(2011, 1, 1, 12, 0)))
  29. def test_crontab_minute(self):
  30. # validates the following minutes
  31. valids = [0, 1, 4, 6, 8, 9, 12, 18, 24, 30, 36, 42, 48, 54]
  32. validate_m = crontab(minute='4,8-9,*/6,1')
  33. for x in range(60):
  34. res = validate_m(datetime.datetime(2011, 1, 1, 1, x))
  35. self.assertEqual(res, x in valids)
  36. def test_crontab_day_of_week(self):
  37. # validates the following days of week
  38. # jan, 1, 2011 is a saturday
  39. valids = [2, 4, 9, 11, 16, 18, 23, 25, 30]
  40. validate_dow = crontab(day_of_week='0,2')
  41. for x in range(1, 32):
  42. res = validate_dow(datetime.datetime(2011, 1, x))
  43. self.assertEqual(res, x in valids)
  44. def test_crontab_sunday(self):
  45. for dow in ('0', '7'):
  46. validate = crontab(day_of_week=dow, hour='0', minute='0')
  47. valid = set((2, 9, 16, 23, 30))
  48. for x in range(1, 32):
  49. if x in valid:
  50. self.assertTrue(validate(datetime.datetime(2011, 1, x)))
  51. else:
  52. self.assertFalse(validate(datetime.datetime(2011, 1, x)))
  53. def test_crontab_all_together(self):
  54. # jan 1, 2011 is a saturday
  55. # may 1, 2011 is a sunday
  56. validate = crontab(
  57. month='1,5',
  58. day='1,4,7',
  59. day_of_week='0,6',
  60. hour='*/4',
  61. minute='1-5,10-15,50'
  62. )
  63. self.assertTrue(validate(datetime.datetime(2011, 5, 1, 4, 11)))
  64. self.assertTrue(validate(datetime.datetime(2011, 5, 7, 20, 50)))
  65. self.assertTrue(validate(datetime.datetime(2011, 1, 1, 0, 1)))
  66. # fails validation on month
  67. self.assertFalse(validate(datetime.datetime(2011, 6, 4, 4, 11)))
  68. # fails validation on day
  69. self.assertFalse(validate(datetime.datetime(2011, 1, 6, 4, 11)))
  70. # fails validation on day_of_week
  71. self.assertFalse(validate(datetime.datetime(2011, 1, 4, 4, 11)))
  72. # fails validation on hour
  73. self.assertFalse(validate(datetime.datetime(2011, 1, 1, 1, 11)))
  74. # fails validation on minute
  75. self.assertFalse(validate(datetime.datetime(2011, 1, 1, 4, 6)))
  76. def test_invalid_crontabs(self):
  77. # check invalid configurations are detected and reported
  78. self.assertRaises(ValueError, crontab, minute='61')
  79. self.assertRaises(ValueError, crontab, minute='0-61')