def test_nonexistent_days(timezone: DstTzInfo):
    """Test that invalid dates are skipped."""
    trigger = CalendarIntervalTrigger(months=1)
    now = timezone.localize(datetime(2016, 4, 30))
    previous = timezone.localize(datetime(2016, 3, 31))
    expected = timezone.localize(datetime(2016, 5, 31))
    assert trigger.get_next_fire_time(previous, now) == expected
def test_dst_backward(args: Dict[str, Any], now: datetime, now_dst: bool,
                      expected: datetime, expected_dst: bool,
                      timezone: DstTzInfo):
    schedule = CronSchedule(task_id='task', timezone=timezone, **args)
    now = timezone.localize(now, is_dst=now_dst)
    expected = timezone.localize(expected, is_dst=expected_dst)
    assert schedule.get_next_run_time(now) == expected
Example #3
0
def clip_activities_by_range(
    start_date: datetime.date, end_date: datetime.date, activities: List[Activity], local_timezone: DstTzInfo,
) -> List[Activity]:
    """ Clip a list of Activity with the given range, remove activities
    which have zero durations

    Parameters
    ----------
    start_date : datetime.date
    end_date : datetime.date
    activities : list of Activity

    Returns
    -------
    clipped: list of Activity
    """
    delta = datetime.timedelta()
    start_dt = local_timezone.localize(datetime.datetime(start_date.year, start_date.month, start_date.day))
    end_dt = local_timezone.localize(datetime.datetime(end_date.year, end_date.month, end_date.day, 23, 59, 59, 99999))
    new_activities = []
    for activity in activities:
        clipped = activity.clip(start_dt, end_dt)
        if clipped.duration > delta:
            new_activities.append(clipped)
    return new_activities
def test_nonexistent_days(timezone: DstTzInfo):
    """Test that invalid dates are skipped."""
    schedule = CalendarIntervalSchedule(task_id='task', timezone=timezone, months=1,
                                        start_date=date(2016, 3, 31))

    now = timezone.localize(datetime(2016, 4, 30))
    expected = timezone.localize(datetime(2016, 5, 31))
    assert schedule.get_next_run_time(now) == expected
def test_range_step(timezone: DstTzInfo):
    """Test that a range expression with a step value produces the correct values."""
    schedule = CronSchedule(task_id='task', timezone=timezone, day='5-24/3')
    previous_run_time = timezone.localize(datetime(2016, 2, 23))
    now = timezone.localize(datetime(2016, 3, 31))
    expected = [timezone.localize(datetime(2016, 3, day)) for day in (5, 8, 11, 14, 17, 20, 23)]
    run_times = list(schedule.get_run_times(now, previous_run_time))
    assert run_times == expected
def test_get_next_run_times(schedule: CalendarIntervalSchedule, timezone: DstTzInfo):
    now = timezone.localize(datetime(2021, 7, 31, 5, 17))
    previous_run_time = timezone.localize(datetime(2016, 3, 5, 3, 0, 8))
    run_times = list(schedule.get_run_times(now, previous_run_time))
    assert run_times == [
        timezone.localize(datetime(2017, 9, 24, 3, 0, 8)),
        timezone.localize(datetime(2019, 4, 15, 3, 0, 8)),
        timezone.localize(datetime(2020, 11, 4, 3, 0, 8))
    ]
def test_repeated_time(timezone: DstTzInfo):
    """
    Test that if the designated time is repeated during a day due to a backward DST shift, the task
    is executed on the earlier occurrence of that time.

    """
    trigger = CalendarIntervalTrigger(days=2, hour=2, minute=30)
    now = timezone.localize(datetime(2016, 10, 30))
    expected = timezone.localize(datetime(2016, 10, 30, 2, 30), is_dst=True)
    assert trigger.get_next_fire_time(None, now) == expected
def test_weekday_increment_rollover(timezone: DstTzInfo):
    """
    Test that if a field's value exceeds the maximum value during a calculation, that field along
    with all the less significant ones are reset to their minimum values.

    """
    schedule = CronSchedule(task_id='task', timezone=timezone, day='8', day_of_week='fri')
    now = timezone.localize(datetime(2016, 3, 1))
    expected = timezone.localize(datetime(2016, 4, 8))
    assert schedule.get_next_run_time(now) == expected
def test_missing_time(timezone: DstTzInfo):
    """
    Test that if the designated time does not exist on a day due to a forward DST shift, the day is
    skipped entirely.

    """
    trigger = CalendarIntervalTrigger(days=1, hour=2, minute=30)
    now = timezone.localize(datetime(2016, 3, 27))
    expected = timezone.localize(datetime(2016, 3, 28, 2, 30))
    assert trigger.get_next_fire_time(None, now) == expected
def test_get_next_run_times(schedule: IntervalSchedule, timezone: DstTzInfo):
    now = timezone.localize(datetime(2016, 7, 31, 5, 17))
    previous_run_time = timezone.localize(datetime(2016, 7, 31, 5, 9, 55))
    expected = [
        timezone.localize(datetime(2016, 7, 31, 5, 12, 0)),
        timezone.localize(datetime(2016, 7, 31, 5, 14, 5)),
        timezone.localize(datetime(2016, 7, 31, 5, 16, 10))
    ]
    run_times = list(schedule.get_run_times(now, previous_run_time))
    assert run_times == expected
