Skip to content

Commit dd9a4da

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

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

tests/microgrid/test_microgrid_api.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pytest
1212

1313
from frequenz.sdk.microgrid import connection_manager
14+
from frequenz.sdk.microgrid import metadata as meta
1415
from frequenz.sdk.microgrid.client import Connection
1516
from frequenz.sdk.microgrid.component import Component, ComponentCategory
1617

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

86+
@pytest.fixture
87+
def metadata(self) -> meta.Metadata:
88+
"""Fetch the microgrid metadata.
89+
90+
Returns:
91+
the microgrid metadata.
92+
"""
93+
mock_timezone_finder = MagicMock()
94+
mock_timezone_finder.timezone_at.return_value = "Europe/Berlin"
95+
meta._timezone_finder = mock_timezone_finder # pylint: disable=protected-access
96+
97+
return meta.Metadata(
98+
microgrid_id=8,
99+
location=meta.Location(latitude=52.520008, longitude=13.404954),
100+
)
101+
85102
@mock.patch("grpc.aio.insecure_channel")
86103
async def test_connection_manager(
87104
self,
88105
_: MagicMock,
89106
components: list[list[Component]],
90107
connections: list[list[Connection]],
108+
metadata: meta.Metadata,
91109
) -> None:
92110
"""Test microgrid api.
93111
94112
Args:
95113
_: insecure channel mock from `mock.patch`
96114
components: components
97115
connections: connections
116+
metadata: the metadata of the microgrid
98117
"""
99118
microgrid_client = MagicMock()
100119
microgrid_client.components = AsyncMock(side_effect=components)
101120
microgrid_client.connections = AsyncMock(side_effect=connections)
121+
microgrid_client.metadata = AsyncMock(return_value=metadata)
102122

103123
with mock.patch(
104124
"frequenz.sdk.microgrid.connection_manager.MicrogridGrpcClient",
@@ -137,6 +157,11 @@ async def test_connection_manager(
137157
assert set(graph.components()) == set(components[0])
138158
assert set(graph.connections()) == set(connections[0])
139159

160+
assert api.microgrid_id == metadata.microgrid_id
161+
assert api.location == metadata.location
162+
assert api.location and api.location.timezone
163+
assert api.location.timezone.key == "Europe/Berlin"
164+
140165
# It should not be possible to initialize method once again
141166
with pytest.raises(AssertionError):
142167
await connection_manager.initialize("127.0.0.1", 10001)
@@ -148,25 +173,36 @@ async def test_connection_manager(
148173
assert set(graph.components()) == set(components[0])
149174
assert set(graph.connections()) == set(connections[0])
150175

176+
assert api.microgrid_id == metadata.microgrid_id
177+
assert api.location == metadata.location
178+
151179
@mock.patch("grpc.aio.insecure_channel")
152180
async def test_connection_manager_another_method(
153181
self,
154182
_: MagicMock,
155183
components: list[list[Component]],
156184
connections: list[list[Connection]],
185+
metadata: meta.Metadata,
157186
) -> None:
158187
"""Test if the api was not deallocated.
159188
160189
Args:
161190
_: insecure channel mock
162191
components: components
163192
connections: connections
193+
metadata: the metadata of the microgrid
164194
"""
165195
microgrid_client = MagicMock()
166196
microgrid_client.components = AsyncMock(return_value=[])
167197
microgrid_client.connections = AsyncMock(return_value=[])
198+
microgrid_client.get_metadata = AsyncMock(return_value=None)
168199

169200
api = connection_manager.get()
170201
graph = api.component_graph
171202
assert set(graph.components()) == set(components[0])
172203
assert set(graph.connections()) == set(connections[0])
204+
205+
assert api.microgrid_id == metadata.microgrid_id
206+
assert api.location == metadata.location
207+
assert api.location and api.location.timezone
208+
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)