Skip to content

Commit

Permalink
Add some tests for scenarios with no meter.
Browse files Browse the repository at this point in the history
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
  • Loading branch information
Marenz committed Aug 21, 2023
1 parent 2e6ca85 commit 0f8063f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/timeseries/mock_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ async def send_meter_power(self, values: list[float | None]) -> None:
sample = Sample(self._next_ts, None if not value else Quantity(value))
await chan.send(sample)

async def send_chp_power(self, values: list[float | None]) -> None:
"""Send the given values as resampler output for CHP power."""
assert len(values) == len(self._chp_power_senders)
for chan, value in zip(self._chp_power_senders, values):
sample = Sample(self._next_ts, None if not value else Quantity(value))
await chan.send(sample)

async def send_pv_inverter_power(self, values: list[float | None]) -> None:
"""Send the given values as resampler output for PV Inverter power."""
assert len(values) == len(self._pv_inverter_power_senders)
Expand Down
42 changes: 42 additions & 0 deletions tests/timeseries/test_logical_meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ async def test_grid_production_consumption_power_consumer_meter(
assert (await grid_production_recv.receive()).value == Power.from_watts(4.0)
assert (await grid_consumption_recv.receive()).value == Power.from_watts(0.0)

async def test_grid_production_consumption_power_no_grid_meter(
self,
mocker: MockerFixture,
) -> None:
"""Test the grid production and consumption power formulas."""
mockgrid = MockMicrogrid(grid_meter=False)
mockgrid.add_batteries(2)
mockgrid.add_solar_inverters(1)
await mockgrid.start(mocker)

logical_meter = microgrid.logical_meter()
grid_recv = logical_meter.grid_power.new_receiver()
grid_production_recv = logical_meter.grid_production_power.new_receiver()
grid_consumption_recv = logical_meter.grid_consumption_power.new_receiver()

await mockgrid.mock_resampler.send_meter_power([2.5, 3.5, 4.0])
assert (await grid_recv.receive()).value == Power.from_watts(10.0)
assert (await grid_production_recv.receive()).value == Power.from_watts(0.0)
assert (await grid_consumption_recv.receive()).value == Power.from_watts(10.0)

await mockgrid.mock_resampler.send_meter_power([3.0, -3.0, -4.0])
assert (await grid_recv.receive()).value == Power.from_watts(-4.0)
assert (await grid_production_recv.receive()).value == Power.from_watts(4.0)
assert (await grid_consumption_recv.receive()).value == Power.from_watts(0.0)

async def test_chp_power(self, mocker: MockerFixture) -> None:
"""Test the chp power formula."""
mockgrid = MockMicrogrid(grid_meter=False)
Expand Down Expand Up @@ -268,6 +293,23 @@ async def test_producer_power_no_chp(self, mocker: MockerFixture) -> None:
await mockgrid.mock_resampler.send_meter_power([2.0, 3.0])
assert (await producer_power_receiver.receive()).value == Power.from_watts(5.0)

async def test_producer_power_no_pv_no_consumer_meter(
self, mocker: MockerFixture
) -> None:
"""Test the producer power formula without pv and without consumer meter."""
mockgrid = MockMicrogrid(grid_meter=False)
mockgrid.add_chps(1)
await mockgrid.start(mocker)

logical_meter = microgrid.logical_meter()
producer_power_receiver = logical_meter.producer_power.new_receiver()

# As there is only one meter in the microgrid, the formula interprets it
# as main meter instead of chp meter, so it reads the power from the
# chp component directly.
await mockgrid.mock_resampler.send_chp_power([2.0])
assert (await producer_power_receiver.receive()).value == Power.from_watts(2.0)

async def test_producer_power_no_pv(self, mocker: MockerFixture) -> None:
"""Test the producer power formula without pv."""
mockgrid = MockMicrogrid(grid_meter=False)
Expand Down

0 comments on commit 0f8063f

Please sign in to comment.