diff --git a/backend/src/alphapulse/calendar/europe_archives.py b/backend/src/alphapulse/calendar/europe_archives.py index 84c371f..dff4564 100644 --- a/backend/src/alphapulse/calendar/europe_archives.py +++ b/backend/src/alphapulse/calendar/europe_archives.py @@ -29,7 +29,11 @@ def ons_links(body: str) -> tuple[list[str], str | None]: def destatis_links(body: str, start: date, end: date) -> tuple[list[str], str | None]: - s = Selector(text=body) + sections = Selector(text=body).css(".s-press-search-results") + if not sections: + raise ValueError("Destatis 历史索引缺少筛选结果列表") + # 第二块是独立的最新新闻,日期不能参与筛选列表的翻页边界。 + s = sections[0] links, days = [], [] for node in s.css(".c-result"): date_text = compact(node.css(".c-result__date").xpath("string(.)").get() or "") diff --git a/backend/tests/fixtures/europe/de_archive_before_start.html b/backend/tests/fixtures/europe/de_archive_before_start.html new file mode 100644 index 0000000..ae34121 --- /dev/null +++ b/backend/tests/fixtures/europe/de_archive_before_start.html @@ -0,0 +1,28 @@ + + +
+

+ + + Press release No. 340 of 10 September 2024 + Inflation rate at +1.9% in August 2024 +

+

+ + + Press release No. 329 of 29 August 2024 + Inflation rate of +1.9% expected in August 2024 +

+ + next page + +
+
+

+ + + Press release No. 364 of 13 October 2026 + Inflation rate at +3.3% in September 2026 +

+
+ diff --git a/backend/tests/test_europe_archives.py b/backend/tests/test_europe_archives.py new file mode 100644 index 0000000..6528fa1 --- /dev/null +++ b/backend/tests/test_europe_archives.py @@ -0,0 +1,22 @@ +from datetime import date +from pathlib import Path + +import pytest + +from alphapulse.calendar.europe_archives import destatis_links + + +@pytest.mark.parametrize("start,expected,has_next", [ + (date(2025, 1, 1), [], False), + (date(2024, 1, 1), ["EN/Press/2024/09/PE24_340_611.html", "EN/Press/2024/08/PE24_329_611.html"], True), +]) +def test_destatis_archive_only_uses_filtered_results_for_links_and_date_boundary(start, expected, has_next): + body = (Path(__file__).parent / "fixtures/europe/de_archive_before_start.html").read_text() + links, following = destatis_links(body, start, date(2026, 10, 26)) + assert links == ["https://www.destatis.de/" + path for path in expected] + assert (following is not None) is has_next + + +def test_destatis_archive_rejects_a_page_without_the_filtered_result_block(): + with pytest.raises(ValueError, match="筛选结果"): + destatis_links("Access denied", date(2025, 1, 1), date(2026, 10, 26))