def test_create_cron_schedule_with_invalid_cron_string_raises_error(self): with pytest.raises(Exception): schedules.CronSchedule("*") with pytest.raises(Exception): schedules.CronSchedule("hello world") with pytest.raises(Exception): schedules.CronSchedule(1) with pytest.raises(Exception): schedules.CronSchedule("* * 32 1 1")
def list_of_schedules(self): sd = pendulum.datetime(2000, 1, 1) ed = pendulum.datetime(2010, 1, 1) list_sched = [] list_sched.append( schedules.IntervalSchedule(interval=timedelta(hours=1), start_date=sd, end_date=ed)) sd = pendulum.datetime(2004, 1, 1) ed = pendulum.datetime(2017, 1, 1) list_sched.append( schedules.CronSchedule("0 0 * * *", start_date=sd, end_date=ed)) list_sched.append(schedules.CronSchedule("0 9 * * 1-5")) return list_sched
def test_cron_schedule_when_start_date_and_after_doesnt_align_with_schedule( self): every_day = "0 0 * * *" start_date = pendulum.datetime(2050, 1, 1, 6, 0, 0) s = schedules.CronSchedule(every_day, start_date=start_date) assert s.next(1, after=pendulum.datetime( 2050, 1, 1, 7, 0, 0)) == [pendulum.datetime(2050, 1, 2, 0, 0, 0)]
def test_cron_schedule_end_date(self): every_day = "0 0 * * *" start_date = pendulum.datetime(2050, 1, 1) end_date = pendulum.datetime(2050, 1, 2) s = schedules.CronSchedule(every_day, start_date=start_date, end_date=end_date) assert s.next(3) == [start_date, start_date.add(days=1)]
def test_cron_schedule_next_n(self): every_day = "0 0 * * *" s = schedules.CronSchedule(every_day) assert s.next(3) == [ pendulum.today("UTC").add(days=1), pendulum.today("UTC").add(days=2), pendulum.today("UTC").add(days=3), ]
def test_cron_schedule_end_daylight_savings_time(self): """ 11/4/2018, at 2am, America/New_York switched clocks back an hour. """ dt = pendulum.datetime(2018, 11, 4, tz="America/New_York") every_hour = "0 * * * *" s = schedules.CronSchedule(every_hour) next_4 = s.next(4, after=dt) assert [t.in_tz("UTC").hour for t in next_4] == [5, 6, 7, 8]
def test_serialize_cron_schedule(self): schedule = schedules.CronSchedule("0 0 * * *") assert schedule.serialize() == { "end_date": None, "start_date": None, "type": "CronSchedule", "cron": "0 0 * * *", "__version__": __version__, }
def test_cron_schedule_start_daylight_savings_time(self): """ On 3/11/2018, at 2am, America/New_York switched clocks forward an hour. """ dt = pendulum.datetime(2018, 3, 11, tz="America/New_York") every_hour = "0 * * * *" s = schedules.CronSchedule(every_hour) next_4 = s.next(4, after=dt) assert [t.in_tz("UTC").hour for t in next_4] == [6, 7, 8, 9]
def test_cron_schedule_next_n_with_after_argument(self): every_day = "0 0 * * *" s = schedules.CronSchedule(every_day) start_date = pendulum.datetime(2018, 1, 1) assert s.next(3, after=start_date) == [ start_date.add(days=1), start_date.add(days=2), start_date.add(days=3), ]
def test_serialize_cron_schedule_with_start_and_end_dates(self): sd = pendulum.datetime(2000, 1, 1) ed = pendulum.datetime(2010, 1, 1) schedule = schedules.CronSchedule("0 0 * * *", start_date=sd, end_date=ed) assert schedule.serialize() == { "start_date": serialize_fmt(sd), "end_date": serialize_fmt(ed), "type": "CronSchedule", "cron": "0 0 * * *", "__version__": __version__, }
def test_next_n_with_different_timezones(self): east = schedules.CronSchedule("0 9 * * 1-5", start_date=pendulum.parse( "2019-03-14", tz="US/Eastern")) west = schedules.CronSchedule("30 6 * * 1-5", start_date=pendulum.parse( "2019-03-14", tz="US/Pacific")) main = schedules.UnionSchedule([east, west]) ## have to do some additional work so this test passes ## regardless of what time it is now = pendulum.now("US/Pacific") after = east.next(1, after=now)[0] + pendulum.duration(minutes=-1) expected = [ east.next(1, after=after)[0], west.next(1, after=after)[0], east.next(2, after=after)[1], west.next(2, after=after)[1], ] assert main.next(4, after=after) == expected
def test_cron_schedule_hourly_daylight_savings_time_backward( self, serialize): """ 11/4/2018, at 2am, America/New_York switched clocks back an hour. """ dt = pendulum.datetime(2018, 11, 3, 23, tz="America/New_York") s = schedules.CronSchedule("0 * * * *", dt) if serialize: s = ScheduleSchema().load(s.serialize()) next_4 = s.next(4, after=dt) # repeat the 1am run in local time assert [t.in_tz("America/New_York").hour for t in next_4] == [0, 1, 2, 3] # runs every hour UTC assert [t.in_tz("UTC").hour for t in next_4] == [4, 6, 7, 8]
def test_cron_schedule_hourly_daylight_savings_time_forward( self, serialize): """ On 3/11/2018, at 2am, America/New_York switched clocks forward an hour. """ dt = pendulum.datetime(2018, 3, 10, 23, tz="America/New_York") s = schedules.CronSchedule("0 * * * *", dt) if serialize: s = ScheduleSchema().load(s.serialize()) next_4 = s.next(4, after=dt) # skip 2am assert [t.in_tz("America/New_York").hour for t in next_4] == [0, 1, 3, 4] # constant hourly schedule in utc time assert [t.in_tz("UTC").hour for t in next_4] == [5, 6, 7, 8]
def test_cron_schedule_daily_start_daylight_savings_time_backward( self, serialize): """ On 11/4/2018, at 2am, America/New_York switched clocks back an hour. Confirm that a schedule for 9am America/New_York stays 9am through the switch. """ dt = pendulum.datetime(2018, 11, 1, 9, tz="America/New_York") s = schedules.CronSchedule("0 9 * * *", dt) if serialize: s = ScheduleSchema().load(s.serialize()) next_4 = s.next(4, after=dt) # constant 9am start assert [t.in_tz("America/New_York").hour for t in next_4] == [9, 9, 9, 9] assert [t.in_tz("UTC").hour for t in next_4] == [13, 13, 14, 14]
def test_create_cron_schedule(self): assert schedules.CronSchedule("* * * * *")
def test_cron_schedule_n_is_negative(self): start_date = pendulum.datetime(2018, 1, 1) s = schedules.CronSchedule(start_date=start_date, cron="0 0 * * *") assert s.next(-3) == []
def test_cron_schedule_n_equals_0(self): start_date = pendulum.datetime(2018, 1, 1) s = schedules.CronSchedule(start_date=start_date, cron="0 0 * * *") assert s.next(0) == []
def test_cron_schedule_respects_microseconds(self): every_day = "0 0 * * *" start_date = pendulum.datetime(2050, 1, 1, 0, 0, 0, 1) s = schedules.CronSchedule(every_day, start_date=start_date) assert s.next(1) == [pendulum.datetime(2050, 1, 2)]
def cron_schedule(): return schedules.CronSchedule( cron="0 0 * * *", start_date=datetime.datetime(2020, 1, 1), end_date=datetime.datetime(2020, 5, 1), )