64 lines
3.1 KiB
Python
64 lines
3.1 KiB
Python
import importlib.util
|
|
from pathlib import Path
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
|
|
SPEC = importlib.util.spec_from_file_location("vehicle_map_server", Path(__file__).parents[1] / "server.py")
|
|
server = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(server)
|
|
|
|
|
|
class DashboardTest(unittest.TestCase):
|
|
def test_dashboard_aggregates_public_vehicle_and_station_data(self):
|
|
def fake_post(path, body):
|
|
if path.endswith("realtime/query"):
|
|
return [
|
|
{"vin": "VIN1", "motionStatus": "driving", "speedKmh": 18, "activeToday": True, "online": True},
|
|
{"vin": "VIN2", "motionStatus": "idle", "speedKmh": 0, "activeToday": True, "online": True},
|
|
{"vin": "VIN3", "motionStatus": "offline", "speedKmh": 0, "activeToday": False, "online": False},
|
|
]
|
|
if path.endswith("mileage/query"):
|
|
return [
|
|
{"vin": "VIN1", "status": "NORMAL", "dailyMileageKm": 12.345},
|
|
{"vin": "VIN2", "status": "NORMAL", "dailyMileageKm": 7.655},
|
|
{"vin": "VIN3", "status": "NO_DATA", "dailyMileageKm": None},
|
|
]
|
|
if path.endswith("hydrogen-stations/query"):
|
|
return [{"id": "1", "cooperative": True}, {"id": "2", "cooperative": False}]
|
|
raise AssertionError(path)
|
|
|
|
with patch.object(server, "_post_open_platform", side_effect=fake_post):
|
|
with patch.object(server, "_cache", {}):
|
|
result = server._load_dashboard()
|
|
|
|
self.assertEqual(result["summary"]["totalVehicles"], 3)
|
|
self.assertEqual(result["summary"]["onlineVehicles"], 2)
|
|
self.assertEqual(result["summary"]["drivingVehicles"], 1)
|
|
self.assertEqual(result["summary"]["todayMileageKm"], 20.0)
|
|
self.assertEqual(result["summary"]["totalStations"], 2)
|
|
self.assertEqual(result["summary"]["cooperativeStations"], 1)
|
|
self.assertEqual(result["vehicles"][0]["dailyMileageKm"], 12.345)
|
|
self.assertEqual(result["vehicles"][2]["dailyMileageKm"], 0)
|
|
|
|
def test_static_asset_version_is_a_content_fingerprint(self):
|
|
self.assertEqual(len(server.STATIC_ASSET_VERSION), 16)
|
|
self.assertRegex(server.STATIC_ASSET_VERSION, r"^[0-9a-f]{16}$")
|
|
index_template = (Path(__file__).parents[1] / "index.html").read_text(encoding="utf-8")
|
|
self.assertEqual(index_template.count("__ASSET_VERSION__"), 2)
|
|
|
|
def test_default_cache_window_is_two_minutes(self):
|
|
self.assertEqual(server.DASHBOARD_CACHE_SECONDS, 120)
|
|
self.assertEqual(server.STATION_CACHE_SECONDS, 120)
|
|
|
|
def test_cache_reuses_dashboard_snapshot_within_window(self):
|
|
calls = []
|
|
with patch.object(server, "_cache", {}):
|
|
first = server._cached("dashboard", 120, lambda: calls.append("load") or {"version": 1})
|
|
second = server._cached("dashboard", 120, lambda: calls.append("load") or {"version": 2})
|
|
self.assertEqual(calls, ["load"])
|
|
self.assertEqual(first, second)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|