47 lines
2.0 KiB
Python
47 lines
2.0 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_authorized_vehicle_and_station_data(self):
|
|
def fake_post(path, body):
|
|
if path.endswith("realtime/query"):
|
|
return [
|
|
{"vin": "VIN1", "motionStatus": "driving", "online": True},
|
|
{"vin": "VIN2", "motionStatus": "idle", "online": True},
|
|
{"vin": "VIN3", "motionStatus": "offline", "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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|