88 lines
2.4 KiB
Python
88 lines
2.4 KiB
Python
"""Tests unitaires du service Open-Meteo enrichi."""
|
|
|
|
from datetime import date as real_date
|
|
|
|
import app.services.meteo as meteo
|
|
|
|
|
|
class _DummyResponse:
|
|
def __init__(self, payload: dict):
|
|
self._payload = payload
|
|
|
|
def raise_for_status(self) -> None:
|
|
return None
|
|
|
|
def json(self) -> dict:
|
|
return self._payload
|
|
|
|
|
|
def test_fetch_and_store_forecast_enriched(monkeypatch):
|
|
payload = {
|
|
"daily": {
|
|
"time": ["2026-02-21", "2026-02-22"],
|
|
"temperature_2m_min": [1.2, 2.3],
|
|
"temperature_2m_max": [8.4, 9.7],
|
|
"precipitation_sum": [0.5, 1.0],
|
|
"wind_speed_10m_max": [12.0, 15.0],
|
|
"weather_code": [3, 61],
|
|
"relative_humidity_2m_max": [88, 92],
|
|
"et0_fao_evapotranspiration": [0.9, 1.1],
|
|
},
|
|
"hourly": {
|
|
"time": [
|
|
"2026-02-21T00:00",
|
|
"2026-02-21T01:00",
|
|
"2026-02-22T00:00",
|
|
"2026-02-22T01:00",
|
|
],
|
|
"soil_temperature_0cm": [4.0, 6.0, 8.0, 10.0],
|
|
},
|
|
}
|
|
|
|
def _fake_get(*_args, **_kwargs):
|
|
return _DummyResponse(payload)
|
|
|
|
monkeypatch.setattr(meteo.httpx, "get", _fake_get)
|
|
|
|
rows = meteo.fetch_and_store_forecast(lat=45.1, lon=4.0)
|
|
|
|
assert len(rows) == 2
|
|
assert rows[0]["date"] == "2026-02-21"
|
|
assert rows[0]["label"] == "Couvert"
|
|
assert rows[0]["sol_0cm"] == 5.0
|
|
assert rows[0]["etp_mm"] == 0.9
|
|
assert rows[1]["label"] == "Pluie légère"
|
|
assert rows[1]["sol_0cm"] == 9.0
|
|
|
|
|
|
def test_fetch_and_store_forecast_handles_http_error(monkeypatch):
|
|
def _boom(*_args, **_kwargs):
|
|
raise RuntimeError("network down")
|
|
|
|
monkeypatch.setattr(meteo.httpx, "get", _boom)
|
|
|
|
rows = meteo.fetch_and_store_forecast()
|
|
assert rows == []
|
|
|
|
|
|
def test_fetch_forecast_filters_from_today(monkeypatch):
|
|
class _FakeDate(real_date):
|
|
@classmethod
|
|
def today(cls):
|
|
return cls(2026, 2, 22)
|
|
|
|
monkeypatch.setattr(meteo, "date", _FakeDate)
|
|
monkeypatch.setattr(
|
|
meteo,
|
|
"fetch_and_store_forecast",
|
|
lambda *_args, **_kwargs: [
|
|
{"date": "2026-02-21", "x": 1},
|
|
{"date": "2026-02-22", "x": 2},
|
|
{"date": "2026-02-23", "x": 3},
|
|
],
|
|
)
|
|
|
|
out = meteo.fetch_forecast(days=14)
|
|
|
|
assert [d["date"] for d in out["days"]] == ["2026-02-22", "2026-02-23"]
|