Files

213 lines
6.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from datetime import date, time, timedelta
import pytest
from tests.helpers import fixture_html
from timetable import (
KIND_REST_DAY,
KIND_SPECIAL,
KIND_WORKDAY,
ParseError,
parse_page,
)
def load(name, line, stop):
return parse_page(fixture_html(name), line, stop)
@pytest.fixture(scope="module")
def line4():
return load("line4_u_potoka.html", "4", "Štípa,u potoka")
def summary(timetable):
return [(c.kind, c.valid_from, len(c.departures)) for c in timetable.columns]
def test_line4_columns(line4):
assert summary(line4) == [
(KIND_WORKDAY, date(2026, 10, 1), 36),
(KIND_REST_DAY, date(2026, 10, 3), 29),
]
def test_line4_first_workday_departures(line4):
deps = line4.columns[0].departures
assert [d.time for d in deps[:3]] == [time(4, 16), time(4, 46), time(5, 32)]
assert all(d.line == "4" and d.stop == "Štípa,u potoka" for d in deps)
def test_line4_footnote_mark_is_kept_not_dropped(line4):
last = line4.columns[0].departures[-1]
assert (last.time, last.mark) == (time(23, 6), "v")
def test_line5_counts_and_mark():
t = load("line5_u_potoka.html", "5", "Štípa,u potoka")
assert summary(t) == [
(KIND_WORKDAY, date(2026, 10, 1), 16),
(KIND_REST_DAY, date(2026, 10, 3), 14),
]
marked = {(d.time, d.mark) for d in t.columns[0].departures if d.mark}
assert (time(7, 16), "A") in marked
def test_line35_has_special_school_holiday_column():
t = load("line35_druzstvo.html", "35", "Štípa,družstvo")
assert summary(t) == [
(KIND_WORKDAY, date(2026, 10, 1), 4),
(KIND_REST_DAY, date(2026, 10, 3), 0),
(KIND_SPECIAL, date(2026, 10, 29), 3),
]
assert t.columns[2].dates == (date(2026, 10, 29), date(2026, 10, 30), date(2026, 12, 23))
assert t.columns[0].dates == ()
def test_line36_counts():
t = load("line36_druzstvo.html", "36", "Štípa,družstvo")
assert [len(c.departures) for c in t.columns] == [4, 1, 3]
HEADER = '<table class="stop-timetable"><thead><tr><th>Hod.</th>{ths}</tr></thead><tbody>{rows}</tbody></table>'
WORKDAY_TH = "<th>Pracovní dny<br/>platnost od 1.10.2026</th>"
def test_zero_departures_is_a_parse_error():
html = HEADER.format(
ths=WORKDAY_TH, rows='<tr><td class="hour-cell">00</td><td class="minutes-cell"></td></tr>'
)
with pytest.raises(ParseError, match="no departures"):
parse_page(html, "4", "X")
@pytest.mark.parametrize("html", ["", "<html>redesigned page</html>", "<table></table>"])
def test_unrecognised_page_is_a_parse_error(html):
with pytest.raises(ParseError):
parse_page(html, "4", "X")
def test_unknown_column_header_is_a_parse_error():
html = HEADER.format(
ths="<th>Něco nového<br/>platnost od 1.10.2026</th>",
rows='<tr><td class="hour-cell">04</td><td class="minutes-cell">16 </td></tr>',
)
with pytest.raises(ParseError, match="unknown column"):
parse_page(html, "4", "X")
def test_cell_count_mismatch_is_a_parse_error():
html = HEADER.format(
ths=WORKDAY_TH,
rows='<tr><td class="hour-cell">04</td><td class="minutes-cell">16 </td><td class="minutes-cell">30 </td></tr>',
)
with pytest.raises(ParseError, match="cells"):
parse_page(html, "4", "X")
def test_invalid_hour_is_a_parse_error():
html = HEADER.format(
ths=WORKDAY_TH,
rows='<tr><td class="hour-cell">25</td><td class="minutes-cell">10 </td></tr>',
)
with pytest.raises(ParseError, match="invalid time"):
parse_page(html, "4", "X")
def test_invalid_minute_is_a_parse_error():
html = HEADER.format(
ths=WORKDAY_TH,
rows='<tr><td class="hour-cell">04</td><td class="minutes-cell">99 </td></tr>',
)
with pytest.raises(ParseError, match="invalid time"):
parse_page(html, "4", "X")
def header_column(label_html):
html = HEADER.format(
ths=f"<th>{label_html}</th>",
rows='<tr><td class="hour-cell">04</td><td class="minutes-cell">16 </td></tr>',
)
return parse_page(html, "4", "X").columns[0]
def special(validity):
return header_column(f"Prac.dny–školní prázdniny<br/>platnost {validity}")
def days(first, last):
out = []
while first <= last:
out.append(first)
first += timedelta(days=1)
return tuple(out)
def test_validity_od_single_date_is_valid_from():
column = header_column("Pracovní dny<br/>platnost od 1.10.2026")
assert (column.kind, column.valid_from, column.dates) == (KIND_WORKDAY, date(2026, 10, 1), ())
def test_validity_rest_day_valid_from():
column = header_column("Dny pracovního klidu<br/>platnost od 3.10.2026")
assert (column.kind, column.valid_from) == (KIND_REST_DAY, date(2026, 10, 3))
def test_validity_day_range_and_single_list():
column = special("29.–30.10.2026, 23.12.2026")
assert column.dates == (date(2026, 10, 29), date(2026, 10, 30), date(2026, 12, 23))
assert (column.kind, column.valid_from) == (KIND_SPECIAL, date(2026, 10, 29))
@pytest.mark.parametrize("text", ["29.10.–30.10.2026", "29.10. - 30.10.2026", "29. – 30.10.2026"])
def test_validity_month_range(text):
assert special(text).dates == (date(2026, 10, 29), date(2026, 10, 30))
def test_validity_range_within_month_end():
assert special("28.12.–31.12.2026").dates == days(date(2026, 12, 28), date(2026, 12, 31))
def test_validity_range_with_both_years():
assert special("28.12.2026–2.1.2027").dates == days(date(2026, 12, 28), date(2027, 1, 2))
def test_validity_range_infers_start_year_across_new_year():
column = special("28.12.–2.1.2027")
assert column.dates == days(date(2026, 12, 28), date(2027, 1, 2))
assert column.valid_from == date(2026, 12, 28)
def test_validity_summer_range():
assert special("1.7.–31.8.2027").dates == days(date(2027, 7, 1), date(2027, 8, 31))
@pytest.mark.parametrize(
"text",
[
"30.–29.10.2026", # reversed
"31.2.2026", # impossible date
"1.2.2026, 31.2.2026",
"29.–30.10.2026, 5", # stray digits
"29.–30.10.2026 a 12",
"29.–30.10.2026, 23.12.", # incomplete date
"od 1.10.2026, 5.10.2026",
"1.10.20266",
"129.10.–30.10.2026",
"29.10.–30.10.",
],
)
def test_validity_unknown_forms_raise(text):
with pytest.raises(ParseError):
special(text)
def test_header_without_date_is_a_parse_error():
with pytest.raises(ParseError, match="no validity"):
header_column("Pracovní dny<br/>platnost od")
def test_label_with_workday_and_school_holidays_is_special():
column = header_column("Pracovní dny – školní prázdniny<br/>platnost 29.–30.10.2026")
assert column.kind == KIND_SPECIAL