import json from datetime import datetime, timedelta import pytest import store from tests.helpers import ROOT, fixture_html LINE4 = store.LineConfig("4", "Štípa,u potoka", "https://example.test/4") NOW = datetime(2026, 10, 6, 12, 0) EMPTY_TABLE = ( '' '
Hod.Pracovní dny
platnost od 1.10.2026
00
' ) def good_page(url): return fixture_html("line4_u_potoka.html") def test_refresh_stores_page_and_load_parses_it(tmp_path): report = store.refresh([LINE4], tmp_path, fetch=good_page, now=NOW) assert report.updated == [LINE4.label] and report.failed == {} [timetable] = store.load_timetables([LINE4], tmp_path) assert timetable.line == "4" and len(timetable.columns[0].departures) == 36 def test_failed_download_keeps_previous_cache(tmp_path): store.refresh([LINE4], tmp_path, fetch=good_page, now=NOW) def network_down(url): raise OSError("network down") report = store.refresh([LINE4], tmp_path, fetch=network_down, now=NOW + timedelta(days=1)) assert "network down" in report.failed[LINE4.label] assert len(store.load_timetables([LINE4], tmp_path)[0].columns[0].departures) == 36 # the failed attempt must not make the old cache look fresh assert store.is_stale([LINE4], tmp_path, NOW + timedelta(days=3, hours=1)) is True @pytest.mark.parametrize("html", ["redesigned page", EMPTY_TABLE, ""]) def test_unparseable_page_does_not_replace_cache(tmp_path, html): store.refresh([LINE4], tmp_path, fetch=good_page, now=NOW) report = store.refresh([LINE4], tmp_path, fetch=lambda url: html, now=NOW + timedelta(days=1)) assert LINE4.label in report.failed and report.updated == [] assert len(store.load_timetables([LINE4], tmp_path)[0].columns[0].departures) == 36 def test_refresh_continues_after_one_line_fails(tmp_path): other = store.LineConfig("5", "Štípa,u potoka", "https://example.test/5") def fetch(url): if url.endswith("/4"): raise OSError("boom") return fixture_html("line5_u_potoka.html") report = store.refresh([LINE4, other], tmp_path, fetch=fetch, now=NOW) assert report.updated == [other.label] and list(report.failed) == [LINE4.label] def test_load_timetables_skips_missing_and_corrupt_cache(tmp_path): assert store.load_timetables([LINE4], tmp_path) == [] # nothing cached yet store.cache_path(tmp_path, LINE4).write_text("junk", encoding="utf-8") assert store.load_timetables([LINE4], tmp_path) == [] def test_is_stale(tmp_path): assert store.is_stale([LINE4], tmp_path, NOW) is True # never fetched store.refresh([LINE4], tmp_path, fetch=good_page, now=NOW) assert store.is_stale([LINE4], tmp_path, NOW + timedelta(days=2, hours=23)) is False assert store.is_stale([LINE4], tmp_path, NOW + timedelta(days=3, hours=1)) is True def test_load_lines_reads_yaml(tmp_path): path = tmp_path / "lines.yaml" path.write_text('lines:\n - line: "4"\n stop: "Štípa,u potoka"\n url: "https://x.test/4"\n', encoding="utf-8") assert store.load_lines(path) == [store.LineConfig("4", "Štípa,u potoka", "https://x.test/4")] def test_repo_lines_yaml_lists_the_four_lines(): labels = [c.label for c in store.load_lines(ROOT / "lines.yaml")] assert labels == ["4 Štípa,u potoka", "5 Štípa,u potoka", "35 Štípa,družstvo", "36 Štípa,družstvo"] @pytest.mark.parametrize("content", ["[]", "null", "42", '"x"']) def test_fetched_json_that_is_not_an_object_is_ignored(tmp_path, content): (tmp_path / store.FETCHED_FILE).write_text(content, encoding="utf-8") assert store.is_stale([LINE4], tmp_path, NOW) is True report = store.refresh([LINE4], tmp_path, fetch=good_page, now=NOW) assert report.updated == [LINE4.label] assert store.is_stale([LINE4], tmp_path, NOW) is False def test_fetched_json_with_non_string_value_is_stale_not_an_error(tmp_path): (tmp_path / store.FETCHED_FILE).write_text(json.dumps({LINE4.label: 5}), encoding="utf-8") assert store.is_stale([LINE4], tmp_path, NOW) is True