From 0f8063f7fc4f99c3dbaf355e12c70a773b71fad2 Mon Sep 17 00:00:00 2001 From: "Mathias L. Baumann" Date: Fri, 18 Aug 2023 13:06:25 +0200 Subject: [PATCH] Add some tests for scenarios with no meter. Signed-off-by: Mathias L. Baumann --- tests/timeseries/mock_resampler.py | 7 +++++ tests/timeseries/test_logical_meter.py | 42 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/tests/timeseries/mock_resampler.py b/tests/timeseries/mock_resampler.py index b3832a56d..8caeafb02 100644 --- a/tests/timeseries/mock_resampler.py +++ b/tests/timeseries/mock_resampler.py @@ -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) diff --git a/tests/timeseries/test_logical_meter.py b/tests/timeseries/test_logical_meter.py index 691f9659c..4ef4ba0eb 100644 --- a/tests/timeseries/test_logical_meter.py +++ b/tests/timeseries/test_logical_meter.py @@ -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) @@ -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)