"""Tests de l'extraction Infoclimat, sans réseau : python -I tests/test_infoclimat.py""" import datetime as dt import os import sys sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "collecte")) import extraire_infoclimat as ic # noqa: E402 from collecter_weatherlink import verifier_public # noqa: E402 FIN = dt.datetime(2026, 11, 3, 21, 50, tzinfo=dt.timezone.utc) def rel(min_avant, t, **k): d = FIN - dt.timedelta(minutes=min_avant) r = {"dh_utc": d.strftime("%Y-%m-%d %H:%M:%S"), "temperature": t, "humidite": None, "pression": 1024.0} r.update(k) return r def source(releves_t0, releves_ia=None): return {"stations": {"000T0": {"nom": "Lans – Bruyères", "releves": releves_t0}, "000IA": {"nom": "Méaudre", "releves": releves_ia or []}}, "metadonnees": {"000T0": {"licence": "CC BY"}}} def test_fenetre_ordre_vides_minmax(): obj, _ = ic.extraire(source([rel(10, -2.1), rel(30 * 60, 9.0), rel(20, -1.0, humidite=91)]), FIN) r = obj["stations"]["bruyeres"]["releves"] assert [x["temperature"] for x in r] == [-1.0, -2.1] # plus de 24 h écarté, ordre croissant assert "humidite" not in r[-1] and r[0]["humidite"] == 91 # pas de valeur vide assert r[-1]["temperature_min"] == -2.1 and r[-1]["temperature_max"] == -1.0 assert not obj["stations"]["bruyeres"]["hors_ligne"] def test_hors_ligne_et_absente(): obj, _ = ic.extraire(source([rel(90, 1.0)]), FIN) assert obj["stations"]["bruyeres"]["hors_ligne"] assert obj["stations"]["meaudre"]["hors_ligne"] and obj["stations"]["meaudre"]["releves"] == [] def test_public_propre(): obj, _ = ic.extraire(source([rel(10, -2.1)]), FIN) verifier_public(obj) assert obj["metadonnees"]["bruyeres"]["licence"] == "CC BY" 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)