def test_missing_time(timezone: DstTzInfo):
    """
    Test that if the designated time does not exist on a day due to a forward DST shift, the day is
    skipped entirely.

    """
    schedule = CalendarIntervalSchedule(task_id='task', timezone=timezone, days=1, hour=2,
                                        minute=30, start_date=date(2016, 3, 27))
    now = timezone.localize(datetime(2016, 3, 27))
    expected = timezone.localize(datetime(2016, 3, 28, 2, 30))
    assert schedule.get_next_run_time(now) == expected
def test_range_step(timezone: DstTzInfo):
    """Test that a range expression with a step value produces the correct values."""
    schedule = CronSchedule(task_id='task', timezone=timezone, day='5-24/3')
    previous_run_time = timezone.localize(datetime(2016, 2, 23))
    now = timezone.localize(datetime(2016, 3, 31))
    expected = [
        timezone.localize(datetime(2016, 3, day))
        for day in (5, 8, 11, 14, 17, 20, 23)
    ]
    run_times = list(schedule.get_run_times(now, previous_run_time))
    assert run_times == expected
def test_weekday_increment_rollover(timezone: DstTzInfo):
    """
    Test that if a field's value exceeds the maximum value during a calculation, that field along
    with all the less significant ones are reset to their minimum values.

    """
    schedule = CronSchedule(task_id='task',
                            timezone=timezone,
                            day='8',
                            day_of_week='fri')
    now = timezone.localize(datetime(2016, 3, 1))
    expected = timezone.localize(datetime(2016, 4, 8))
    assert schedule.get_next_run_time(now) == expected
def release_schedule(
    release_file: str, timezone: DstTzInfo
) -> Dict[dt.datetime, version.Version]:
    with open(release_file) as f:
        release_schedule: Dict[str, str] = json.loads(f.read())

    parsed_schedule: Dict[dt.datetime, version.Version] = {}
    for date, release_version in release_schedule.items():
        parsed_date = dateutil.parser.parse(date)
        parsed_date = timezone.localize(parsed_date)
        parsed_version = cast(version.Version, version.parse(release_version))
        parsed_schedule[parsed_date] = parsed_version

    return parsed_schedule
def test_repeated_time(timezone: DstTzInfo):
    """
    Test that if the designated time is repeated during a day due to a backward DST shift, the task
    is executed twice that day.

    """
    schedule = CalendarIntervalSchedule(task_id='task', timezone=timezone, days=2, hour=2,
                                        minute=30, start_date=date(2016, 10, 30))

    # The first returned datetime should be the on still in DST
    now = timezone.localize(datetime(2016, 10, 30))
    expected = timezone.localize(datetime(2016, 10, 30, 2, 30), is_dst=True)
    assert schedule.get_next_run_time(now) == expected

    # The next one should then be the one w/o DST
    now = timezone.localize(datetime(2016, 10, 30, 2, 40), is_dst=True)
    expected = timezone.localize(datetime(2016, 10, 30, 2, 30), is_dst=False)
    assert schedule.get_next_run_time(now) == expected

    # But if both times have passed, move on to the next interval
    now = timezone.localize(datetime(2016, 10, 30, 2, 40), is_dst=False)
    expected = timezone.localize(datetime(2016, 11, 1, 2, 30))
    assert schedule.get_next_run_time(now) == expected
def test_start_time(schedule: CronSchedule, timezone: DstTzInfo):
    """Test that start_time is respected."""
    now = timezone.localize(datetime(2016, 1, 15))
    expected = timezone.localize(datetime(2016, 7, 20, 18, 5))
    assert schedule.get_next_run_time(now) == expected
def test_last_day_of_month(month: int, expected_day: int, timezone: DstTzInfo):
    schedule = CronSchedule(task_id='task', timezone=timezone, day='last')
    now = timezone.localize(datetime(2016, month, 1))
    expected = timezone.localize(datetime(2016, month, expected_day))
    assert schedule.get_next_run_time(now) == expected
def test_dst_forward(schedule: IntervalSchedule, timezone: DstTzInfo):
    previous_time = timezone.localize(datetime(2016, 3, 27, 1, 58))
    now = timezone.localize(datetime(2016, 3, 27, 1, 59))
    expected = timezone.localize(datetime(2016, 3, 27, 3, 0, 5))
    assert schedule.get_next_run_time(now, previous_time) == expected
def test_start_date(trigger: CalendarIntervalTrigger, timezone: DstTzInfo):
    """Test that start_date is respected."""
    now = timezone.localize(datetime(2016, 1, 15))
    expected = timezone.localize(datetime(2016, 3, 5, 3, 0, 8))
    assert trigger.get_next_fire_time(None, now) == expected
