13
13
from frequenz .sdk .microgrid import connection_manager
14
14
from frequenz .sdk .microgrid .client import Connection
15
15
from frequenz .sdk .microgrid .component import Component , ComponentCategory
16
+ from frequenz .sdk .microgrid .metadata import Location , Metadata
16
17
17
18
18
19
class TestMicrogridApi :
@@ -82,23 +83,39 @@ def connections(self) -> list[list[Connection]]:
82
83
]
83
84
return connections
84
85
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
+
85
99
@mock .patch ("grpc.aio.insecure_channel" )
86
100
async def test_connection_manager (
87
101
self ,
88
102
_ : MagicMock ,
89
103
components : list [list [Component ]],
90
104
connections : list [list [Connection ]],
105
+ metadata : Metadata ,
91
106
) -> None :
92
107
"""Test microgrid api.
93
108
94
109
Args:
95
110
_: insecure channel mock from `mock.patch`
96
111
components: components
97
112
connections: connections
113
+ metadata: the metadata of the microgrid
98
114
"""
99
115
microgrid_client = MagicMock ()
100
116
microgrid_client .components = AsyncMock (side_effect = components )
101
117
microgrid_client .connections = AsyncMock (side_effect = connections )
118
+ microgrid_client .metadata = AsyncMock (return_value = metadata )
102
119
103
120
with mock .patch (
104
121
"frequenz.sdk.microgrid.connection_manager.MicrogridGrpcClient" ,
@@ -137,6 +154,11 @@ async def test_connection_manager(
137
154
assert set (graph .components ()) == set (components [0 ])
138
155
assert set (graph .connections ()) == set (connections [0 ])
139
156
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
+
140
162
# It should not be possible to initialize method once again
141
163
with pytest .raises (AssertionError ):
142
164
await connection_manager .initialize ("127.0.0.1" , 10001 )
@@ -148,25 +170,36 @@ async def test_connection_manager(
148
170
assert set (graph .components ()) == set (components [0 ])
149
171
assert set (graph .connections ()) == set (connections [0 ])
150
172
173
+ assert api .microgrid_id == metadata .microgrid_id
174
+ assert api .location == metadata .location
175
+
151
176
@mock .patch ("grpc.aio.insecure_channel" )
152
177
async def test_connection_manager_another_method (
153
178
self ,
154
179
_ : MagicMock ,
155
180
components : list [list [Component ]],
156
181
connections : list [list [Connection ]],
182
+ metadata : Metadata ,
157
183
) -> None :
158
184
"""Test if the api was not deallocated.
159
185
160
186
Args:
161
187
_: insecure channel mock
162
188
components: components
163
189
connections: connections
190
+ metadata: the metadata of the microgrid
164
191
"""
165
192
microgrid_client = MagicMock ()
166
193
microgrid_client .components = AsyncMock (return_value = [])
167
194
microgrid_client .connections = AsyncMock (return_value = [])
195
+ microgrid_client .get_metadata = AsyncMock (return_value = None )
168
196
169
197
api = connection_manager .get ()
170
198
graph = api .component_graph
171
199
assert set (graph .components ()) == set (components [0 ])
172
200
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"
0 commit comments