Files
2026-10-07 00:55:17 +02:00

174 lines
6.6 KiB
Python

from datetime import date, datetime, time
from schedule import is_rest_day, next_departures, select_column
from tests.helpers import load_timetables
from timetable import KIND_REST_DAY, KIND_SPECIAL, KIND_WORKDAY, Column, Departure, Timetable
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_is_ignored_on_a_rest_day():
# Decision: the special column is "Prac.dny-skolni prazdniny", i.e. workdays during
# school holidays only, so it never applies on a Saturday, Sunday or state holiday
# (rule set by the controller on 2026-10-07 after the final review).
assert select_column([WORK_NEW, REST, SPECIAL], date(2026, 10, 29), rest_day=True) is REST
def test_special_dates_on_a_saturday_use_the_rest_column():
special = col(KIND_SPECIAL, date(2026, 10, 29), [date(2026, 10, 29), date(2026, 10, 31)])
assert select_column([WORK_NEW, REST, special], date(2026, 10, 31), rest_day=True) is REST
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
def dep(line, stop, h, m):
return Departure(line, stop, time(h, m))
def column(kind, valid_from, deps, dates=()):
return Column(kind, kind, valid_from, tuple(dates), tuple(deps))
V = date(2026, 10, 1)
STOP_A, STOP_B = "U potoka", "Družstvo"
A = Timetable(
"4",
STOP_A,
(
column(KIND_WORKDAY, V, [dep("4", STOP_A, 10, 0), dep("4", STOP_A, 10, 30), dep("4", STOP_A, 23, 6)]),
column(KIND_REST_DAY, V, [dep("4", STOP_A, 9, 15)]),
column(KIND_SPECIAL, V, [dep("4", STOP_A, 6, 45)], dates=[date(2026, 10, 29)]),
),
)
B = Timetable("35", STOP_B, (column(KIND_WORKDAY, V, [dep("35", STOP_B, 10, 10)]),))
TUE_0950 = datetime(2026, 10, 6, 9, 50, 0)
def lines_and_times(upcoming):
return [(u.departure.line, u.when.strftime("%H:%M")) for u in upcoming]
def test_merges_timetables_earliest_first(cz):
up = next_departures([A, B], TUE_0950, 3, cz)
assert lines_and_times(up) == [("4", "10:00"), ("35", "10:10"), ("4", "10:30")]
def test_minutes_until_departure(cz):
first = next_departures([A, B], TUE_0950, 1, cz)[0]
assert first.minutes == 10 and first.next_day is False
def test_departure_in_the_current_minute_is_now(cz):
first = next_departures([A], datetime(2026, 10, 6, 10, 0, 40), 1, cz)[0]
assert first.when == datetime(2026, 10, 6, 10, 0) and first.minutes == 0
def test_departed_bus_is_dropped(cz):
first = next_departures([A], datetime(2026, 10, 6, 10, 1, 0), 1, cz)[0]
assert first.when == datetime(2026, 10, 6, 10, 30)
def test_rolls_into_next_day_after_last_departure(cz):
up = next_departures([A], datetime(2026, 10, 6, 23, 30), 2, cz) # last bus today was 23:06
assert [(u.when, u.next_day) for u in up] == [
(datetime(2026, 10, 7, 10, 0), True),
(datetime(2026, 10, 7, 10, 30), True),
]
assert up[0].minutes == 630
def test_state_holiday_on_weekday_uses_rest_day_column(cz):
first = next_departures([A], datetime(2026, 10, 28, 8, 0), 1, cz)[0] # Wednesday, Statehood Day
assert first.when == datetime(2026, 10, 28, 9, 15)
def test_saturday_uses_rest_day_column(cz):
first = next_departures([A], datetime(2026, 10, 10, 8, 0), 1, cz)[0]
assert first.when == datetime(2026, 10, 10, 9, 15)
def test_special_date_uses_special_column(cz):
first = next_departures([A], datetime(2026, 10, 29, 6, 0), 1, cz)[0]
assert first.when == datetime(2026, 10, 29, 6, 45)
def test_nothing_usable_returns_empty_list(cz):
empty = Timetable("9", "X", ())
assert next_departures([empty], TUE_0950, 3, cz) == []
assert next_departures([], TUE_0950, 3, cz) == []
def test_real_timetables_in_the_afternoon(cz):
up = next_departures(load_timetables(), datetime(2026, 10, 6, 13, 28, 20), 5, cz)
assert lines_and_times(up) == [
("4", "13:33"),
("4", "13:53"),
("4", "14:08"),
("5", "14:25"),
("36", "14:27"),
]
assert up[0].minutes == 5
def test_real_special_date_includes_line35(cz):
first = next_departures(load_timetables(), datetime(2026, 10, 29, 5, 0), 1, cz)[0]
assert (first.departure.line, first.departure.mark) == ("35", "v")
assert first.when == datetime(2026, 10, 29, 5, 16)
def test_real_timetables_late_evening_show_tomorrow(cz):
up = next_departures(load_timetables(), datetime(2026, 10, 6, 23, 52, 30), 3, cz)
assert all(u.next_day for u in up)
assert up[0].when == datetime(2026, 10, 7, 4, 16)