Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
| """Unit test for `02_collect_news.py --skip-existing` (issue #59). | |
| Verifies that the filter helper correctly drops events whose article files | |
| already exist on disk and keeps the rest. We test the helper rather than | |
| invoking the script end-to-end (which loads an LLM client and hits GDACS). | |
| """ | |
| from __future__ import annotations | |
| import importlib.util | |
| from pathlib import Path | |
| from src.models.schemas import FloodEvent | |
| def _load_collect_module(): | |
| path = Path(__file__).resolve().parent.parent / "scripts" / "02_collect_news.py" | |
| spec = importlib.util.spec_from_file_location("collect_news_02", path) | |
| assert spec and spec.loader | |
| mod = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(mod) | |
| return mod | |
| def _make_event(event_id: str) -> FloodEvent: | |
| return FloodEvent( | |
| event_id=event_id, | |
| country="X", | |
| iso="XXX", | |
| region="Europe", | |
| location=None, | |
| latitude=None, | |
| longitude=None, | |
| start_date="2020-01-01", | |
| end_date="2020-01-01", | |
| disaster_subtype=None, | |
| origin=None, | |
| magnitude=None, | |
| magnitude_scale=None, | |
| total_deaths=None, | |
| total_affected=None, | |
| total_damage_k_usd=None, | |
| ) | |
| def test_filter_skips_events_with_cached_articles(tmp_path: Path) -> None: | |
| mod = _load_collect_module() | |
| articles_dir = tmp_path | |
| (articles_dir / "EVT-A_abc123.json").write_text("{}") | |
| (articles_dir / "EVT-C_def456.json").write_text("{}") | |
| events = [_make_event("EVT-A"), _make_event("EVT-B"), _make_event("EVT-C")] | |
| kept = mod._filter_skip_existing(events, articles_dir) | |
| assert [e.event_id for e in kept] == ["EVT-B"] | |
| def test_filter_keeps_all_when_articles_dir_empty(tmp_path: Path) -> None: | |
| mod = _load_collect_module() | |
| articles_dir = tmp_path # empty | |
| events = [_make_event("EVT-A"), _make_event("EVT-B")] | |
| kept = mod._filter_skip_existing(events, articles_dir) | |
| assert [e.event_id for e in kept] == ["EVT-A", "EVT-B"] | |
| def test_filter_keeps_all_when_articles_dir_missing(tmp_path: Path) -> None: | |
| mod = _load_collect_module() | |
| articles_dir = tmp_path / "does_not_exist" | |
| events = [_make_event("EVT-A")] | |
| kept = mod._filter_skip_existing(events, articles_dir) | |
| assert [e.event_id for e in kept] == ["EVT-A"] | |