Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion tests/common_mocks.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
from __future__ import annotations

import time
from unittest.mock import MagicMock

from saic_ismart_client_ng.api.schema import GpsPosition, GpsStatus
from saic_ismart_client_ng.api.vehicle import VehicleStatusResp
from saic_ismart_client_ng.api.vehicle.schema import BasicVehicleStatus
from saic_ismart_client_ng.api.vehicle.schema import (
BasicVehicleStatus,
VehicleModelConfiguration,
VinInfo,
)
from saic_ismart_client_ng.api.vehicle_charging import ChrgMgmtDataResp
from saic_ismart_client_ng.api.vehicle_charging.schema import (
ChrgMgmtData,
RvsChargeStatus,
)

from vehicle_info import VehicleInfo

VIN = "vin10000000000000"

DRIVETRAIN_RUNNING = True
Expand Down Expand Up @@ -166,3 +173,23 @@ def get_mock_charge_management_data_resp(
fuelRangeElec=int(DRIVETRAIN_RANGE_BMS * 10.0),
),
)


def make_vehicle_state_with_target_soc_support(
*, supports_target_soc: bool
) -> MagicMock:
"""Create a minimal mock VehicleState with the desired supports_target_soc flag."""
configurations = []
if supports_target_soc:
configurations.append(
VehicleModelConfiguration(itemCode="BType", itemValue="1")
)
vin_info = VinInfo()
vin_info.vin = "vin_test_000000000"
vin_info.vehicleModelConfiguration = configurations
vehicle_info = VehicleInfo(vin_info, None)

vehicle_state = MagicMock()
vehicle_state.vehicle = vehicle_info
vehicle_state.vin = vehicle_info.vin
return vehicle_state
10 changes: 7 additions & 3 deletions tests/handlers/command/test_drivetrain_chargecurrent_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
from handlers.command.drivetrain.drivetrain_chargecurrent_limit import (
DrivetrainChargeCurrentLimitCommand,
)
from tests.handlers.command.test_drivetrain_soc_guards import _make_vehicle_state
from tests.common_mocks import make_vehicle_state_with_target_soc_support

EXPECTED_LIMIT = DrivetrainChargeCurrentLimitCommand.convert_payload("16A")


class TestDrivetrainChargeCurrentLimitTargetSoc(unittest.IsolatedAsyncioTestCase):
async def test_lfp_vehicle_sends_ignore_target_soc(self) -> None:
vehicle_state = _make_vehicle_state(supports_target_soc=False)
vehicle_state = make_vehicle_state_with_target_soc_support(
supports_target_soc=False
)
vehicle_state.target_soc = TargetBatteryCode.P_80
saic_api = AsyncMock()
handler = DrivetrainChargeCurrentLimitCommand(saic_api, vehicle_state)
Expand All @@ -31,7 +33,9 @@ async def test_lfp_vehicle_sends_ignore_target_soc(self) -> None:
)

async def test_nmc_vehicle_keeps_known_target_soc(self) -> None:
vehicle_state = _make_vehicle_state(supports_target_soc=True)
vehicle_state = make_vehicle_state_with_target_soc_support(
supports_target_soc=True
)
vehicle_state.target_soc = TargetBatteryCode.P_80
saic_api = AsyncMock()
handler = DrivetrainChargeCurrentLimitCommand(saic_api, vehicle_state)
Expand Down
47 changes: 20 additions & 27 deletions tests/handlers/command/test_drivetrain_soc_guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,23 @@

import json
import unittest
from unittest.mock import AsyncMock, MagicMock
from unittest.mock import AsyncMock

from saic_ismart_client_ng.api.vehicle.schema import VehicleModelConfiguration, VinInfo
from saic_ismart_client_ng.api.vehicle_charging import TargetBatteryCode

from handlers.command.base import RESULT_DO_NOTHING, RESULT_REFRESH_ONLY
from handlers.command.drivetrain.drivetrain_charging_schedule import (
DrivetrainChargingScheduleCommand,
)
from handlers.command.drivetrain.drivetrain_soc_target import DrivetrainSoCTargetCommand
from vehicle_info import VehicleInfo


