Skip to content

Commit 5e0e9a6

Browse files
committed
Add tests for EVChargerPool
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent c8ce30b commit 5e0e9a6

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# License: MIT
2+
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Tests for the `EVChargerPool`."""
5+
6+
from __future__ import annotations
7+
8+
import asyncio
9+
from typing import Optional
10+
11+
from frequenz.api.microgrid import ev_charger_pb2
12+
from pytest_mock import MockerFixture
13+
14+
from frequenz.sdk.timeseries.ev_charger_pool import (
15+
EVChargerPool,
16+
EVChargerPoolStates,
17+
EVChargerState,
18+
)
19+
from tests.timeseries.mock_microgrid import MockMicrogrid
20+
21+
22+
class TestEVChargerPool:
23+
"""Tests for the `EVChargerPool`."""
24+
25+
async def test_state_updates(self, mocker: MockerFixture) -> None:
26+
"""Test ev charger state updates are visible."""
27+
28+
mockgrid = MockMicrogrid(grid_side_meter=False, sample_rate_s=0.01)
29+
mockgrid.add_ev_chargers(5)
30+
await mockgrid.start(mocker)
31+
32+
pool = EVChargerPool()
33+
34+
states = pool.states()
35+
36+
async def check_next_state(
37+
expected: dict[int, EVChargerState],
38+
latest: Optional[tuple[int, EVChargerState]],
39+
) -> EVChargerPoolStates:
40+
pool_states = await states.receive()
41+
assert pool_states.latest_change() == latest
42+
assert pool_states._states == expected # pylint: disable=protected-access
43+
return pool_states
44+
45+
## check that all chargers are in idle state.
46+
expected_states = {evc_id: EVChargerState.IDLE for evc_id in mockgrid.evc_ids}
47+
assert len(expected_states) == 5
48+
await check_next_state(expected_states, None)
49+
50+
## check that EV_PLUGGED state gets set
51+
await asyncio.sleep(0.02)
52+
evc_2_id = mockgrid.evc_ids[2]
53+
mockgrid.evc_states[evc_2_id] = ev_charger_pb2.State(
54+
component_state=ev_charger_pb2.COMPONENT_STATE_READY,
55+
cable_state=ev_charger_pb2.CABLE_STATE_EV_PLUGGED,
56+
)
57+
expected_states[evc_2_id] = EVChargerState.EV_PLUGGED
58+
await check_next_state(expected_states, (evc_2_id, EVChargerState.EV_PLUGGED))
59+
60+
## check that EV_LOCKED state gets set
61+
await asyncio.sleep(0.03)
62+
evc_3_id = mockgrid.evc_ids[3]
63+
mockgrid.evc_states[evc_3_id] = ev_charger_pb2.State(
64+
component_state=ev_charger_pb2.COMPONENT_STATE_READY,
65+
cable_state=ev_charger_pb2.CABLE_STATE_EV_LOCKED,
66+
)
67+
expected_states[evc_3_id] = EVChargerState.EV_LOCKED
68+
await check_next_state(expected_states, (evc_3_id, EVChargerState.EV_LOCKED))
69+
70+
## check that ERROR state gets set
71+
await asyncio.sleep(0.1)
72+
evc_1_id = mockgrid.evc_ids[1]
73+
mockgrid.evc_states[evc_1_id] = ev_charger_pb2.State(
74+
component_state=ev_charger_pb2.COMPONENT_STATE_ERROR,
75+
cable_state=ev_charger_pb2.CABLE_STATE_EV_LOCKED,
76+
)
77+
expected_states[evc_1_id] = EVChargerState.ERROR
78+
await check_next_state(expected_states, (evc_1_id, EVChargerState.ERROR))
79+
80+
await pool._stop() # pylint: disable=protected-access
81+
await mockgrid.cleanup()

0 commit comments

Comments
 (0)