def test_last_weekday(timezone, serializer): start_time = datetime(2020, 1, 1, tzinfo=timezone) trigger = CronTrigger(year=2020, day='last sun', start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == datetime(2020, 1, 26, tzinfo=timezone) assert trigger.next() == datetime(2020, 2, 23, tzinfo=timezone) assert trigger.next() == datetime(2020, 3, 29, tzinfo=timezone) assert repr(trigger) == ("CronTrigger(year='2020', month='*', day='last sun', week='*', " "day_of_week='*', hour='0', minute='0', second='0', " "start_time='2020-01-01T00:00:00+01:00', timezone='Europe/Berlin')")
def test_week_1(timezone, serializer): start_time = datetime(2009, 1, 1, tzinfo=timezone) trigger = CronTrigger(year=2009, month=2, week=8, start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) for day in range(16, 23): assert trigger.next() == datetime(2009, 2, day, tzinfo=timezone) assert trigger.next() is None assert repr(trigger) == ("CronTrigger(year='2009', month='2', day='*', week='8', " "day_of_week='*', hour='0', minute='0', second='0', " "start_time='2009-01-01T00:00:00+01:00', timezone='Europe/Berlin')")
def test_cron_trigger_3(timezone, serializer): start_time = datetime(2009, 1, 1, tzinfo=timezone) trigger = CronTrigger(year='2009', month='feb-dec', hour='8-9', start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == datetime(2009, 2, 1, 8, tzinfo=timezone) assert trigger.next() == datetime(2009, 2, 1, 9, tzinfo=timezone) assert trigger.next() == datetime(2009, 2, 2, 8, tzinfo=timezone) assert repr(trigger) == ("CronTrigger(year='2009', month='feb-dec', day='*', week='*', " "day_of_week='*', hour='8-9', minute='0', second='0', " "start_time='2009-01-01T00:00:00+01:00', timezone='Europe/Berlin')")
def test_cron_trigger_2(timezone, serializer): start_time = datetime(2009, 10, 14, tzinfo=timezone) trigger = CronTrigger(year='2009/2', month='1-3', day='5', start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == datetime(2011, 1, 5, tzinfo=timezone) assert trigger.next() == datetime(2011, 2, 5, tzinfo=timezone) assert trigger.next() == datetime(2011, 3, 5, tzinfo=timezone) assert trigger.next() == datetime(2013, 1, 5, tzinfo=timezone) assert repr(trigger) == ("CronTrigger(year='2009/2', month='1-3', day='5', week='*', " "day_of_week='*', hour='0', minute='0', second='0', " "start_time='2009-10-14T00:00:00+02:00', timezone='Europe/Berlin')")
def test_weekday_range(timezone, serializer): start_time = timezone.localize(datetime(2020, 1, 1)) trigger = CronTrigger(year=2020, month=1, week=1, day_of_week='fri-sun', start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == timezone.localize(datetime(2020, 1, 3)) assert trigger.next() == timezone.localize(datetime(2020, 1, 4)) assert trigger.next() == timezone.localize(datetime(2020, 1, 5)) assert trigger.next() is None assert repr(trigger) == ("CronTrigger(year='2020', month='1', day='*', week='1', " "day_of_week='fri-sun', hour='0', minute='0', second='0', " "start_time='2020-01-01T00:00:00+01:00', timezone='Europe/Berlin')")
def test_cron_trigger_1(timezone, serializer): start_time = datetime(2008, 12, 1, tzinfo=timezone) trigger = CronTrigger(year='2009/2', month='1-4/3', day='5-6', start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == datetime(2009, 1, 5, tzinfo=timezone) assert trigger.next() == datetime(2009, 1, 6, tzinfo=timezone) assert trigger.next() == datetime(2009, 4, 5, tzinfo=timezone) assert trigger.next() == datetime(2009, 4, 6, tzinfo=timezone) assert trigger.next() == datetime(2011, 1, 5, tzinfo=timezone) assert repr(trigger) == ("CronTrigger(year='2009/2', month='1-4/3', day='5-6', week='*', " "day_of_week='*', hour='0', minute='0', second='0', " "start_time='2008-12-01T00:00:00+01:00', timezone='Europe/Berlin')")
def test_month_rollover(timezone, serializer): start_time = datetime(2016, 2, 1, tzinfo=timezone) trigger = CronTrigger(day=30, start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == datetime(2016, 3, 30, tzinfo=timezone) assert trigger.next() == datetime(2016, 4, 30, tzinfo=timezone)
def test_year_list(timezone, serializer): start_time = datetime(2009, 1, 1, tzinfo=timezone) trigger = CronTrigger(year='2009,2008', start_time=start_time, timezone=timezone) assert repr(trigger) == "CronTrigger(year='2009,2008', month='1', day='1', week='*', " \ "day_of_week='*', hour='0', minute='0', second='0', " \ "start_time='2009-01-01T00:00:00+01:00', timezone='Europe/Berlin')" assert trigger.next() == datetime(2009, 1, 1, tzinfo=timezone) assert trigger.next() is None
def test_weekday_positional(timezone, serializer): start_time = datetime(2009, 1, 1, tzinfo=timezone) trigger = CronTrigger(year=2009, month=1, day='4th wed', start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == datetime(2009, 1, 28, tzinfo=timezone) assert repr(trigger) == ("CronTrigger(year='2009', month='1', day='4th wed', week='*', " "day_of_week='*', hour='0', minute='0', second='0', " "start_time='2009-01-01T00:00:00+01:00', timezone='Europe/Berlin')")
def test_weekday_overlap(timezone, serializer, expr): start_time = timezone.localize(datetime(2009, 1, 1)) trigger = CronTrigger(year=2009, month=1, day='6-10', day_of_week=expr, start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == timezone.localize(datetime(2009, 1, 7)) assert repr(trigger) == ("CronTrigger(year='2009', month='1', day='6-10', week='*', " "day_of_week='wed-fri', hour='0', minute='0', second='0', " "start_time='2009-01-01T00:00:00+01:00', timezone='Europe/Berlin')")
def test_weekday_nomatch(timezone, serializer, weekday): start_time = datetime(2009, 1, 1, tzinfo=timezone) trigger = CronTrigger(year=2009, month=1, day='6-10', day_of_week=weekday, start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() is None assert repr(trigger) == ("CronTrigger(year='2009', month='1', day='6-10', week='*', " "day_of_week='mon,sun', hour='0', minute='0', second='0', " "start_time='2009-01-01T00:00:00+01:00', timezone='Europe/Berlin')")
def test_cron_trigger_4(timezone, serializer): start_time = timezone.localize(datetime(2012, 2, 1)) trigger = CronTrigger(year='2012', month='2', day='last', start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == timezone.localize(datetime(2012, 2, 29)) assert repr(trigger) == ("CronTrigger(year='2012', month='2', day='last', week='*', " "day_of_week='*', hour='0', minute='0', second='0', " "start_time='2012-02-01T00:00:00+01:00', timezone='Europe/Berlin')")
def test_week_2(timezone, serializer, weekday): start_time = timezone.localize(datetime(2009, 1, 1)) trigger = CronTrigger(year=2009, week=15, day_of_week=weekday, start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == timezone.localize(datetime(2009, 4, 8)) assert trigger.next() is None assert repr(trigger) == ("CronTrigger(year='2009', month='*', day='*', week='15', " "day_of_week='wed', hour='0', minute='0', second='0', " "start_time='2009-01-01T00:00:00+01:00', timezone='Europe/Berlin')")
def test_dst_change(trigger_args, start_time, start_time_dst, correct_next_date, serializer): """ Making sure that CronTrigger works correctly when crossing the DST switch threshold. Note that you should explicitly compare datetimes as strings to avoid the internal datetime comparison which would test for equality in the UTC timezone. """ timezone = pytz.timezone('US/Eastern') start_time = timezone.localize(start_time, is_dst=start_time_dst) trigger = CronTrigger(timezone=timezone, start_time=start_time, **trigger_args) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == timezone.localize(correct_next_date, is_dst=not start_time_dst)
def test_end_time(timezone, serializer): """Test that next() won't produce""" start_time = datetime(2014, 4, 13, 2, tzinfo=timezone) end_time = datetime(2014, 4, 13, 4, tzinfo=timezone) trigger = CronTrigger(hour=4, start_time=start_time, end_time=end_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == datetime(2014, 4, 13, 4, tzinfo=timezone) assert trigger.next() is None assert repr(trigger) == ("CronTrigger(year='*', month='*', day='*', week='*', " "day_of_week='*', hour='4', minute='0', second='0', " "start_time='2014-04-13T02:00:00+02:00', " "end_time='2014-04-13T04:00:00+02:00', timezone='Europe/Berlin')")
def test_increment_weekday(timezone, serializer): """ Tests that incrementing the weekday field in the process of calculating the next matching date won't cause problems. """ start_time = datetime(2009, 9, 25, 7, tzinfo=timezone) trigger = CronTrigger(hour='5-6', start_time=start_time, timezone=timezone) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == datetime(2009, 9, 26, 5, tzinfo=timezone) assert repr(trigger) == ("CronTrigger(year='*', month='*', day='*', week='*', " "day_of_week='*', hour='5-6', minute='0', second='0', " "start_time='2009-09-25T07:00:00+02:00', timezone='Europe/Berlin')")
def test_dst_change(trigger_args, start_time, start_time_fold, correct_next_date, correct_next_date_fold, serializer): """ Making sure that CronTrigger works correctly when crossing the DST switch threshold. Note that you should explicitly compare datetimes as strings to avoid the internal datetime comparison which would test for equality in the UTC timezone. """ timezone = ZoneInfo('US/Eastern') start_time = start_time.replace(tzinfo=timezone, fold=start_time_fold) trigger = CronTrigger(timezone=timezone, start_time=start_time, **trigger_args) if serializer: trigger = serializer.deserialize(serializer.serialize(trigger)) assert trigger.next() == correct_next_date.replace(tzinfo=timezone, fold=correct_next_date_fold)