def test_interval_clocks_with_sub_minute_intervals_cant_be_deserialized(): schema = ScheduleSchema() s = schedules.Schedule(clocks=[clocks.IntervalClock(timedelta(seconds=100))]) data = schema.dump(s) data["clocks"][0]["interval"] = 59 * 1e6 with pytest.raises(ValueError, match="can not be less than one minute"): schema.load(data)
def test_interval_schedule(self): serialized = { "start_date": { "dt": "2019-01-02T03:00:00", "tz": "UTC" }, "end_date": None, "interval": 3720000000, "__version__": "0.6.0+87.g44ac9ba5", "type": "IntervalSchedule", } schema = ScheduleSchema() schedule = schema.load(serialized) assert schedule.next(10, after=pendulum.datetime(2019, 1, 1)) == [ pendulum.datetime(2019, 1, 2, 3, 0, 0), pendulum.datetime(2019, 1, 2, 4, 2, 0), pendulum.datetime(2019, 1, 2, 5, 4, 0), pendulum.datetime(2019, 1, 2, 6, 6, 0), pendulum.datetime(2019, 1, 2, 7, 8, 0), pendulum.datetime(2019, 1, 2, 8, 10, 0), pendulum.datetime(2019, 1, 2, 9, 12, 0), pendulum.datetime(2019, 1, 2, 10, 14, 0), pendulum.datetime(2019, 1, 2, 11, 16, 0), pendulum.datetime(2019, 1, 2, 12, 18, 0), ]
def test_union_schedule(self): serialized = { "end_date": { "dt": "2020-01-01T00:00:00", "tz": "UTC" }, "schedules": [ { "end_date": { "dt": "2020-01-01T00:00:00", "tz": "UTC" }, "interval": 3720000000, "start_date": { "dt": "2019-01-02T03:00:00", "tz": "UTC" }, "__version__": "0.6.0+105.gce4aef06", "type": "IntervalSchedule", }, { "end_date": { "dt": "2020-01-01T00:00:00", "tz": "UTC" }, "cron": "3 0 * * *", "start_date": { "dt": "2019-01-02T03:00:00", "tz": "UTC" }, "__version__": "0.6.0+105.gce4aef06", "type": "CronSchedule", }, ], "start_date": { "dt": "2019-01-02T03:00:00", "tz": "UTC" }, "__version__": "0.6.0+105.gce4aef06", "type": "UnionSchedule", } schema = ScheduleSchema() with pytest.warns(UserWarning): schedule = schema.load(serialized) assert schedule.next(10, after=pendulum.datetime(2019, 1, 1)) == [ pendulum.datetime(2019, 1, 2, 3, 0, 0), pendulum.datetime(2019, 1, 2, 4, 2, 0), pendulum.datetime(2019, 1, 2, 5, 4, 0), pendulum.datetime(2019, 1, 2, 6, 6, 0), pendulum.datetime(2019, 1, 2, 7, 8, 0), pendulum.datetime(2019, 1, 2, 8, 10, 0), pendulum.datetime(2019, 1, 2, 9, 12, 0), pendulum.datetime(2019, 1, 2, 10, 14, 0), pendulum.datetime(2019, 1, 2, 11, 16, 0), pendulum.datetime(2019, 1, 2, 12, 18, 0), ]
def test_one_time_schedule_with_after(self): serialized = { "start_date": {"dt": "2019-01-02T03:00:00", "tz": "UTC"}, "__version__": "0.6.0+105.gce4aef06", "type": "OneTimeSchedule", } schema = ScheduleSchema() with pytest.warns(UserWarning): schedule = schema.load(serialized) assert schedule.next(10, after=pendulum.datetime(2020, 1, 1)) == []
def test_cron_schedule_with_end_date(self): serialized = { "end_date": {"dt": "2020-01-01T00:00:00", "tz": "UTC"}, "cron": "3 0 * * *", "start_date": {"dt": "2019-01-02T03:00:00", "tz": "UTC"}, "__version__": "0.6.0+105.gce4aef06", "type": "CronSchedule", } schema = ScheduleSchema() schedule = schema.load(serialized) assert schedule.next(10, after=pendulum.datetime(2019, 12, 31, 20)) == []
def test_one_time_schedule(self): serialized = { "start_date": { "dt": "2019-01-02T03:00:00", "tz": "UTC" }, "__version__": "0.6.0+105.gce4aef06", "type": "OneTimeSchedule", } schema = ScheduleSchema() schedule = schema.load(serialized) assert schedule.next(10, after=pendulum.datetime( 2019, 1, 1)) == [pendulum.datetime(2019, 1, 2, 3, 0, 0)]
def test_interval_schedule_with_end_date(self): serialized = { "interval": 3720000000, "start_date": {"dt": "2019-01-02T03:00:00", "tz": "UTC"}, "end_date": {"dt": "2020-01-01T00:00:00", "tz": "UTC"}, "__version__": "0.6.0+87.g44ac9ba5", "type": "IntervalSchedule", } schema = ScheduleSchema() schedule = schema.load(serialized) assert schedule.next(10, after=pendulum.datetime(2019, 12, 31, 20)) == [ pendulum.datetime(2019, 12, 31, 20, 36, 0), pendulum.datetime(2019, 12, 31, 21, 38, 0), pendulum.datetime(2019, 12, 31, 22, 40, 0), pendulum.datetime(2019, 12, 31, 23, 42, 0), ]
async def test_set_flow_group_schedule(self, flow_group_id, clock): flow_group = await models.FlowGroup.where(id=flow_group_id ).first({"schedule"}) assert flow_group.schedule is None success = await api.flow_groups.set_flow_group_schedule( flow_group_id=flow_group_id, clocks=[clock]) assert success is True flow_group = await models.FlowGroup.where(id=flow_group_id ).first({"schedule"}) assert flow_group.schedule["clocks"][0] == clock schema = ScheduleSchema() schedule = schema.load(flow_group.schedule) assert schedule.clocks[0].start_date is None
async def test_set_flow_group_schedule_respects_passed_timezones( self, flow_group_id, clock): flow_group = await models.FlowGroup.where(id=flow_group_id ).first({"schedule"}) assert flow_group.schedule is None success = await api.flow_groups.set_flow_group_schedule( flow_group_id=flow_group_id, clocks=[clock], timezone="US/Pacific") assert success is True schema = ScheduleSchema() flow_group = await models.FlowGroup.where(id=flow_group_id ).first({"schedule"}) schedule = schema.load(flow_group.schedule) assert schedule.clocks[0].start_date <= pendulum.now("utc") assert schedule.clocks[0].start_date.timezone_name == "US/Pacific"
def test_cron_schedule(self): serialized = { "cron": "3 0 * * *", "start_date": None, "end_date": None, "__version__": "0.6.0+87.g44ac9ba5", "type": "CronSchedule", } schema = ScheduleSchema() schedule = schema.load(serialized) assert schedule.next(10, after=pendulum.datetime(2019, 1, 1)) == [ pendulum.datetime(2019, 1, 1, 0, 3, 0), pendulum.datetime(2019, 1, 2, 0, 3, 0), pendulum.datetime(2019, 1, 3, 0, 3, 0), pendulum.datetime(2019, 1, 4, 0, 3, 0), pendulum.datetime(2019, 1, 5, 0, 3, 0), pendulum.datetime(2019, 1, 6, 0, 3, 0), pendulum.datetime(2019, 1, 7, 0, 3, 0), pendulum.datetime(2019, 1, 8, 0, 3, 0), pendulum.datetime(2019, 1, 9, 0, 3, 0), pendulum.datetime(2019, 1, 10, 0, 3, 0), ]
def serialize_and_deserialize(schedule: schedules.Schedule): schema = ScheduleSchema() return schema.load(json.loads(json.dumps(schema.dump(schedule))))