Skip to content

Commit 065d631

Browse files
committed
Make the _mocks power distributor test fixture component-agnostic
Then it can be used for testing EV chargers and other component caterogies as well. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 0f7631a commit 065d631

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

tests/actor/power_distributing/test_power_distributing.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ class _Mocks:
7070
streamer: MockComponentDataStreamer
7171
"""A mock component data streamer."""
7272

73-
battery_status_sender: Sender[ComponentPoolStatus]
74-
"""Sender for sending status of the batteries."""
73+
component_status_sender: Sender[ComponentPoolStatus]
74+
"""Sender for sending status of the components being tested."""
7575

7676
@classmethod
7777
async def new(
7878
cls,
79+
component_category: ComponentCategory,
7980
mocker: MockerFixture,
8081
graph: _MicrogridComponentGraph | None = None,
8182
grid_meter: bool | None = None,
@@ -97,11 +98,13 @@ async def new(
9798
dp = microgrid._data_pipeline._DATA_PIPELINE
9899
assert dp is not None
99100

100-
return cls(
101-
mockgrid,
102-
streamer,
103-
dp._battery_power_wrapper.status_channel.new_sender(),
104-
)
101+
if component_category == ComponentCategory.BATTERY:
102+
return cls(
103+
mockgrid,
104+
streamer,
105+
dp._battery_power_wrapper.status_channel.new_sender(),
106+
)
107+
raise ValueError(f"Unsupported component category: {component_category}")
105108

106109
async def stop(self) -> None:
107110
"""Stop the mocks."""
@@ -120,12 +123,15 @@ async def stop(self) -> None:
120123
@asynccontextmanager
121124
async def _mocks(
122125
mocker: MockerFixture,
126+
component_category: ComponentCategory,
123127
*,
124128
graph: _MicrogridComponentGraph | None = None,
125129
grid_meter: bool | None = None,
126130
) -> AsyncIterator[_Mocks]:
127131
"""Initialize the mocks."""
128-
mocks = await _Mocks.new(mocker, graph=graph, grid_meter=grid_meter)
132+
mocks = await _Mocks.new(
133+
component_category, mocker, graph=graph, grid_meter=grid_meter
134+
)
129135
try:
130136
yield mocks
131137
finally:
@@ -166,7 +172,7 @@ async def _patch_battery_pool_status(
166172
".ComponentPoolStatusTracker",
167173
return_value=mock,
168174
)
169-
await mocks.battery_status_sender.send(
175+
await mocks.component_status_sender.send(
170176
ComponentPoolStatus(
171177
working=set(mocks.microgrid.battery_ids), uncertain=set()
172178
)
@@ -262,7 +268,7 @@ async def init_component_data(
262268

263269
async def test_power_distributor_one_user(self, mocker: MockerFixture) -> None:
264270
"""Test if power distribution works with a single user."""
265-
mocks = await _Mocks.new(mocker)
271+
mocks = await _Mocks.new(ComponentCategory.BATTERY, mocker)
266272
requests_channel = Broadcast[Request](name="power_distributor requests")
267273
results_channel = Broadcast[Result](name="power_distributor results")
268274

@@ -305,7 +311,7 @@ async def test_power_distributor_exclusion_bounds(
305311
self, mocker: MockerFixture
306312
) -> None:
307313
"""Test if power distributing actor rejects non-zero requests in exclusion bounds."""
308-
async with _mocks(mocker) as mocks:
314+
async with _mocks(mocker, ComponentCategory.BATTERY) as mocks:
309315
await self._patch_battery_pool_status(mocks, mocker, {9, 19})
310316
await self.init_component_data(mocks, skip_batteries={9, 19})
311317

@@ -416,7 +422,7 @@ async def test_two_batteries_one_inverters(self, mocker: MockerFixture) -> None:
416422
)
417423
)
418424

419-
async with _mocks(mocker, graph=graph) as mocks:
425+
async with _mocks(mocker, ComponentCategory.BATTERY, graph=graph) as mocks:
420426
await self.init_component_data(mocks)
421427

422428
requests_channel = Broadcast[Request](name="power_distributor requests")
@@ -485,7 +491,7 @@ async def test_two_batteries_one_broken_one_inverters(
485491
)
486492
)
487493

488-
async with _mocks(mocker, graph=graph) as mocks:
494+
async with _mocks(mocker, ComponentCategory.BATTERY, graph=graph) as mocks:
489495
await self.init_component_data(
490496
mocks, skip_batteries={bat_components[0].component_id}
491497
)
@@ -551,7 +557,7 @@ async def test_battery_two_inverters(self, mocker: MockerFixture) -> None:
551557
)
552558
)
553559

