Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
49 lignes
2.2 KiB
Python
49 lignes
2.2 KiB
Python
import json, os, sys, unittest
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "collecte"))
|
|
import collecter_weatherlink as c
|
|
|
|
REPONSE = json.dumps({"station_id": 1, "generated_at": 1761700000, "sensors": [
|
|
{"lsid": 1, "data": [{"ts": 1761699900, "temp": 50.0, "hum": 80.4, "dew_point": 44.0, "wind_speed_avg_last_10_min": 10.0,
|
|
"wind_dir_scalar_avg_last_10_min": 370, "rainfall_daily_in": 0.1, "temp_in": 70.0, "hum_in": 40}]},
|
|
{"lsid": 2, "data": [{"ts": 1761699900, "bar_sea_level": 30.0}]}]})
|
|
|
|
|
|
class T(unittest.TestCase):
|
|
def test_conversion_et_filtre(self):
|
|
m = c.mesure_depuis_current(REPONSE)
|
|
self.assertEqual(m["temperature"], 10.0)
|
|
self.assertEqual(m["humidite"], 80)
|
|
self.assertEqual(m["vent_direction"], 10)
|
|
self.assertAlmostEqual(m["pression"], 1015.9, 1)
|
|
self.assertNotIn("temp_in", m)
|
|
self.assertTrue(set(m) <= set(c.CHAMPS_PUBLICS))
|
|
|
|
def test_sans_temperature(self):
|
|
with self.assertRaises(c.ErreurWL):
|
|
c.mesure_depuis_current(json.dumps({"sensors": [{"data": [{"ts": 1, "hum": 50}]}]}))
|
|
|
|
def test_exemple_propre(self):
|
|
cfg = json.load(open(c.CONFIG_DEFAUT, encoding="utf-8"))
|
|
obj = c.construire_exemple(cfg)
|
|
c.verifier_public(obj)
|
|
self.assertTrue(all(s["exemple"] for s in obj["stations"].values()))
|
|
|
|
def test_fuite_refusee(self):
|
|
for mauvais in ({"x": "192.168.1.15"}, {"x": "/mnt/hdd3/a"}, {"x": "api-key"}, {"x": "weather-db"}):
|
|
with self.assertRaises(ValueError):
|
|
c.verifier_public(mauvais)
|
|
with self.assertRaises(ValueError):
|
|
c.verifier_public({"stations": {"a": {"releves": [{"dh_utc": "x", "trame_brute": 1}]}}})
|
|
|
|
def test_sans_cle_hors_ligne(self):
|
|
for v in ("MC_WL_BRUYERES_CLE", "MC_WL_ALLIERES_CLE", "MC_WL_VP2_CLE"):
|
|
os.environ.pop(v, None)
|
|
cfg = json.load(open(c.CONFIG_DEFAUT, encoding="utf-8"))
|
|
obj, j = c.collecter(cfg, {}, 1)
|
|
c.verifier_public(obj)
|
|
self.assertTrue(all(s["hors_ligne"] for s in obj["stations"].values()))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|