Files
lingniu-vehicle-ingest/vehicle-map/tests/test_server.py
T

52 lines
2.5 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)
if __name__ == "__main__":
unittest.main()