def test_dst_backward(timezone: DstTzInfo):
    schedule = CronSchedule(task_id='task', timezone=timezone, minute='*/5')
    previous_time = timezone.localize(datetime(2016, 10, 30, 2, 55), is_dst=True)
    now = timezone.localize(datetime(2016, 10, 30, 2, 59), is_dst=True)
    expected = timezone.localize(datetime(2016, 10, 30, 2), is_dst=False)
    assert schedule.get_next_run_time(now, previous_time) == expected
def test_dst_forward(args: Dict[str, Any], now: datetime, expected: datetime,
                     timezone: DstTzInfo):
    schedule = CronSchedule(task_id='task', timezone=timezone, **args)
    now = timezone.localize(now)
    expected = timezone.localize(expected)
    assert schedule.get_next_run_time(now) == expected
def test_end_time(schedule: CronSchedule, timezone: DstTzInfo):
    """Test that end_time is respected."""
    now = timezone.localize(datetime(2020, 12, 31))
    assert schedule.get_next_run_time(now) is None
def test_start_time(schedule: CronSchedule, timezone: DstTzInfo):
    """Test that start_time is respected."""
    now = timezone.localize(datetime(2016, 1, 15))
    expected = timezone.localize(datetime(2016, 7, 20, 18, 5))
    assert schedule.get_next_run_time(now) == expected
def test_start_time(schedule: IntervalSchedule, timezone: DstTzInfo,
                    start_time: datetime):
    """Test that start_time is respected."""
    now = timezone.localize(datetime(2016, 1, 15))
    assert schedule.get_next_run_time(now) == start_time
def test_start_date(schedule: CalendarIntervalSchedule, timezone: DstTzInfo):
    """Test that start_date is respected."""
    now = timezone.localize(datetime(2016, 1, 15))
    expected = timezone.localize(datetime(2016, 3, 5, 3, 0, 8))
    assert schedule.get_next_run_time(now) == expected
def test_weekday_position(month: int, expression: str, expected_day: int,
                          timezone: DstTzInfo):
    schedule = CronSchedule(task_id='task', timezone=timezone, day=expression)
    now = timezone.localize(datetime(2016, month, 1))
    expected = timezone.localize(datetime(2016, month, expected_day))
    assert schedule.get_next_run_time(now) == expected
def test_end_time(schedule: CronSchedule, timezone: DstTzInfo):
    """Test that end_time is respected."""
    now = timezone.localize(datetime(2020, 12, 31))
    assert schedule.get_next_run_time(now) is None
def test_dst_backward(schedule: IntervalSchedule, timezone: DstTzInfo):
    previous_time = timezone.localize(datetime(2016, 10, 30, 2, 58),
                                      is_dst=True)
    now = timezone.localize(datetime(2016, 10, 30, 2, 59), is_dst=True)
    expected = timezone.localize(datetime(2016, 10, 30, 2, 0, 5), is_dst=False)
    assert schedule.get_next_run_time(now, previous_time) == expected
def test_dst_forward(timezone: DstTzInfo):
    schedule = CronSchedule(task_id='task', timezone=timezone, minute='*/5')
    previous_time = timezone.localize(datetime(2016, 3, 27, 1, 55))
    now = timezone.localize(datetime(2016, 3, 27, 1, 59))
    expected = timezone.localize(datetime(2016, 3, 27, 3))
    assert schedule.get_next_run_time(now, previous_time) == expected
def test_month_rollover(timezone: DstTzInfo):
    """Test that if the maximum value in a field is reached, the previous field is incremented."""
    schedule = CronSchedule(task_id='task', timezone=timezone, day=30)
    now = timezone.localize(datetime(2016, 2, 1))
    expected = timezone.localize(datetime(2016, 3, 30))
    assert schedule.get_next_run_time(now) == expected
def test_month_rollover(timezone: DstTzInfo):
    """Test that if the maximum value in a field is reached, the previous field is incremented."""
    schedule = CronSchedule(task_id='task', timezone=timezone, day=30)
    now = timezone.localize(datetime(2016, 2, 1))
    expected = timezone.localize(datetime(2016, 3, 30))
    assert schedule.get_next_run_time(now) == expected
def test_end_date(trigger: CalendarIntervalTrigger, timezone: DstTzInfo):
    """Test that end_date is respected."""
    now = timezone.localize(datetime(2020, 12, 31))
    previous = timezone.localize(datetime(2020, 12, 25))
    assert trigger.get_next_fire_time(now, previous) is None
def test_weekday_position(month: int, expression: str, expected_day: int, timezone: DstTzInfo):
    schedule = CronSchedule(task_id='task', timezone=timezone, day=expression)
    now = timezone.localize(datetime(2016, month, 1))
    expected = timezone.localize(datetime(2016, month, expected_day))
    assert schedule.get_next_run_time(now) == expected
def test_last_day_of_month(month: int, expected_day: int, timezone: DstTzInfo):
    schedule = CronSchedule(task_id='task', timezone=timezone, day='last')
    now = timezone.localize(datetime(2016, month, 1))
    expected = timezone.localize(datetime(2016, month, expected_day))
    assert schedule.get_next_run_time(now) == expected