|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2025 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Tests for the component graph generator.""" |
| 5 | + |
| 6 | +from unittest.mock import AsyncMock, MagicMock |
| 7 | + |
| 8 | +from frequenz.client.assets import AssetsApiClient |
| 9 | +from frequenz.client.assets.electrical_component import ( |
| 10 | + ComponentConnection, |
| 11 | + GridConnectionPoint, |
| 12 | + Meter, |
| 13 | + SolarInverter, |
| 14 | +) |
| 15 | +from frequenz.client.common.microgrid import MicrogridId |
| 16 | +from frequenz.client.common.microgrid.electrical_components import ElectricalComponentId |
| 17 | + |
| 18 | +from frequenz.gridpool._graph_generator import ComponentGraphGenerator |
| 19 | + |
| 20 | + |
| 21 | +async def test_formula_generation() -> None: |
| 22 | + """Test formula generation from component graph created from Assets API.""" |
| 23 | + assets_client_mock = MagicMock(spec=AssetsApiClient) |
| 24 | + assets_client_mock.list_microgrid_electrical_components = AsyncMock( |
| 25 | + return_value=[ |
| 26 | + GridConnectionPoint( |
| 27 | + id=ElectricalComponentId(1), |
| 28 | + microgrid_id=MicrogridId(10), |
| 29 | + rated_fuse_current=100, |
| 30 | + ), |
| 31 | + Meter( |
| 32 | + id=ElectricalComponentId(2), |
| 33 | + microgrid_id=MicrogridId(10), |
| 34 | + ), |
| 35 | + Meter( |
| 36 | + id=ElectricalComponentId(3), |
| 37 | + microgrid_id=MicrogridId(10), |
| 38 | + ), |
| 39 | + SolarInverter( |
| 40 | + id=ElectricalComponentId(4), |
| 41 | + microgrid_id=MicrogridId(10), |
| 42 | + ), |
| 43 | + ] |
| 44 | + ) |
| 45 | + assets_client_mock.list_microgrid_electrical_component_connections = AsyncMock( |
| 46 | + return_value=[ |
| 47 | + ComponentConnection( |
| 48 | + source=ElectricalComponentId(1), |
| 49 | + destination=ElectricalComponentId(2), |
| 50 | + ), |
| 51 | + ComponentConnection( |
| 52 | + source=ElectricalComponentId(1), |
| 53 | + destination=ElectricalComponentId(3), |
| 54 | + ), |
| 55 | + ComponentConnection( |
| 56 | + source=ElectricalComponentId(2), |
| 57 | + destination=ElectricalComponentId(4), |
| 58 | + ), |
| 59 | + ] |
| 60 | + ) |
| 61 | + |
| 62 | + g = ComponentGraphGenerator(assets_client_mock) |
| 63 | + graph = await g.get_component_graph(MicrogridId(10)) |
| 64 | + |
| 65 | + assert graph.grid_formula() == "COALESCE(#2, #4, 0.0) + #3" |
| 66 | + assert graph.pv_formula(None) == "COALESCE(#4, #2, 0.0)" |
0 commit comments