Skip to content

Commit 54c3066

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

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
@@ -26,12 +26,19 @@
2626
_MicrogridComponentGraph,
2727
)
2828
from frequenz.sdk.microgrid.connection_manager import ConnectionManager
29+
from frequenz.sdk.microgrid.metadata import Location
2930

3031

3132
class MockMicrogridClient:
3233
"""Class that mocks MicrogridClient behavior."""
3334

34-
def __init__(self, components: set[Component], connections: set[Connection]):
35+
def __init__(
36+
self,
37+
components: set[Component],
38+
connections: set[Connection],
39+
microgrid_id: int = 8,
40+
location: Location = Location(latitude=52.520008, longitude=13.404954),
41+
):
3542
"""Create mock microgrid with given components and connections.
3643
3744
This simulates microgrid.
@@ -43,6 +50,8 @@ def __init__(self, components: set[Component], connections: set[Connection]):
4350
Args:
4451
components: List of the microgrid components
4552
connections: List of the microgrid connections
53+
microgrid_id: the ID of the microgrid
54+
location: the location of the microgrid
4655
"""
4756
self._component_graph = _MicrogridComponentGraph(components, connections)
4857

@@ -66,6 +75,8 @@ def __init__(self, components: set[Component], connections: set[Connection]):
6675
kwargs: dict[str, Any] = {
6776
"api_client": mock_api,
6877
"component_graph": self._component_graph,
78+
"microgrid_id": microgrid_id,
79+
"location": location,
6980
}
7081

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

0 commit comments

Comments
 (0)