Skip to content

Commit be5ea72

Browse files
Amend tests to reflect microgrid metadata changes
Signed-off-by: Daniel Zullo <[email protected]>
1 parent 355d7f8 commit be5ea72

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

tests/microgrid/test_microgrid_api.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from frequenz.sdk.microgrid import connection_manager
1414
from frequenz.sdk.microgrid.client import Connection
1515
from frequenz.sdk.microgrid.component import Component, ComponentCategory
16+
from frequenz.sdk.microgrid.metadata import Location, Metadata
1617

1718

1819
class TestMicrogridApi:
@@ -82,23 +83,39 @@ def connections(self) -> list[list[Connection]]:
8283
]
8384
return connections
8485

86+
# ignore mypy: Untyped decorator makes function "components" untyped
87+
@pytest.fixture
88+
def metadata(self) -> Metadata:
89+
"""Fetch the microgrid metadata.
90+
91+
Returns:
92+
the microgrid metadata.
93+
"""
94+
return Metadata(
95+
microgrid_id=8,
96+
location=Location(latitude=52.520008, longitude=13.404954),
97+
)
98+
8599
@mock.patch("grpc.aio.insecure_channel")
86100
async def test_connection_manager(
87101
self,
88102
_: MagicMock,
89103
components: list[list[Component]],
90104
connections: list[list[Connection]],
105+
metadata: Metadata,
91106
) -> None:
92107
"""Test microgrid api.
93108
94109
Args:
95110
_: insecure channel mock from `mock.patch`
96111
components: components
97112
connections: connections
113+
metadata: the metadata of the microgrid
98114
"""
99115
microgrid_client = MagicMock()
100116
microgrid_client.components = AsyncMock(side_effect=components)
101117
microgrid_client.connections = AsyncMock(side_effect=connections)
118+
microgrid_client.metadata = AsyncMock(return_value=metadata)
102119

103120
with mock.patch(
104121
"frequenz.sdk.microgrid.connection_manager.MicrogridGrpcClient",
@@ -137,6 +154,10 @@ async def test_connection_manager(
137154
assert set(graph.components()) == set(components[0])
138155
assert set(graph.connections()) == set(connections[0])
139156

157+
assert api.microgrid_id == metadata.microgrid_id
158+
assert api.location == metadata.location
159+
assert api.location.get_timezone() == "Europe/Berlin"
160+
140161
# It should not be possible to initialize method once again
141162
with pytest.raises(AssertionError):
142163
await connection_manager.initialize("127.0.0.1", 10001)
@@ -148,25 +169,35 @@ async def test_connection_manager(
148169
assert set(graph.components()) == set(components[0])
149170
assert set(graph.connections()) == set(connections[0])
150171

172+
assert api.microgrid_id == metadata.microgrid_id
173+
assert api.location == metadata.location
174+
151175
@mock.patch("grpc.aio.insecure_channel")
152176
async def test_connection_manager_another_method(
153177
self,
154178
_: MagicMock,
155179
components: list[list[Component]],
156180
connections: list[list[Connection]],
181+
metadata: Metadata,
157182
) -> None:
158183
"""Test if the api was not deallocated.
159184
160185
Args:
161186
_: insecure channel mock
162187
components: components
163188
connections: connections
189+
metadata: the metadata of the microgrid
164190
"""
165191
microgrid_client = MagicMock()
166192
microgrid_client.components = AsyncMock(return_value=[])
167193
microgrid_client.connections = AsyncMock(return_value=[])
194+
microgrid_client.get_metadata = AsyncMock(return_value=None)
168195

169196
api = connection_manager.get()
170197
graph = api.component_graph
171198
assert set(graph.components()) == set(components[0])
172199
assert set(graph.connections()) == set(connections[0])
200+
201+
assert api.microgrid_id == metadata.microgrid_id
202+
assert api.location == metadata.location
203+
assert api.location.get_timezone() == "Europe/Berlin"

tests/utils/mock_microgrid_client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@
2323
MeterData,
2424
)
2525
from frequenz.sdk.microgrid.connection_manager import ConnectionManager
26+
from frequenz.sdk.microgrid.metadata import Location
2627

2728

2829
class MockMicrogridClient:
2930
"""Class that mocks MicrogridClient behavior."""
3031

31-
def __init__(self, components: set[Component], connections: set[Connection]):
32+
def __init__(
33+
self,
34+
components: set[Component],
35+
connections: set[Connection],
36+
microgrid_id: int = 8,
37+
location: Location = Location(latitude=52.520008, longitude=13.404954),
38+
):
3239
"""Create mock microgrid with given components and connections.
3340
3441
This simulates microgrid.
@@ -40,6 +47,8 @@ def __init__(self, components: set[Component], connections: set[Connection]):
4047
Args:
4148
components: List of the microgrid components
4249
connections: List of the microgrid connections
50+
microgrid_id: the ID of the microgrid
51+
location: the location of the microgrid
4352
"""
4453
self._component_graph = _MicrogridComponentGraph(components, connections)
4554

@@ -63,6 +72,8 @@ def __init__(self, components: set[Component], connections: set[Connection]):
6372
kwargs: dict[str, Any] = {
6473
"api_client": mock_api,
6574
"component_graph": self._component_graph,
75+
"microgrid_id": microgrid_id,
76+
"location": location,
6677
}
6778

6879
self._mock_microgrid = MagicMock(spec=ConnectionManager, **kwargs)

0 commit comments

Comments
 (0)