Skip to content

Commit 0bbd7e0

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

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,11 @@ 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 and api.location.timezone
160+
assert api.location.timezone.key == "Europe/Berlin"
161+
140162
# It should not be possible to initialize method once again
141163
with pytest.raises(AssertionError):
142164
await connection_manager.initialize("127.0.0.1", 10001)
@@ -148,25 +170,36 @@ async def test_connection_manager(
148170
assert set(graph.components()) == set(components[0])
149171
assert set(graph.connections()) == set(connections[0])
150172

173+
assert api.microgrid_id == metadata.microgrid_id
174+
assert api.location == metadata.location
175+
151176
@mock.patch("grpc.aio.insecure_channel")
152177
async def test_connection_manager_another_method(
153178
self,
154179
_: MagicMock,
155180
components: list[list[Component]],
156181
connections: list[list[Connection]],
182+
metadata: Metadata,
157183
) -> None:
158184
"""Test if the api was not deallocated.
159185
160186
Args:
161187
_: insecure channel mock
162188
components: components
163189
connections: connections
190+
metadata: the metadata of the microgrid
164191
"""
165192
microgrid_client = MagicMock()
166193
microgrid_client.components = AsyncMock(return_value=[])
167194
microgrid_client.connections = AsyncMock(return_value=[])
195+
microgrid_client.get_metadata = AsyncMock(return_value=None)
168196

169197
api = connection_manager.get()
170198
graph = api.component_graph
171199
assert set(graph.components()) == set(components[0])
172200
assert set(graph.connections()) == set(connections[0])
201+
202+
assert api.microgrid_id == metadata.microgrid_id
203+
assert api.location == metadata.location
204+
assert api.location and api.location.timezone
205+
assert 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)