def _make_vehicle_state(*, supports_target_soc: bool) -> MagicMock:
"""Create a minimal mock VehicleState with the desired supports_target_soc flag."""
configurations = []
if supports_target_soc:
configurations.append(
VehicleModelConfiguration(itemCode="BType", itemValue="1")
)
vin_info = VinInfo()
vin_info.vin = "vin_test_000000000"
vin_info.vehicleModelConfiguration = configurations
vehicle_info = VehicleInfo(vin_info, None)

vehicle_state = MagicMock()
vehicle_state.vehicle = vehicle_info
vehicle_state.vin = vehicle_info.vin
return vehicle_state
from tests.common_mocks import make_vehicle_state_with_target_soc_support


class TestDrivetrainSoCTargetGuard(unittest.IsolatedAsyncioTestCase):
async def test_reject_target_soc_when_unsupported(self) -> None:
vehicle_state = _make_vehicle_state(supports_target_soc=False)
vehicle_state = make_vehicle_state_with_target_soc_support(
supports_target_soc=False
)
saic_api = AsyncMock()
handler = DrivetrainSoCTargetCommand(saic_api, vehicle_state)

Expand All @@ -45,7 +28,9 @@ async def test_reject_target_soc_when_unsupported(self) -> None:
saic_api.set_target_battery_soc.assert_not_called()

async def test_allow_target_soc_when_supported(self) -> None:
vehicle_state = _make_vehicle_state(supports_target_soc=True)
vehicle_state = make_vehicle_state_with_target_soc_support(
supports_target_soc=True
)
saic_api = AsyncMock()
handler = DrivetrainSoCTargetCommand(saic_api, vehicle_state)

Expand All @@ -59,7 +44,9 @@ async def test_allow_target_soc_when_supported(self) -> None:

class TestDrivetrainChargingScheduleGuard(unittest.IsolatedAsyncioTestCase):
async def test_reject_until_configured_soc_when_unsupported(self) -> None:
vehicle_state = _make_vehicle_state(supports_target_soc=False)
vehicle_state = make_vehicle_state_with_target_soc_support(
supports_target_soc=False
)
saic_api = AsyncMock()
handler = DrivetrainChargingScheduleCommand(saic_api, vehicle_state)

Expand All @@ -72,7 +59,9 @@ async def test_reject_until_configured_soc_when_unsupported(self) -> None:
saic_api.set_schedule_charging.assert_not_called()

async def test_allow_until_configured_soc_when_supported(self) -> None:
vehicle_state = _make_vehicle_state(supports_target_soc=True)
vehicle_state = make_vehicle_state_with_target_soc_support(
supports_target_soc=True
)
saic_api = AsyncMock()
handler = DrivetrainChargingScheduleCommand(saic_api, vehicle_state)

Expand All @@ -86,7 +75,9 @@ async def test_allow_until_configured_soc_when_supported(self) -> None:

async def test_allow_until_configured_time_regardless(self) -> None:
"""UNTIL_CONFIGURED_TIME should work even without SoC support."""
vehicle_state = _make_vehicle_state(supports_target_soc=False)
vehicle_state = make_vehicle_state_with_target_soc_support(
supports_target_soc=False
)
saic_api = AsyncMock()
handler = DrivetrainChargingScheduleCommand(saic_api, vehicle_state)

Expand All @@ -100,7 +91,9 @@ async def test_allow_until_configured_time_regardless(self) -> None:

async def test_allow_disabled_regardless(self) -> None:
"""DISABLED should work even without SoC support."""
vehicle_state = _make_vehicle_state(supports_target_soc=False)
vehicle_state = make_vehicle_state_with_target_soc_support(
supports_target_soc=False
)
saic_api = AsyncMock()
handler = DrivetrainChargingScheduleCommand(saic_api, vehicle_state)

Expand Down