diff --git a/schedule.py b/schedule.py new file mode 100644 index 0000000..ad799c3 --- /dev/null +++ b/schedule.py @@ -0,0 +1,32 @@ +"""Pick the right timetable column for a day and list upcoming departures.""" +from datetime import date +from typing import Container, Sequence + +from timetable import KIND_REST_DAY, KIND_SPECIAL, KIND_WORKDAY, Column + + +def is_rest_day(day: date, state_holidays: Container[date]) -> bool: + """Saturday, Sunday or a Czech state holiday.""" + return day.weekday() >= 5 or day in state_holidays + + +def select_column(columns: Sequence[Column], day: date, rest_day: bool) -> Column | None: + """The column that applies on `day`, or None if the timetable has no usable column. + + Rules (see spec "Choosing the column"): + 1. a KIND_SPECIAL column whose `dates` contain `day` wins; + 2. otherwise rest days use KIND_REST_DAY columns, other days KIND_WORKDAY; + 3. among columns of that kind the one with the latest valid_from <= day; + if none has started yet, the one with the earliest valid_from; + 4. no column of the needed kind -> None. + """ + for column in columns: + if column.kind == KIND_SPECIAL and day in column.dates: + return column + + wanted = KIND_REST_DAY if rest_day else KIND_WORKDAY + candidates = sorted((c for c in columns if c.kind == wanted), key=lambda c: c.valid_from) + if not candidates: + return None + started = [c for c in candidates if c.valid_from <= day] + return started[-1] if started else candidates[0] diff --git a/tests/test_schedule.py b/tests/test_schedule.py new file mode 100644 index 0000000..8a8c57b --- /dev/null +++ b/tests/test_schedule.py @@ -0,0 +1,64 @@ +from datetime import date + +from schedule import is_rest_day, select_column +from timetable import KIND_REST_DAY, KIND_SPECIAL, KIND_WORKDAY, Column + + +def col(kind, valid_from, dates=()): + return Column(label=f"{kind} {valid_from}", kind=kind, valid_from=valid_from, dates=tuple(dates), departures=()) + + +WORK_OLD = col(KIND_WORKDAY, date(2026, 3, 1)) +WORK_NEW = col(KIND_WORKDAY, date(2026, 10, 1)) +REST = col(KIND_REST_DAY, date(2026, 10, 3)) +SPECIAL = col( + KIND_SPECIAL, date(2026, 10, 29), [date(2026, 10, 29), date(2026, 10, 30), date(2026, 12, 23)] +) + + +def test_is_rest_day(cz): + assert not is_rest_day(date(2026, 10, 6), cz) # Tuesday + assert is_rest_day(date(2026, 10, 10), cz) # Saturday + assert is_rest_day(date(2026, 10, 11), cz) # Sunday + assert is_rest_day(date(2026, 10, 28), cz) # Wednesday, Statehood Day + assert is_rest_day(date(2026, 12, 24), cz) # Thursday, Christmas Eve + + +def test_workday_uses_workday_column(): + assert select_column([WORK_NEW, REST], date(2026, 10, 6), rest_day=False) is WORK_NEW + + +def test_rest_day_uses_rest_column(): + assert select_column([WORK_NEW, REST], date(2026, 10, 10), rest_day=True) is REST + + +def test_latest_started_column_of_a_kind_wins(): + assert select_column([WORK_OLD, WORK_NEW], date(2026, 10, 6), rest_day=False) is WORK_NEW + assert select_column([WORK_OLD, WORK_NEW], date(2026, 9, 15), rest_day=False) is WORK_OLD + + +def test_column_not_valid_yet_is_used_when_nothing_else_exists(): + assert select_column([WORK_NEW], date(2026, 9, 1), rest_day=False) is WORK_NEW + assert select_column([WORK_NEW, REST], date(2026, 9, 27), rest_day=True) is REST + + +def test_special_date_replaces_workday_column(): + assert select_column([WORK_NEW, REST, SPECIAL], date(2026, 10, 29), rest_day=False) is SPECIAL + assert select_column([WORK_NEW, REST, SPECIAL], date(2026, 12, 23), rest_day=False) is SPECIAL + + +def test_special_column_ignored_on_other_days(): + # 28.10. is a holiday right before the special dates: must use the rest column. + assert select_column([WORK_NEW, REST, SPECIAL], date(2026, 10, 28), rest_day=True) is REST + assert select_column([WORK_NEW, REST, SPECIAL], date(2026, 10, 31), rest_day=True) is REST + assert select_column([WORK_NEW, REST, SPECIAL], date(2026, 11, 2), rest_day=False) is WORK_NEW + + +def test_special_date_beats_rest_day(): + # Default precedence (your call to change): a special date wins even on a rest day. + assert select_column([WORK_NEW, REST, SPECIAL], date(2026, 10, 29), rest_day=True) is SPECIAL + + +def test_no_column_of_the_needed_kind_returns_none(): + assert select_column([WORK_NEW], date(2026, 10, 10), rest_day=True) is None + assert select_column([], date(2026, 10, 6), rest_day=False) is None