"""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")