fix(core): clean predictor debt

This commit is contained in:
dasemu committed 2026-03-13 10:51:29 +01:00
1 parent e952cea417
commit e6add1b727
10 files changed
+268 -58

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
"""Unit tests for daily prediction horizon helpers."""
from __future__ import annotations
from datetime import date
import pandas as pd
from scripts.collect_daily_data import (
_available_prediction_dates,
_select_forecast_row_for_date,
)
def _forecast_df(start: str = "2026-03-13", periods: int = 7) -> pd.DataFrame:
"""Build a minimal forecast frame with consecutive daily dates."""
dates = pd.date_range(start=start, periods=periods, freq="D")
return pd.DataFrame(
{
"date": dates,
"station_id": ["pajares"] * periods,
"temperature_2m_max": [1.0] * periods,
}
)
def test_available_prediction_dates_match_real_forecast_horizon():
"""Helpers must not invent an eighth day when the provider returns seven."""
forecast_dates = _available_prediction_dates(
{"pajares": _forecast_df()},
base_date=date(2026, 3, 13),
include_today=True,
)
assert forecast_dates == [
date(2026, 3, 13),
date(2026, 3, 14),
date(2026, 3, 15),
date(2026, 3, 16),
date(2026, 3, 17),
date(2026, 3, 18),
date(2026, 3, 19),
]
def test_available_prediction_dates_can_skip_today():
"""Afternoon scheduler runs must start from tomorrow when requested."""
forecast_dates = _available_prediction_dates(
{"pajares": _forecast_df()},
base_date=date(2026, 3, 13),
include_today=False,
)
assert forecast_dates == [
date(2026, 3, 14),
date(2026, 3, 15),
date(2026, 3, 16),
date(2026, 3, 17),
date(2026, 3, 18),
date(2026, 3, 19),
]
def test_select_forecast_row_returns_none_when_target_date_is_missing():
"""Out-of-range requests must fail cleanly instead of reusing the last row."""
row = _select_forecast_row_for_date(
_forecast_df(),
station_id="pajares",
target_date=date(2026, 3, 20),
)
assert row is None
+21
View File
@@ -5,8 +5,11 @@ from __future__ import annotations
import sys
import types
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo
import yaml
telegram_module = types.ModuleType("telegram")
@@ -186,3 +189,21 @@ def test_format_daily_predictions_includes_all_three_stations():
assert "San Isidro" in text
assert "Fuentes de Invierno" in text
assert "2026-03-14" in text
def test_station_metadata_is_loaded_from_shared_config():
"""Bot station labels and max km must come from config/stations.yaml."""
config_path = Path(__file__).resolve().parents[1] / "config" / "stations.yaml"
with config_path.open("r", encoding="utf-8") as fh:
stations = yaml.safe_load(fh)["stations"]
expected = {
station_id: {
"name": station_cfg["name"],
"max_km": float(station_cfg["max_km_skiable"]),
}
for station_id, station_cfg in stations.items()
}
assert telegram_bot.STATIONS == expected
assert telegram_bot.STATION_ORDER == tuple(stations.keys())