554-
async with _mocks(mocker, graph=graph) as mocks:
560+
async with _mocks(mocker, ComponentCategory.BATTERY, graph=graph) as mocks:
555561
await self.init_component_data(mocks)
556562

557563
requests_channel = Broadcast[Request](name="power_distributor requests")
@@ -601,7 +607,7 @@ async def test_two_batteries_three_inverters(self, mocker: MockerFixture) -> Non
601607
(ComponentCategory.METER, gen.batteries_with_inverter(batteries, 3))
602608
)
603609

604-
async with _mocks(mocker, graph=graph) as mocks:
610+
async with _mocks(mocker, ComponentCategory.BATTERY, graph=graph) as mocks:
605611
await self.init_component_data(mocks)
606612

607613
requests_channel = Broadcast[Request](name="power_distributor requests")
@@ -661,7 +667,7 @@ async def test_two_batteries_one_inverter_different_exclusion_bounds_2(
661667
)
662668
)
663669

664-
async with _mocks(mocker, graph=graph) as mocks:
670+
async with _mocks(mocker, ComponentCategory.BATTERY, graph=graph) as mocks:
665671
mocks.streamer.start_streaming(
666672
inverter_msg(
667673
inverter.component_id,
@@ -750,7 +756,7 @@ async def test_two_batteries_one_inverter_different_exclusion_bounds(
750756
)
751757
)
752758

753-
async with _mocks(mocker, graph=graph) as mocks:
759+
async with _mocks(mocker, ComponentCategory.BATTERY, graph=graph) as mocks:
754760
await self.init_component_data(
755761
mocks, skip_batteries={bat.component_id for bat in batteries}
756762
)
@@ -834,7 +840,7 @@ async def test_connected_but_not_requested_batteries(
834840
)
835841
)
836842

837-
async with _mocks(mocker, graph=graph) as mocks:
843+
async with _mocks(mocker, ComponentCategory.BATTERY, graph=graph) as mocks:
838844
await self.init_component_data(mocks)
839845

