"""Tests de l'archive, sans réseau : python -I tests/test_archive.py""" import os import sys sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "collecte")) import archiver_historique as ar # noqa: E402 import collecter_weatherlink as wl # noqa: E402 BASE = 1793779200 # 2026-11-04 08:00:00 UTC (heure pile) def dh(ts): import datetime as dt return dt.datetime.fromtimestamp(ts, dt.timezone.utc).strftime("%Y-%m-%d %H:%M:%S") def source(sid, points): return {"stations": {sid: {"releves": [dict(dh_utc=dh(ts), **v) for ts, v in points]}}} def test_tranches_et_priorite(): db = ar.ouvrir(":memory:") ar.ingerer(db, source("bruyeres", [(BASE, {"temperature": 1.0, "humidite": 80.0})])) # même tranche de 10 min, autre source : la température est remplacée, l'humidité absente ne l'efface pas ar.ingerer(db, source("bruyeres", [(BASE + 120, {"temperature": 2.0, "vent_moyen": 5.0})])) t, h, v = db.execute("SELECT t, h, v FROM obs WHERE id = 'bruyeres'").fetchone() assert (t, h, v) == (2.0, 80.0, 5.0) assert db.execute("SELECT COUNT(*) FROM obs").fetchone()[0] == 1 def test_correspondance_mf_et_sans_temperature(): db = ar.ouvrir(":memory:") ar.ingerer(db, source("mf-villard", [(BASE, {"temperature": 3.0}), (BASE + 600, {"humidite": 90.0})]), ar.CORRESPONDANCE_MF) assert [r[0] for r in db.execute("SELECT id FROM obs")] == ["villard"] def test_courbes_capteurs_et_pluie_horaire(): db = ar.ouvrir(":memory:") pts = [(BASE + i * 600, {"temperature": float(i % 5), "pluie_1h": 0.4 if i % 6 == 0 else 0.0}) for i in range(36)] ar.ingerer(db, source("autrans", pts)) ar.ingerer(db, source("meaudre", [(BASE, {"temperature": 1.0, "vent_moyen": 3.0, "humidite": 90.0})])) cb = ar.courbes(db, BASE + 36 * 600) assert cb["autrans"]["capteurs"] == {"vent": False, "pluie": True, "humidite": False} assert cb["meaudre"]["capteurs"] == {"vent": True, "pluie": False, "humidite": True} assert len(cb["autrans"]["t"]) == 7 * 48 + 1 and cb["autrans"]["pas"] == 1800 r = [x for x in cb["autrans"]["r"] if x is not None] assert 0.4 in r # pluie relevée à l'heure pile def test_ecriture_publique(tmp="test-archive-sortie"): import json import shutil db = ar.ouvrir(":memory:") ar.ingerer(db, source("die", [(BASE, {"temperature": 1.0})])) try: idx = ar.ecrire(tmp, BASE + 600, db) assert idx["die"]["pluie"] is False and os.path.exists(os.path.join(tmp, "die.json")) wl.verifier_public({"stations": {}}) texte = open(os.path.join(tmp, "die.json"), encoding="utf-8").read() assert "/home" not in texte and json.loads(texte)["id"] == "die" finally: shutil.rmtree(tmp, ignore_errors=True) def test_alleger_weatherlink(): maintenant = wl.maintenant() rel = [{"dh_utc": wl.fmt_utc(maintenant - wl.dt.timedelta(minutes=m)), "temperature": 1.0} for m in range(0, 300)] out = wl.alleger(rel, maintenant) assert len(out) < 60 and out[-1]["dh_utc"] == rel[0]["dh_utc"] # le plus récent est gardé assert len({r["dh_utc"] for r in out}) == len(out) 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)