69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
"""Tests de régression de l'analyse MA7D_DROP, sur données réelles uniquement (spec §9)."""
|
|
import functools
|
|
import io
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from contextlib import redirect_stderr
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "src"))
|
|
import energy_check as ec # noqa: E402
|
|
|
|
DATA, VERMONT = ROOT / "data", ROOT / "vermont"
|
|
VERMONT_V = VERMONT / "VERMONT_BusVoltage_2026_04_09_15_00 to 2026_10_09_15_00.csv"
|
|
VERMONT_THERMAL = VERMONT / "VERMONT_sensor_thermal_deg_c_2026_02_22_15_00 to 2026_10_09_15_00.csv"
|
|
JUNGFRAU_V = DATA / "JUNGFRAU_BusVoltage_2026_09_11_14_00 to 2026_10_09_14_00.csv"
|
|
|
|
|
|
def at(*args):
|
|
return datetime(*args, tzinfo=timezone.utc)
|
|
|
|
|
|
@functools.cache
|
|
def stations():
|
|
return ec.load_stations([str(DATA), str(VERMONT)])
|
|
|
|
|
|
class Loading(unittest.TestCase):
|
|
def test_parse_name_metric_with_underscores(self):
|
|
self.assertEqual(ec.parse_name(str(VERMONT_THERMAL)), ("VERMONT", "sensor_thermal_deg_c"))
|
|
|
|
def test_parse_name_rejects_unknown_pattern(self):
|
|
self.assertIsNone(ec.parse_name("data/metadata.csv"))
|
|
|
|
def test_load_csv_empty_value_is_none(self):
|
|
s = ec.load_csv(VERMONT_V)
|
|
self.assertIsNone(s[at(2026, 4, 9, 15)])
|
|
self.assertAlmostEqual(s[at(2026, 4, 9, 16)], 14.011)
|
|
|
|
def test_load_stations_groups_by_station(self):
|
|
self.assertEqual(sorted(stations()), ["FLORIDA", "JUNGFRAU", "MISSISSIPPI", "VERMONT", "WILDSTRUBEL"])
|
|
self.assertEqual(len(stations()["VERMONT"]), 7)
|
|
|
|
def test_same_file_given_twice_is_not_a_duplicate(self):
|
|
self.assertIn("BusVoltage", ec.load_stations([str(DATA), str(JUNGFRAU_V)])["JUNGFRAU"])
|
|
|
|
def test_two_files_same_station_metric_raise(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
shutil.copy(JUNGFRAU_V, Path(d) / "JUNGFRAU_BusVoltage_2026_09_12_14_00 to 2026_10_09_14_00.csv")
|
|
with self.assertRaises(ValueError):
|
|
ec.load_stations([str(DATA), d])
|
|
|
|
def test_unknown_csv_name_is_skipped_with_message(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
shutil.copy(JUNGFRAU_V, Path(d) / "export.csv")
|
|
shutil.copy(JUNGFRAU_V, d)
|
|
err = io.StringIO()
|
|
with redirect_stderr(err):
|
|
st = ec.load_stations([d])
|
|
self.assertEqual(list(st), ["JUNGFRAU"])
|
|
self.assertIn("export.csv", err.getvalue())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|