840846
requests_channel = Broadcast[Request](name="power_distributor requests")
@@ -881,7 +887,7 @@ async def test_connected_but_not_requested_batteries(
881887

882888
async def test_battery_soc_nan(self, mocker: MockerFixture) -> None:
883889
"""Test if battery with SoC==NaN is not used."""
884-
async with _mocks(mocker, grid_meter=False) as mocks:
890+
async with _mocks(mocker, ComponentCategory.BATTERY, grid_meter=False) as mocks:
885891
await self.init_component_data(mocks, skip_batteries={9})
886892

887893
mocks.streamer.start_streaming(
@@ -933,7 +939,7 @@ async def test_battery_soc_nan(self, mocker: MockerFixture) -> None:
933939

934940
async def test_battery_capacity_nan(self, mocker: MockerFixture) -> None:
935941
"""Test battery with capacity set to NaN is not used."""
936-
async with _mocks(mocker, grid_meter=False) as mocks:
942+
async with _mocks(mocker, ComponentCategory.BATTERY, grid_meter=False) as mocks:
937943
await self.init_component_data(mocks, skip_batteries={9})
938944

939945
mocks.streamer.start_streaming(
@@ -986,7 +992,7 @@ async def test_battery_capacity_nan(self, mocker: MockerFixture) -> None:
986992

987993
async def test_battery_power_bounds_nan(self, mocker: MockerFixture) -> None:
988994
"""Test battery with power bounds set to NaN is not used."""
989-
async with _mocks(mocker, grid_meter=False) as mocks:
995+
async with _mocks(mocker, ComponentCategory.BATTERY, grid_meter=False) as mocks:
990996
await self.init_component_data(
991997
mocks, skip_batteries={9}, skip_inverters={8, 18}
992998
)
@@ -1060,7 +1066,7 @@ async def test_power_distributor_invalid_battery_id(
10601066
self, mocker: MockerFixture
10611067
) -> None:
10621068
"""Test if power distribution raises error if any battery id is invalid."""
1063-
async with _mocks(mocker, grid_meter=False) as mocks:
1069+
async with _mocks(mocker, ComponentCategory.BATTERY, grid_meter=False) as mocks:
10641070
await self.init_component_data(mocks)
10651071

10661072
requests_channel = Broadcast[Request](name="power_distributor requests")
@@ -1101,7 +1107,7 @@ async def test_power_distributor_one_user_adjust_power_consume(
11011107
self, mocker: MockerFixture
11021108
) -> None:
11031109
"""Test if power distribution works with single user works."""
1104-
async with _mocks(mocker, grid_meter=False) as mocks:
1110+
async with _mocks(mocker, ComponentCategory.BATTERY, grid_meter=False) as mocks:
11051111
await self.init_component_data(mocks)
11061112

11071113
requests_channel = Broadcast[Request](name="power_distributor")
@@ -1146,7 +1152,7 @@ async def test_power_distributor_one_user_adjust_power_supply(
11461152
self, mocker: MockerFixture
11471153
) -> None:
11481154
"""Test if power distribution works with single user works."""
1149-
async with _mocks(mocker, grid_meter=False) as mocks:
1155+
async with _mocks(mocker, ComponentCategory.BATTERY, grid_meter=False) as mocks:
11501156
await self.init_component_data(mocks)
11511157

11521158
requests_channel = Broadcast[Request](name="power_distributor requests")
@@ -1191,7 +1197,7 @@ async def test_power_distributor_one_user_adjust_power_success(
11911197
self, mocker: MockerFixture
11921198
) -> None:
11931199
"""Test if power distribution works with single user works."""
1194-
async with _mocks(mocker, grid_meter=False) as mocks:
1200+
async with _mocks(mocker, ComponentCategory.BATTERY, grid_meter=False) as mocks:
11951201
await self.init_component_data(mocks)
11961202

11971203
requests_channel = Broadcast[Request](name="power_distributor requests")
@@ -1234,7 +1240,7 @@ async def test_power_distributor_one_user_adjust_power_success(
12341240

12351241
async def test_not_all_batteries_are_working(self, mocker: MockerFixture) -> None:
12361242
"""Test if power distribution works if not all batteries are working."""
1237-
async with _mocks(mocker, grid_meter=False) as mocks:
1243+
async with _mocks(mocker, ComponentCategory.BATTERY, grid_meter=False) as mocks:
12381244
await self.init_component_data(mocks)
12391245

12401246
batteries = {9, 19}
@@ -1278,7 +1284,7 @@ async def test_not_all_batteries_are_working(self, mocker: MockerFixture) -> Non
12781284

12791285
async def test_partial_failure_result(self, mocker: MockerFixture) -> None:
12801286
"""Test power results when the microgrid failed to set power for one of the batteries."""
1281-
async with _mocks(mocker, grid_meter=False) as mocks:
1287+
async with _mocks(mocker, ComponentCategory.BATTERY, grid_meter=False) as mocks:
12821288
await self.init_component_data(mocks)
12831289

12841290
batteries = {9, 19, 29}

0 commit comments

Comments
 (0)