-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for 3-phase active power streaming
Signed-off-by: Daniel Zullo <daniel.zullo@frequenz.com>
- Loading branch information
1 parent
4681446
commit 16e2030
Showing
4 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# License: MIT | ||
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH | ||
|
||
"""Tests for fetching and streaming the 3-phase active power.""" | ||
|
||
|
||
import asyncio | ||
|
||
from pytest_mock import MockerFixture | ||
|
||
from frequenz.sdk import microgrid | ||
|
||
from .mock_microgrid import MockMicrogrid | ||
|
||
# pylint: disable=protected-access | ||
|
||
|
||
async def test_active_power_1(mocker: MockerFixture) -> None: | ||
"""Test the 3-phase active power with a grid side meter.""" | ||
mockgrid = MockMicrogrid(grid_meter=True, mocker=mocker) | ||
mockgrid.add_batteries(1, no_meter=True) | ||
mockgrid.add_batteries(1, no_meter=False) | ||
|
||
async with mockgrid: | ||
active_power = microgrid._active_power() | ||
active_power_recv = active_power.new_receiver() | ||
|
||
assert active_power._task is not None | ||
# Wait for active power requests to be sent, one request per phase. | ||
for _ in range(3): | ||
await asyncio.sleep(0) | ||
|
||
for count in range(10): | ||
watts_delta = 1 if count % 2 == 0 else -1 | ||
watts_phases: list[float | None] = [ | ||
220.0 * watts_delta, | ||
219.8 * watts_delta, | ||
220.2 * watts_delta, | ||
] | ||
|
||
await mockgrid.mock_resampler.send_meter_active_power( | ||
[watts_phases, watts_phases] | ||
) | ||
|
||
val = await active_power_recv.receive() | ||
assert val is not None | ||
assert val.value_p1 and val.value_p2 and val.value_p3 | ||
assert val.value_p1.as_watts() == watts_phases[0] | ||
assert val.value_p2.as_watts() == watts_phases[1] | ||
assert val.value_p3.as_watts() == watts_phases[2] | ||
|
||
|
||
async def test_active_power_2(mocker: MockerFixture) -> None: | ||
"""Test the 3-phase active power without a grid side meter.""" | ||
mockgrid = MockMicrogrid(grid_meter=False, mocker=mocker) | ||
mockgrid.add_batteries(1, no_meter=False) | ||
mockgrid.add_batteries(1, no_meter=True) | ||
|
||
async with mockgrid: | ||
active_power = microgrid._active_power() | ||
active_power_recv = active_power.new_receiver() | ||
|
||
assert active_power._task is not None | ||
# Wait for active power requests to be sent, one request per phase. | ||
for _ in range(3): | ||
await asyncio.sleep(0) | ||
|
||
for count in range(10): | ||
watts_delta = 1 if count % 2 == 0 else -1 | ||
watts_phases: list[float | None] = [ | ||
220.0 * watts_delta, | ||
219.8 * watts_delta, | ||
220.2 * watts_delta, | ||
] | ||
|
||
await mockgrid.mock_resampler.send_meter_active_power([watts_phases]) | ||
|
||
val = await active_power_recv.receive() | ||
assert val is not None | ||
assert val.value_p1 and val.value_p2 and val.value_p3 | ||
assert val.value_p1.as_watts() == watts_phases[0] | ||
assert val.value_p2.as_watts() == watts_phases[1] | ||
assert val.value_p3.as_watts() == watts_phases[2] | ||
|
||
|
||
async def test_active_power_3(mocker: MockerFixture) -> None: | ||
"""Test the 3-phase active power with None values.""" | ||
mockgrid = MockMicrogrid(grid_meter=True, mocker=mocker) | ||
mockgrid.add_batteries(2, no_meter=False) | ||
|
||
async with mockgrid: | ||
active_power = microgrid._active_power() | ||
active_power_recv = active_power.new_receiver() | ||
|
||
assert active_power._task is not None | ||
# Wait for active power requests to be sent, one request per phase. | ||
for _ in range(3): | ||
await asyncio.sleep(0) | ||
|
||
for count in range(10): | ||
watts_delta = 1 if count % 2 == 0 else -1 | ||
watts_phases: list[float | None] = [ | ||
220.0 * watts_delta, | ||
219.8 * watts_delta, | ||
220.2 * watts_delta, | ||
] | ||
|
||
await mockgrid.mock_resampler.send_meter_active_power( | ||
[watts_phases, [None, None, None], [None, 219.8, 220.2]] | ||
) | ||
|
||
val = await active_power_recv.receive() | ||
assert val is not None | ||
assert val.value_p1 and val.value_p2 and val.value_p3 | ||
assert val.value_p1.as_watts() == watts_phases[0] | ||
assert val.value_p2.as_watts() == watts_phases[1] | ||
assert val.value_p3.as_watts() == watts_phases[2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters