|
| 1 | +import unittest |
| 2 | +from datetime import datetime, time |
| 3 | + |
| 4 | +from dbtasks.crontab import Crontab, hour, minute, month |
| 5 | + |
| 6 | + |
| 7 | +class CrontabTests(unittest.TestCase): |
| 8 | + def test_parts(self): |
| 9 | + self.assertEqual(minute.parse("30"), [30]) |
| 10 | + self.assertEqual(minute.parse("7-12,42"), [7, 8, 9, 10, 11, 12, 42]) |
| 11 | + self.assertEqual(len(minute.parse("~")), 1) |
| 12 | + self.assertEqual(minute.parse("*/15"), [0, 15, 30, 45]) |
| 13 | + self.assertEqual(minute.parse("30-45/3"), [30, 33, 36, 39, 42, 45]) |
| 14 | + self.assertEqual(hour.parse("*/23"), [0, 23]) |
| 15 | + self.assertEqual(month.parse("oct,february,jun"), [2, 6, 10]) |
| 16 | + self.assertEqual(month.parse("jan-jun/2"), [1, 3, 5]) |
| 17 | + |
| 18 | + def test_crontab(self): |
| 19 | + # 4:30am on the 1st and 15th of each month |
| 20 | + c = Crontab("30 4 1,15 * *") |
| 21 | + matches = list(c.dates(after=datetime(2025, 1, 1), until=datetime(2026, 1, 1))) |
| 22 | + self.assertEqual(len(matches), 12 * 2) |
| 23 | + self.assertTrue(all(d.day in (1, 15) for d in matches)) |
| 24 | + self.assertTrue(all(d.time() == time(4, 30) for d in matches)) |
0 commit comments