"""Unit tests for scripts/collect_historical_webcams.py.""" from __future__ import annotations from datetime import date import pandas as pd from scripts.collect_historical_webcams import HistoricalWebcamCollector class _StoreStub: """Minimal store stub for historical webcam collector tests.""" def __init__(self, station_df: pd.DataFrame, crowd_df: pd.DataFrame): self._station_df = station_df self._crowd_df = crowd_df def read_dataframe(self, *_: object, **__: object) -> pd.DataFrame: return self._station_df.copy() def load_crowd_analyses(self, **_: object) -> pd.DataFrame: return self._crowd_df.copy() def _collector_with_store(store: _StoreStub) -> HistoricalWebcamCollector: collector = object.__new__(HistoricalWebcamCollector) collector.store = store collector.get_available_date_range = lambda _station_id: None return collector def test_get_missing_dates_uses_analysis_date_column(): """Missing-date detection must use analysis_date instead of timestamp.""" station_df = pd.DataFrame( { "date": pd.to_datetime(["2026-03-10", "2026-03-11", "2026-03-12"]), "station_id": ["pajares", "pajares", "pajares"], } ) crowd_df = pd.DataFrame( { "analysis_date": ["2026-03-10", "2026-03-11"], "station_id": ["pajares", "pajares"], } ) collector = _collector_with_store(_StoreStub(station_df, crowd_df)) missing = collector.get_missing_dates("pajares") assert missing == [date(2026, 3, 12)] def test_is_already_processed_uses_analysis_date_column(): """Processed-day checks must match against analysis_date values.""" collector = _collector_with_store( _StoreStub( station_df=pd.DataFrame(), crowd_df=pd.DataFrame( { "analysis_date": ["2026-03-10"], "station_id": ["pajares"], } ), ) ) assert collector._is_already_processed("pajares", date(2026, 3, 10)) assert not collector._is_already_processed("pajares", date(2026, 3, 11))