1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
import calendar import datetime import os import site import sys import time import unittest
MINUTE = 60 HOUR = 60 * MINUTE DAY = 24 * HOUR
if isinstance(map, type): xrange = range
sys.path.insert(0, os.path.join( os.path.dirname(os.path.realpath(__file__)), os.pardir))
import cronex
class test_testedmodule(unittest.TestCase): def test_substitution(self): """ Verify that the special substitutions are replaced by the correct, expanded cron expressions. """ testcases = [("@yearly", "0 0 1 1 *"), ("@annually", "0 0 1 1 *"), ("@monthly", "0 0 1 * *"), ("@weekly", "0 0 * * 0"), ("@daily", "0 0 * * *"), ("@midnight", "0 0 * * *"), ("@hourly", "0 * * * *")] for a, b in testcases: obj = cronex.CronExpression(a) self.assertTrue(b in repr(obj))
def test_compute_numtab(self): """ Verify that calling compute_numtab after modifying the string-version of an expression results in the static trigger-value cache being updated. """ testex1 = cronex.CronExpression("*/7 5-10 5 * *") testex2 = cronex.CronExpression("*/5 23-2 5 8 *") self.assertNotEqual(testex1.string_tab, testex2.string_tab) self.assertNotEqual(testex1.numerical_tab, testex2.numerical_tab) testex1.string_tab = testex2.string_tab testex1.compute_numtab() self.assertEqual(testex1.string_tab, testex2.string_tab) self.assertEqual(testex1.numerical_tab, testex2.numerical_tab)
def test_periodics_dom(self): """ Verify that arbitrary-period repeaters in the day-of-the-month field work as expected. """ now = int(time.time()) then = time.gmtime(now - 491 * DAY) now_tuple = time.gmtime(now) testex = cronex.CronExpression("* * %491 * *") self.assertFalse(testex.check_trigger(now_tuple[:5])) testex.epoch = tuple(list(then[:5]) + [0]) self.assertTrue(testex.check_trigger(now_tuple[:5])) self.assertTrue(testex.check_trigger(then[:5]))
def test_periodics_hours(self): """ Verify that arbitrary-period repeaters in the hours field work as expected. """ now = int(time.time()) then = time.gmtime(now - 9001 * HOUR) now_tuple = time.gmtime(now) testex = cronex.CronExpression("* %9001 * * *") self.assertFalse(testex.check_trigger(now_tuple[:5])) testex.epoch = tuple(list(then[:5]) + [0]) self.assertTrue(testex.check_trigger(now_tuple[:5])) self.assertTrue(testex.check_trigger(then[:5]))
def test_periodics_minutes(self): """ Verify that arbitrary-period repeaters in the minutes field work as expected. """ now = int(time.time()) then = time.gmtime(now - 814075 * MINUTE) now_tuple = time.gmtime(now) testex = cronex.CronExpression("%814075 * * * *") self.assertFalse(testex.check_trigger(now_tuple[:5])) testex.epoch = tuple(list(then[:5]) + [0]) self.assertTrue(testex.check_trigger(now_tuple[:5])) self.assertTrue(testex.check_trigger(then[:5]))
def test_periodics_month(self): """ Verify that arbitrary-period repeaters in the month field work as expected. More specifically, it verifies that the repeaters are triggered every "N" calendar months regardless of the day for triggers set to go offer every 1 month, ever 2 months, etc... through 18 months from 1970 through 1980. """ for period in range(2, 18): calendar_months = 0 description = "* * * %%%i *" % period cron_expression = cronex.CronExpression(description)
for year in range(1970, 1980): for month in range(1, 13): days_in_month = calendar.monthrange(year, month)[-1] should_trigger = not (calendar_months % period)
for day in range(1, days_in_month + 1): time_tuple = (year, month, day, 0, 0) triggered = cron_expression.check_trigger(time_tuple) self.assertEqual(should_trigger, triggered, "%s: expected trigger to be %r for %r" % (description, should_trigger, time_tuple) )
calendar_months += 1
def test_parse_atom(self): """ Verify that parsing atoms returns sets containing all of the expected values. """ input_expect = [ (('5-10', (1, 20)), set([5, 6, 7, 8, 9, 10])), (('10-5', (1, 12)), set([10, 11, 12, 1, 2, 3, 4, 5])), (('5-10/2', (1, 20)), set([5, 7, 9])), (('10-5/2', (1, 12)), set([10, 12, 2, 4])), (('11-5/2', (1, 12)), set([11, 1, 3, 5])), (('10-5/3', (1, 11)), set([10, 2, 5])), (('11-5/3', (1, 14)), set([11, 14, 3])), (('*', (1, 100)), set(xrange(1, 101))), (('*/5', (1, 100)), set(xrange(1, 101, 5))), (('666', (1, 1000)), set([666])), (('21-1', (0, 23)), set([21, 22, 23, 0, 1])), (('5/15', (0, 60)), set([5, 20, 35, 50])), (('1/4', (0, 23)), set([1, 5, 9, 13, 17, 21])), ]
for give_the_function, want_from_function in input_expect: self.assertEqual(want_from_function, cronex.parse_atom(*give_the_function))
def test_str_and_repr(self): """ Verify that the __repr__ and __str__ return values can be passed to eval to generate an identical CronExpression. """ CronExpression = cronex.CronExpression testex1 = cronex.CronExpression("*/15 4 1-7 * * TEST___TEST") testex2 = eval(repr(testex1)) self.assertEqual(testex1.string_tab, testex2.string_tab) self.assertEqual(testex1.numerical_tab, testex2.numerical_tab) self.assertEqual(testex1.comment, testex2.comment) self.assertEqual(repr(testex1), str(testex1))
def test_dom_substitution(self): """ Verify that shortened month names are correctly translated numeric indexes. """ testex1 = cronex.CronExpression( "* * * jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec *") testex2 = cronex.CronExpression("* * * 1,2,3,4,5,6,7,8,9,10,11,12 *") self.assertEqual(repr(testex1), repr(testex2))
def test_dow_substitution(self): """ Verify that shortened days-of-the-week are correctly translated numeric indexes. """ testex1 = cronex.CronExpression("* * * * sun,mon,tue,wed,thu,fri,sat") testex2 = cronex.CronExpression("* * * * 0,1,2,3,4,5,6") self.assertEqual(repr(testex1), repr(testex2))
def test_dom_either_or_dow(self): """ Verify that combining day of-the-month field and day-of-the-week field triggers on either condition matching the current day and that using a wild-card day of the month with a day of the week entry doesn't trigger every single day. """ testex = cronex.CronExpression("0 0 5 * mon") for e in xrange(1, 30): if e in (1, 5, 8, 15, 22, 29): self.assertTrue(testex.check_trigger((2010, 11, e, 0, 0))) else: self.assertFalse(testex.check_trigger((2010, 11, e, 0, 0)))
testex = cronex.CronExpression("0 0 * * wed") for d in xrange(1, 32): if not(d % 7): self.assertTrue(testex.check_trigger((2010, 7, d, 0, 0))) else: self.assertFalse(testex.check_trigger((2010, 7, d, 0, 0)))
def test_L_in_dow(self): """ Verify that having L in the day-of-the-week field with a number always triggers on last occurrence of the corresponding day of the week in any given month. """ testex = cronex.CronExpression("0 0 * * 6L") tv = [30, 27, 27, 24, 29, 26, 31, 28, 25, 30, 27, 25] for v in xrange(0, 12): self.assertTrue((testex.check_trigger((2010, v+1, tv[v], 0, 0))))
def test_L_in_dom(self): """ Verify that having L in the day-of-the-month field always triggers on the last day of the month. """ testex = cronex.CronExpression("0 0 L * *") for y in xrange(2000, 2009): for v in xrange(1, 13): lastdom = calendar.monthrange(y, v)[-1] for d in xrange(1, lastdom + 1): if d < lastdom: self.assertFalse(testex.check_trigger((y, v, d, 0, 0))) else: self.assertTrue(testex.check_trigger((y, v, d, 0, 0)))
def test_calendar_change_vs_hour_change(self): """ Verify that a periodic trigger for the day of the month is based on calendar days, not 24-hour days. """ epoch = (2010, 11, 16, 23, 59) local_time = (2010, 11, 18, 0, 0) testex = cronex.CronExpression("0 0 %2 * *", epoch, -6) self.assertTrue(testex.check_trigger(local_time, -6))
def test_asterisk_is_loney(self): """ Verify that asterisk cannot be combined with other atoms. """ self.assertRaises(ValueError, cronex.CronExpression, "* *,1-9 * * *")
def test_dow_occurence(self): """ Verify that using "#" to find the Nth occurrence of a given day of the week works correctly. """ for dow in xrange(0, 7): for occurence in (1, 6): day = (7 * (occurence - 1)) + dow + 1 expression = "0 0 * * %i#%i" % (dow, occurence) if occurence > 5: self.assertRaises(ValueError, cronex.CronExpression, expression) elif day < 32: testex = cronex.CronExpression(expression) self.assertTrue(testex.check_trigger( (2011, 5, day, 0, 0))) if day > 8: self.assertFalse(testex.check_trigger( (2011, 5, max(day - 7, 1), 0, 0))) elif day < 25: self.assertFalse(testex.check_trigger( (2011, 5, max(day + 7, 1), 0, 0)))
def test_strict_range_bounds(self): """ Verify that having numbers outside of the reasonable ranges for each field raises an exception. Tests both upper and lower bounds. """ self.assertRaises(ValueError, cronex.CronExpression, "1000 * * * *") self.assertRaises(ValueError, cronex.CronExpression, "* 1000 * * *") self.assertRaises(ValueError, cronex.CronExpression, "* * 1000 * *") self.assertRaises(ValueError, cronex.CronExpression, "* * * 1000 *") self.assertRaises(ValueError, cronex.CronExpression, "* * * * 1000") self.assertRaises(ValueError, cronex.CronExpression, "-1 * * * *") self.assertRaises(ValueError, cronex.CronExpression, "* -1 * * *") self.assertRaises(ValueError, cronex.CronExpression, "* * 0 * *") self.assertRaises(ValueError, cronex.CronExpression, "* * * 0 *") self.assertRaises(ValueError, cronex.CronExpression, "* * * * -1")
def test_catches_bad_modulus(self): """ Verify that having a trigger with an invalid repetition period raises an exception. """ badstuff = [ "* * * * %-1", "%1 * * * *", "* %1 * * *", "* * %1 * *", "* * * %1 *", "* * * * %1", "%0 * * * *", "* %0 * * *", "* * %0 * *", "* * * %0 *", "* * * * %0" ] for case in badstuff: self.assertRaises(ValueError, cronex.CronExpression, case)
def test_catches_bad_W(self): """ Verify that having a syntactically invalid "W" raises an exception. """ badstuff = [ "5W * * * *", "* 5W * * *", "* * 99W * *", "* * 0W * *", "* * W0 * *", "* * * 5W *", "* * * * 5W", ] for case in badstuff: self.assertRaises(ValueError, cronex.CronExpression, case)
def test_catches_bad_L(self): """ Verify that having a syntactically invalid "L" raises an exception. """ badstuff = [ "L * * * *", "* L * * *", "* * 99L * *", "* * 0L * *", "* * * L *", "* * * * L", "* * * * 9L", "* * * * -9L"] for case in badstuff: self.assertRaises(ValueError, cronex.CronExpression, case)
def test_catches_bad_Pound(self): """ Verify that having a syntactically invalid "#" raises an exception. """ badstuff = [ "# * * * *", "* # * * *", "* * # * *", "* * * # *", "* * * * 9#9L"] for case in badstuff: self.assertRaises(ValueError, cronex.CronExpression, case)
def test_fail_on_not_enough_fields(self): """ Verify that an exception is thrown when the cron expression has too few fields. """ badstuff = ["*", "* *", "* * *", "* * * *"] for case in badstuff: self.assertRaises(ValueError, cronex.CronExpression, case)
if __name__ == "__main__": unittest.main()
|