def test_serialize_rruleset_clocks(): s = ("DTSTART:20120201T023000Z\n" "RRULE:FREQ=MONTHLY;COUNT=5\n" "RDATE:20120701T023000Z,20120702T023000Z\n" "EXRULE:FREQ=MONTHLY;COUNT=2\n" "EXDATE:20120601T023000Z") rr = rrule.rrulestr(s) days = [ pendulum.datetime(2012, 4, 1, 2, 30), pendulum.datetime(2012, 5, 1, 2, 30) ] t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) assert t.next(2, after=pendulum.datetime(2012, 1, 1, 0, 0)) == days t2 = serialize_and_deserialize(t) assert t2.next(2, after=pendulum.datetime(2012, 1, 1, 0, 0)) == days s = ("DTSTART:20120201T023000Z\n" "RDATE:20120701T023000Z,20120702T023000Z\n" "EXDATE:20120601T023000Z") rr = rrule.rrulestr(s) days = [ pendulum.datetime(2012, 7, 1, 2, 30), pendulum.datetime(2012, 7, 2, 2, 30) ] t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) assert t.next(2, after=pendulum.datetime(2012, 1, 1, 0, 0)) == days t2 = serialize_and_deserialize(t) assert t2.next(2, after=pendulum.datetime(2012, 1, 1, 0, 0)) == days
def test_rrule_clock_doesnt_compute_dates_before_start_date(self): dtstart = pendulum.datetime(2018, 1, 1, microsecond=0) start_date = pendulum.datetime(2020, 1, 1, microsecond=0) c = clocks.RRuleClock(rrule.rrule(freq=rrule.HOURLY, dtstart=dtstart), start_date=start_date) assert islice(c.events(after=pendulum.datetime(2000, 1, 1)), 3) == [ start_date, start_date.add(hours=1), start_date.add(hours=2), ]
def test_rrule_clock_events_with_after_argument(self): start_date = pendulum.datetime(2018, 1, 1, microsecond=0) after = pendulum.datetime(2025, 1, 5) c = clocks.RRuleClock( rrule.rrule(freq=rrule.HOURLY, dtstart=start_date)) assert islice(c.events(after=after), 3) == [ after.add(hours=1), after.add(hours=2), after.add(hours=3), ]
def test_rrule_clock_doesnt_compute_dates_after_end_date(self): dtstart = pendulum.datetime(2018, 1, 1, microsecond=0) end_date = pendulum.datetime(2020, 1, 1, microsecond=0) c = clocks.RRuleClock(rrule.rrule(freq=rrule.DAILY, dtstart=dtstart), end_date=end_date) assert islice(c.events(after=end_date), 3) == [] after = end_date.subtract(days=2) assert islice(c.events(after=after), 3) == [ after.add(days=1), after.add(days=2), ]
def test_rrule_clock_events(self, params, labels): start = pendulum.now().replace(microsecond=0) c = clocks.RRuleClock( rrule.rrule(freq=rrule.DAILY, dtstart=start), parameter_defaults=params, labels=labels, ) output = islice(c.events(), 3) assert all(isinstance(e, clocks.ClockEvent) for e in output) assert all(e.parameter_defaults == params for e in output) assert all(e.labels == labels for e in output) assert output == [ start.add(days=1), start.add(days=2), start.add(days=3), ]
def test_serialize_rrule_clocks(): start = pendulum.datetime(2020, 1, 1, 0, 0) rr = rrule.rrule(rrule.MINUTELY, start) t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) assert t.next(1, after=start) == [pendulum.datetime(2020, 1, 1, 0, 1)] t2 = serialize_and_deserialize(t) assert t2.next(1, after=start) == [pendulum.datetime(2020, 1, 1, 0, 1)] weekdays = (rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR) rr = rrule.rrule(rrule.MONTHLY, start, byweekday=weekdays, bysetpos=-1) t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) assert t.next(1, after=start) == [pendulum.datetime(2020, 1, 31, 0, 0)] t2 = serialize_and_deserialize(t) assert t2.next(1, after=start) == [pendulum.datetime(2020, 1, 31, 0, 0)] # Every weekday (BYDAY) for the next 8 weekdays (COUNT). rr = rrule.rrule(rrule.DAILY, start, byweekday=weekdays, count=8) t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) assert t.next(1, after=start) == [pendulum.datetime(2020, 1, 2, 0, 0)] assert len(t.next(10, after=start)) == 7 t2 = serialize_and_deserialize(t) assert t2.next(1, after=start) == [pendulum.datetime(2020, 1, 2, 0, 0)] assert len(t2.next(10, after=start)) == 7 # Every third year (INTERVAL) on the first Tuesday (BYDAY) after a Monday (BYMONTHDAY) in October. month_day = (2, 3, 4, 5, 6, 7, 8) rr = rrule.rrule( rrule.YEARLY, start, interval=3, bymonth=10, byweekday=rrule.TU, bymonthday=month_day, ) t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) first = pendulum.datetime(2020, 10, 6, 0, 0) second = pendulum.datetime(2023, 10, 3, 0, 0) assert t.next(2, after=start) == [first, second] t2 = serialize_and_deserialize(t) assert t2.next(2, after=start) == [first, second] # Every three weeks on Sunday until 9/23/2021 until = pendulum.datetime(2021, 9, 23) days = [pendulum.datetime(2020, 1, 5), pendulum.datetime(2020, 1, 26)] after_days = [pendulum.datetime(2021, 9, 1), pendulum.datetime(2021, 9, 5)] rr = rrule.rrule(rrule.WEEKLY, start, byweekday=rrule.SU, interval=3, until=until) t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) assert t.next(2, after=start) == days assert t.next(3, after=after_days[0]) == [after_days[1]] t2 = serialize_and_deserialize(t) assert t2.next(2, after=start) == days assert t2.next(3, after=after_days[0]) == [after_days[1]] rr = rrule.rrule(rrule.YEARLY, start, byyearday=200) days = [pendulum.datetime(2020, 7, 18), pendulum.datetime(2021, 7, 19)] t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) assert t.next(2, after=start) == days t2 = serialize_and_deserialize(t) assert t2.next(2, after=start) == days # Three days after easter annually rr = rrule.rrule(rrule.YEARLY, start, byeaster=3) days = [pendulum.datetime(2020, 4, 15), pendulum.datetime(2021, 4, 7)] t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) assert t.next(2, after=start) == days t2 = serialize_and_deserialize(t) assert t2.next(2, after=start) == days rr = rrule.rrule(rrule.WEEKLY, start, byhour=9, byminute=13, bysecond=54) t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) assert t.next(1, after=start) == [pendulum.datetime(2020, 1, 1, 9, 13, 54)] t2 = serialize_and_deserialize(t) assert t2.next(1, after=start) == [pendulum.datetime(2020, 1, 1, 9, 13, 54)] rr = rrule.rrule(rrule.YEARLY, start, byweekno=(7, 16), byweekday=rrule.WE) days = [pendulum.datetime(2020, 2, 12), pendulum.datetime(2020, 4, 15)] t = schedules.Schedule(clocks=[clocks.RRuleClock(rrule_obj=rr)]) assert t.next(2, after=start) == days t2 = serialize_and_deserialize(t) assert t2.next(2, after=start) == days
def test_rrule_create_without_rrule(self): with pytest.raises(TypeError): clocks.RRuleClock(42)
def test_rrule_clock_creation(self): c = clocks.RRuleClock(rrule.rrule(freq=rrule.DAILY)) assert c.parameter_defaults == dict() assert c.labels is None assert c.start_date is None assert c.end_date is None