Files
skipredictor/tests/test_sqlite_store.py
T

112 lines
3.4 KiB
Python

"""Unit tests for the SQLite-backed persistence layer."""
from __future__ import annotations
from storage import SQLiteStore
def test_prediction_run_roundtrip(tmp_path):
"""Prediction payloads must round-trip through SQLite unchanged."""
store = SQLiteStore(tmp_path / "skipredictor.db")
payload = {
"generated_at": "2026-03-13T10:00:00+00:00",
"prediction_date": "2026-03-14",
"stations": {
"pajares": {
"station_name": "Valgrande-Pajares",
"open_probability": 0.82,
"km_open": 12.4,
"crowd_level": "medium",
"weather_summary": "Temp -2-5 C",
"rule_overrides": ["Wind below threshold."],
"prediction_source": "ml",
}
},
}
store.save_prediction_run(payload)
loaded = store.load_latest_predictions()
assert loaded == payload
def test_feature_importances_replace_latest_values(tmp_path):
"""Saving importances twice for the same target must replace older rows."""
store = SQLiteStore(tmp_path / "skipredictor.db")
store.save_feature_importances(
target="station_open",
importances={"snow_depth": 0.9, "wind_gusts_10m_max": 0.5},
)
store.save_feature_importances(
target="station_open",
importances={"snow_depth": 0.2, "temperature_2m_mean": 0.8},
)
assert store.load_feature_importances("station_open") == [
("temperature_2m_mean", 0.8),
("snow_depth", 0.2),
]
def test_evaluation_run_roundtrip(tmp_path):
"""Evaluation reports must be persisted and reloaded from SQLite."""
store = SQLiteStore(tmp_path / "skipredictor.db")
report = {
"station_open": {"accuracy": 0.91},
"km_open": {"mae": 1.2},
}
run_id = store.save_evaluation_run(
dataset_table="historical_featured_dataset",
report=report,
models_dir="ml/models",
)
loaded = store.load_latest_evaluation_run()
assert loaded is not None
assert loaded["id"] == run_id
assert loaded["dataset_table"] == "historical_featured_dataset"
assert loaded["report"] == report
assert loaded["models_dir"] == "ml/models"
def test_crowd_analysis_roundtrip(tmp_path):
"""Crowd analysis rows must be queryable with parsed confidence scores."""
store = SQLiteStore(tmp_path / "skipredictor.db")
store.save_crowd_analysis(
{
"timestamp": "2026-03-13T11:00:00",
"station_id": "sanisidro",
"image_path": "/tmp/frame.jpg",
"cars_detected": 5,
"people_detected": 2,
"buses_detected": 0,
"total_vehicles": 5,
"crowd_density": "low",
"confidence_scores": [0.91, 0.88],
}
)
store.save_crowd_analysis(
{
"timestamp": "2026-03-13T12:00:00",
"station_id": "sanisidro",
"image_path": "/tmp/frame2.jpg",
"cars_detected": 25,
"people_detected": 4,
"buses_detected": 1,
"total_vehicles": 26,
"crowd_density": "medium",
"confidence_scores": [0.77],
}
)
rows = store.load_crowd_analyses(
station_id="sanisidro",
analysis_date="2026-03-13",
)
assert len(rows) == 2
assert rows.iloc[0]["confidence_scores"] == [0.91, 0.88]
assert rows.iloc[1]["crowd_density"] == "medium"