- collecte : collecter_meteofrance, extraire_infoclimat, extraire_vp2, archiver_historique, importer_opendata ; WeatherLink allégé (alleger) - tests : meteofrance, infoclimat, vp2, archive, opendata (tous verts) - maquettes/accueil-carte : sources de la maquette (générateurs de tuiles, page, À propos, CGU, confidentialité) - forum : noyau local (rôles, droits, SQLite, serveur de test, 18 tests) - deploy : Caddyfile.vercors.me.v2 (non appliqué) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
90 lignes
3.6 KiB
Python
90 lignes
3.6 KiB
Python
"""Tests du collecteur Météo-France, sans réseau : python -I tests/test_meteofrance.py"""
|
|
import datetime as dt
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "collecte"))
|
|
import collecter_meteofrance as mf # noqa: E402
|
|
from collecter_weatherlink import verifier_public # noqa: E402
|
|
|
|
FIN = dt.datetime(2026, 11, 3, 21, 30, tzinfo=dt.timezone.utc)
|
|
|
|
|
|
def enr(minutes_avant, t=272.15, rr=0.0, **extra):
|
|
d = FIN - dt.timedelta(minutes=minutes_avant)
|
|
r = {"geo_id_insee": "38548001", "validity_time": d.strftime("%Y-%m-%dT%H:%M:%SZ"), "t": t, "rr_per": rr,
|
|
"td": None, "u": None, "ff": None, "raf10": None, "dd": None, "pmer": None}
|
|
r.update(extra)
|
|
return r
|
|
|
|
|
|
def test_conversions():
|
|
p = mf.releve_depuis_mf(enr(0, t=272.15, td=271.15, u=93, ff=2.0, raf10=5.0, dd=250, pmer=101820))
|
|
assert p["temperature"] == -1.0 and p["point_de_rosee"] == -2.0 and p["humidite"] == 93
|
|
assert p["vent_moyen"] == 7.2 and p["vent_rafales"] == 18.0 and p["vent_direction"] == 250
|
|
assert p["pression"] == 1018.2 and p["dh_utc"] == "2026-11-03 21:30:00"
|
|
|
|
|
|
def test_sans_temperature_ignore():
|
|
assert mf.releve_depuis_mf(enr(0, t=None)) is None
|
|
|
|
|
|
def test_releves_ordre_fenetre_minmax():
|
|
# l'API renvoie du plus récent au plus ancien, sur plusieurs jours
|
|
brut = [enr(m, t=273.15 + (m % 50) / 10) for m in range(0, 5 * 24 * 60, 6)]
|
|
rel = mf.construire_releves(brut, FIN, heures=24)
|
|
assert len(rel) == 240 and rel[0]["dh_utc"] < rel[-1]["dh_utc"]
|
|
ts = [r["temperature"] for r in rel]
|
|
assert rel[-1]["temperature_min"] == min(ts) and rel[-1]["temperature_max"] == max(ts)
|
|
|
|
|
|
def test_pluie_glissante_et_depuis_minuit():
|
|
brut = [enr(m, rr=0.2) for m in range(0, 180, 6)] # 0,2 mm toutes les 6 min pendant 3 h
|
|
rel = mf.construire_releves(brut, FIN, heures=24)
|
|
assert rel[-1]["pluie_1h"] == 2.0 # 10 périodes dans l'heure
|
|
# minuit à Paris = 23:00 UTC la veille : les 3 h sont dans la journée
|
|
assert rel[-1]["pluie_cumul_0h"] == round(0.2 * 30, 1)
|
|
|
|
|
|
def test_hors_ligne_et_erreur_sans_cle():
|
|
cfg = {"stations": {"mf-x": {"id": "1", "nom": "X", "lat": 45.1, "lon": 5.5, "altitude": 1000}}}
|
|
obj, journal = mf.collecter(cfg, "", pause=0)
|
|
assert obj["stations"]["mf-x"]["hors_ligne"] and "sans clé" in journal[0]
|
|
|
|
def vieux(_id, _cle):
|
|
return [enr(120)]
|
|
mf.maintenant = lambda: FIN
|
|
obj, journal = mf.collecter(cfg, "cle-factice", appel=vieux, pause=0)
|
|
assert obj["stations"]["mf-x"]["hors_ligne"] and "trop ancien" in journal[0]
|
|
|
|
|
|
def test_json_public_propre():
|
|
cfg = {"stations": {"mf-x": {"id": "38548001", "nom": "Villard", "lat": 45.0785, "lon": 5.561, "altitude": 1027}}}
|
|
mf.maintenant = lambda: FIN
|
|
obj, _ = mf.collecter(cfg, "cle-secrete-123", appel=lambda i, c: [enr(0, u=90)], pause=0)
|
|
verifier_public(obj)
|
|
texte = json.dumps(obj, ensure_ascii=False)
|
|
assert "cle-secrete-123" not in texte and "38548001" not in texte
|
|
assert not obj["stations"]["mf-x"]["hors_ligne"]
|
|
|
|
|
|
def test_cle_depuis_fichier(tmp="test-mf.env"):
|
|
with open(tmp, "w", encoding="utf-8") as f:
|
|
f.write("AUTRE=x\nMF_DPPAQUETOBS_CLE=valeur-test\n")
|
|
try:
|
|
os.environ.pop("MF_DPPAQUETOBS_CLE", None)
|
|
assert mf.lire_cle("MF_DPPAQUETOBS_CLE", tmp) == "valeur-test"
|
|
assert mf.lire_cle("ABSENTE", tmp) == ""
|
|
finally:
|
|
os.remove(tmp)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
n = 0
|
|
for nom, f in list(globals().items()):
|
|
if nom.startswith("test_") and callable(f):
|
|
f()
|
|
n += 1
|
|
print("%d tests OK" % n)
|