vercors.me : collecte Météo-France / Infoclimat / VP2, archive SQLite, accueil en carte, forum (squelette)
- 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>
Cette révision appartient à :
1 Parent
956d08ffd7
révision
ff42faca16
42 fichiers modifiés
+5127
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,79 @@
|
||||
"""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)
|
||||
@@ -0,0 +1,52 @@
|
||||
"""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)
|
||||
@@ -0,0 +1,89 @@
|
||||
"""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)
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Test de l'import open data : python -I tests/test_opendata.py"""
|
||||
import gzip
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "collecte"))
|
||||
import importer_opendata as io # noqa: E402
|
||||
|
||||
|
||||
def test_import(tmp="test-od.csv.gz"):
|
||||
lignes = ["NUM_POSTE;NOM_USUEL;AAAAMMJJHH;T;TN;TX;TD;U;FF;FXI;DD;RR1;PMER;NEIGETOT",
|
||||
"38548001;VILLARD;2026010112;4.4;1.7;4.4;-3.3;58;1.8;3.9;220;0.0;;0",
|
||||
"38548001;VILLARD;2026010113;;;;;;;;;;;",
|
||||
"99999999;AUTRE;2026010112;9.9;;;;;;;;;;"]
|
||||
with gzip.open(tmp, "wt", encoding="utf-8") as f:
|
||||
f.write("\n".join(lignes) + "\n")
|
||||
try:
|
||||
db = sqlite3.connect(":memory:")
|
||||
db.execute(io.SCHEMA)
|
||||
assert io.importer_fichier(db, tmp) == 2 # le poste inconnu est ignoré
|
||||
t, u, p = db.execute("SELECT t, u, pmer FROM horaire WHERE id = 'villard' ORDER BY ts LIMIT 1").fetchone()
|
||||
assert (t, u, p) == (4.4, 58.0, None)
|
||||
assert io.importer_fichier(db, tmp) == 2 and db.execute("SELECT COUNT(*) FROM horaire").fetchone()[0] == 2 # ré-import sans doublon
|
||||
finally:
|
||||
os.remove(tmp)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_import()
|
||||
print("1 test OK")
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Tests de l'extraction VP2, sans réseau : python -I tests/test_vp2.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_vp2 as vp # noqa: E402
|
||||
from collecter_weatherlink import verifier_public # noqa: E402
|
||||
|
||||
BASE = 1793779200 # 2026-11-04 08:00:00 UTC
|
||||
|
||||
|
||||
def l(ts, t=1.0, h=90.0, v=2.0, jour=0.0):
|
||||
return (BASE + ts, {"t": t, "h": h, "p": 1020.0, "v": v, "dir": 200.0, "jour": jour})
|
||||
|
||||
|
||||
def test_tranches_unites_pluie():
|
||||
lignes = [l(0, jour=1.0), l(20, jour=1.0), l(600, jour=1.4), l(1200, jour=1.4), l(1800, jour=1.8)]
|
||||
r = vp.releves(lignes)
|
||||
assert len(r) == 4 # une tranche de 10 minutes chacune
|
||||
assert r[-1]["vent_moyen"] == 7.2 and r[-1]["vent_direction"] == 200 # m/s -> km/h
|
||||
assert r[-1]["pluie_1h"] == 0.8 and r[-1]["pluie_cumul_0h"] == 1.8
|
||||
|
||||
|
||||
def test_remise_a_zero_minuit():
|
||||
r = vp.releves([l(0, jour=5.0), l(600, jour=0.2)])
|
||||
assert r[-1]["pluie_1h"] == 0.2 # cumul du jour reparti à zéro : on ne compte pas −4,8
|
||||
|
||||
|
||||
def test_point_de_rosee_et_hors_ligne():
|
||||
assert abs(vp.point_de_rosee(10.0, 100.0) - 10.0) < 0.01
|
||||
fin = dt.datetime.fromtimestamp(BASE + 7200, dt.timezone.utc)
|
||||
obj = vp.construire([l(0)], fin)
|
||||
assert obj["stations"]["lans"]["hors_ligne"] # dernier relevé vieux de 2 h
|
||||
verifier_public(obj)
|
||||
|
||||
|
||||
def test_aucune_donnee_interieure():
|
||||
obj = vp.construire([l(0)], dt.datetime.fromtimestamp(BASE + 60, dt.timezone.utc))
|
||||
texte = str(obj)
|
||||
assert "int" not in texte.replace("interieur", "") or "temp_int" not in texte
|
||||
assert set(obj["stations"]["lans"]["releves"][0]) <= {"dh_utc", "temperature", "humidite", "point_de_rosee", "pression",
|
||||
"vent_moyen", "vent_direction", "pluie_1h", "pluie_cumul_0h", "temperature_min", "temperature_max"}
|
||||
|
||||
|
||||
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)
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur