Skip to content

Commit b16d5b8

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

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

tests/microgrid/test_microgrid_api.py

Lines changed: 33 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,12 @@ 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 (
160+
api.location.timezone and api.location.timezone.key == "Europe/Berlin"
161+
)
162+
140163
# It should not be possible to initialize method once again
141164
with pytest.raises(AssertionError):
142165
await connection_manager.initialize("127.0.0.1", 10001)
@@ -148,25 +171,35 @@ async def test_connection_manager(
148171
assert set(graph.components()) == set(components[0])
149172
assert set(graph.connections()) == set(connections[0])
150173

174+
assert api.microgrid_id == metadata.microgrid_id
175+
assert api.location == metadata.location
176+
151177
@mock.patch("grpc.aio.insecure_channel")
152178
async def test_connection_manager_another_method(
153179
self,
154180
_: MagicMock,
155181
components: list[list[Component]],
156182
connections: list[list[Connection]],
183+
metadata: Metadata,
157184
) -> None:
158185
"""Test if the api was not deallocated.
159186
160187
Args:
161188
_: insecure channel mock
162189
components: components
163190
connections: connections
191+
metadata: the metadata of the microgrid
164192
"""
165193
microgrid_client = MagicMock()
166194
microgrid_client.components = AsyncMock(return_value=[])
167195
microgrid_client.connections = AsyncMock(return_value=[])
196+
microgrid_client.get_metadata = AsyncMock(return_value=None)
168197

169198
api = connection_manager.get()
170199
graph = api.component_graph
171200
assert set(graph.components()) == set(components[0])
172201
assert set(graph.connections()) == set(connections[0])
202+
203+
assert api.microgrid_id == metadata.microgrid_id
204+
assert api.location == metadata.location
205+
assert api.location.timezone and api.location.timezone.key == "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)