Skip to content

Commit 0a0c44b

Browse files
Fix Battery Pool tests
Signed-off-by: Matthias Wende <[email protected]>
1 parent f2b2af1 commit 0a0c44b

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

tests/timeseries/_battery_pool/test_battery_pool.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import async_solipsism
1919
import pytest
20+
import time_machine
2021
from frequenz.channels import Receiver, Sender
2122
from pytest_mock import MockerFixture
2223

@@ -296,22 +297,28 @@ async def run_scenarios(
296297
raise err
297298

298299

299-
async def test_all_batteries_capacity(setup_all_batteries: SetupArgs) -> None:
300+
async def test_all_batteries_capacity(
301+
fake_time: time_machine.Coordinates, setup_all_batteries: SetupArgs
302+
) -> None:
300303
"""Test capacity metric for battery pool with all components in the microgrid.
301304
302305
Args:
306+
fake_time: The time machine fake time argument.
303307
setup_all_batteries: Fixture that creates needed microgrid tools.
304308
"""
305-
await run_capacity_test(setup_all_batteries)
309+
await run_capacity_test(fake_time, setup_all_batteries)
306310

307311

308-
async def test_battery_pool_capacity(setup_batteries_pool: SetupArgs) -> None:
312+
async def test_battery_pool_capacity(
313+
fake_time: time_machine.Coordinates, setup_batteries_pool: SetupArgs
314+
) -> None:
309315
"""Test capacity metric for battery pool with subset of components in the microgrid.
310316
311317
Args:
318+
fake_time: The time machine fake time argument.
312319
setup_batteries_pool: Fixture that creates needed microgrid tools.
313320
"""
314-
await run_capacity_test(setup_batteries_pool)
321+
await run_capacity_test(fake_time, setup_batteries_pool)
315322

316323

317324
async def test_all_batteries_soc(setup_all_batteries: SetupArgs) -> None:
@@ -499,10 +506,13 @@ async def test_battery_pool_power(mocker: MockerFixture) -> None:
499506
await mockgrid.cleanup()
500507

501508

502-
async def run_capacity_test(setup_args: SetupArgs) -> None:
509+
async def run_capacity_test( # pylint: disable=too-many-locals
510+
fake_time: time_machine.Coordinates, setup_args: SetupArgs
511+
) -> None:
503512
"""Test if capacity metric is working as expected.
504513
505514
Args:
515+
fake_time: The time machine fake time argument.
506516
setup_args: Needed sdk tools and tools for mocking microgrid.
507517
"""
508518
battery_pool = setup_args.battery_pool
@@ -666,20 +676,30 @@ async def run_capacity_test(setup_args: SetupArgs) -> None:
666676
# One battery stopped sending data.
667677
await streamer.stop_streaming(batteries_in_pool[1])
668678
await asyncio.sleep(MAX_BATTERY_DATA_AGE_SEC + 0.2)
679+
fake_time.shift(MAX_BATTERY_DATA_AGE_SEC + 0.2)
669680
msg = await asyncio.wait_for(capacity_receiver.receive(), timeout=waiting_time_sec)
670-
compare_messages(msg, Sample(now, Energy.from_watt_hours(21.0)), 0.2)
681+
# the msg time difference shouldn't be bigger then the shifted time + 0.2 sec tolerance
682+
compare_messages(
683+
msg, Sample(now, Energy.from_watt_hours(21.0)), MAX_BATTERY_DATA_AGE_SEC + 0.4
684+
)
671685

672686
# All batteries stopped sending data.
673687
await streamer.stop_streaming(batteries_in_pool[0])
674688
await asyncio.sleep(MAX_BATTERY_DATA_AGE_SEC + 0.2)
689+
fake_time.shift(MAX_BATTERY_DATA_AGE_SEC + 0.2)
675690
msg = await asyncio.wait_for(capacity_receiver.receive(), timeout=waiting_time_sec)
676691
assert isinstance(msg, Sample) and msg.value is None
677692

678693
# One battery started sending data.
679694
latest_data = streamer.get_current_component_data(batteries_in_pool[0])
680695
streamer.start_streaming(latest_data, sampling_rate=0.1)
681696
msg = await asyncio.wait_for(capacity_receiver.receive(), timeout=waiting_time_sec)
682-
compare_messages(msg, Sample(now, Energy.from_watt_hours(21.0)), 0.2)
697+
# the msg time difference shouldn't be bigger then the shifted time + 0.2 sec tolerance
698+
compare_messages(
699+
msg,
700+
Sample(datetime.now(tz=timezone.utc), Energy.from_watt_hours(21.0)),
701+
MAX_BATTERY_DATA_AGE_SEC + 0.4,
702+
)
683703

684704

685705
async def run_soc_test(setup_args: SetupArgs) -> None:

0 commit comments

Comments
 (0)