data/stations.json, data/station/<source>-<id>.json (48 h), data/etat.json, ecriture atomique, attribution par source, nom de fichier assaini. direct.json de compatibilite optionnel (MC_DIRECT_COMPAT). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lignes
2.8 KiB
Python
63 lignes
2.8 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from meteo_collecte import publier
|
|
from meteo_collecte.commun import lire_json
|
|
from meteo_collecte.stockage import Stockage
|
|
from tests.aide import config, t
|
|
|
|
|
|
class TestPublier(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.cfg, _ = config(self.tmp.name)
|
|
self.s = Stockage(Path(self.tmp.name) / "m.sqlite")
|
|
self.s.maj_station("infoclimat", "000T0", nom="Lans", licence="CC BY")
|
|
self.s.maj_station("romma", "80", nom="Lans-en-Vercors", lat=45.1, lien="https://www.romma.fr/station_24.php?id=80")
|
|
self.s.ajouter("infoclimat", "000T0", [{"dh_utc": "2026-10-14 10:00:00", "temperature": 1},
|
|
{"dh_utc": "2026-10-16 11:50:00", "temperature": 5}], t(12, 0))
|
|
self.s.ajouter("romma", "80", [{"dh_utc": "2026-10-16 11:50:00", "temperature": 7, "pluie_cumul": 0.2}], t(12, 0))
|
|
self.data = self.cfg.public / "data"
|
|
|
|
def tearDown(self):
|
|
self.s.fermer()
|
|
self.tmp.cleanup()
|
|
|
|
def test_index_fiches_etat(self):
|
|
n = publier.publier(self.cfg, self.s, t(12, 4))
|
|
self.assertEqual(n, 2)
|
|
idx = lire_json(self.data / "stations.json", {})
|
|
par = {x["id"]: x for x in idx["stations"]}
|
|
self.assertEqual(par["infoclimat-000T0"]["derniere"]["temperature"], 5)
|
|
self.assertEqual(par["romma-80"]["derniere"]["temperature"], 7) # pas de mélange entre sources
|
|
self.assertEqual(par["romma-80"]["age_min"], 14)
|
|
self.assertIn("ROMMA n'est pas responsable", par["romma-80"]["attribution"])
|
|
fiche = lire_json(self.data / "station" / "infoclimat-000T0.json", {})
|
|
self.assertEqual(len(fiche["releves"]), 1) # 48 h seulement
|
|
etat = lire_json(self.data / "etat.json", {})
|
|
self.assertIn("romma", etat["sources"])
|
|
self.assertEqual(sorted(p.name for p in self.data.iterdir()), ["etat.json", "station", "stations.json"])
|
|
|
|
def test_aucun_secret_publie(self):
|
|
publier.publier(self.cfg, self.s, t(12, 4))
|
|
for p in self.data.rglob("*.json"):
|
|
texte = p.read_text(encoding="utf-8")
|
|
self.assertNotIn("FICTI", texte)
|
|
self.assertNotIn("token", texte)
|
|
self.assertNotIn("apikey", texte)
|
|
|
|
def test_nom_de_fichier_sur(self):
|
|
self.assertEqual(publier.nom_fichier("romma", "../../etc"), "romma-______etc")
|
|
|
|
def test_direct_compat(self):
|
|
self.cfg.direct_compat = Path(self.tmp.name) / "direct.json"
|
|
publier.publier(self.cfg, self.s, t(12, 4))
|
|
d = lire_json(self.cfg.direct_compat, {})
|
|
self.assertEqual(list(d["stations"]), ["000T0"])
|
|
self.assertIn("pluie_cumul_0h", d["stations"]["000T0"]["releves"][0])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|