66import re
77from dataclasses import dataclass
88from typing import Set , Tuple , TypeVar
9- from unittest import mock
109from unittest .mock import AsyncMock , MagicMock
1110
1211from frequenz .channels import Bidirectional , Receiver , Sender
@@ -81,31 +80,30 @@ def component_graph(self) -> Tuple[Set[Component], Set[Connection]]:
8180 }
8281 return components , connections
8382
84- async def test_constructor (self ) -> None :
83+ async def test_constructor (self , mocker : MockerFixture ) -> None :
8584 """Test if gets all necessary data."""
8685 components , connections = self .component_graph ()
8786 mock_microgrid = MockMicrogridClient (components , connections )
87+ await mock_microgrid .initialize (mocker )
8888
89- with mock .patch (
90- "frequenz.sdk.microgrid.get" , return_value = mock_microgrid .mock_microgrid
91- ):
92- channel = Bidirectional [Request , Result ]("user1" , "power_distributor" )
93- distributor = PowerDistributingActor ({"user1" : channel .service_handle })
89+ channel = Bidirectional [Request , Result ]("user1" , "power_distributor" )
90+ distributor = PowerDistributingActor ({"user1" : channel .service_handle })
9491
95- assert distributor ._bat_inv_map == {106 : 105 , 206 : 205 , 306 : 305 }
96- assert distributor ._inv_bat_map == {105 : 106 , 205 : 206 , 305 : 306 }
97- await distributor ._stop_actor ()
92+ assert distributor ._bat_inv_map == {106 : 105 , 206 : 205 , 306 : 305 }
93+ assert distributor ._inv_bat_map == {105 : 106 , 205 : 206 , 305 : 306 }
94+ await distributor ._stop_actor ()
9895
99- async def init_mock_microgrid (self ) -> MockMicrogridClient :
96+ async def init_mock_microgrid (self , mocker : MockerFixture ) -> MockMicrogridClient :
10097 """Create mock microgrid and send initial data from the components.
10198
10299 Returns:
103100 Mock microgrid instance.
104101 """
105102 components , connections = self .component_graph ()
106103 microgrid = MockMicrogridClient (components , connections )
107- graph = microgrid .component_graph
104+ await microgrid .initialize ( mocker )
108105
106+ graph = microgrid .component_graph
109107 for battery in graph .components (component_category = {ComponentCategory .BATTERY }):
110108 assert await microgrid .send (
111109 battery_msg (
@@ -130,7 +128,7 @@ async def init_mock_microgrid(self) -> MockMicrogridClient:
130128 async def test_power_distributor_one_user (self , mocker : MockerFixture ) -> None :
131129 # pylint: disable=too-many-locals
132130 """Test if power distribution works with single user works."""
133- microgrid = await self .init_mock_microgrid ()
131+ await self .init_mock_microgrid (mocker )
134132
135133 channel = Bidirectional [Request , Result ]("user1" , "power_distributor" )
136134
@@ -141,10 +139,6 @@ async def test_power_distributor_one_user(self, mocker: MockerFixture) -> None:
141139 )
142140
143141 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
144- mocker .patch (
145- "frequenz.sdk.microgrid.get" , return_value = microgrid .mock_microgrid
146- )
147-
148142 distributor = PowerDistributingActor ({"user1" : channel .service_handle })
149143
150144 # Mock that all requested batteries are working.
@@ -172,7 +166,7 @@ async def test_power_distributor_one_user(self, mocker: MockerFixture) -> None:
172166 async def test_power_distributor_two_users (self , mocker : MockerFixture ) -> None :
173167 # pylint: disable=too-many-locals
174168 """Test if power distribution works with two users."""
175- microgrid = await self .init_mock_microgrid ()
169+ await self .init_mock_microgrid (mocker )
176170
177171 channel1 = Bidirectional [Request , Result ]("user1" , "power_distributor" )
178172 channel2 = Bidirectional [Request , Result ]("user2" , "power_distributor" )
@@ -182,9 +176,6 @@ async def test_power_distributor_two_users(self, mocker: MockerFixture) -> None:
182176 }
183177
184178 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
185- mocker .patch (
186- "frequenz.sdk.microgrid.get" , return_value = microgrid .mock_microgrid
187- )
188179
189180 distributor = PowerDistributingActor (service_channels )
190181
@@ -228,7 +219,7 @@ async def test_power_distributor_invalid_battery_id(
228219 ) -> None :
229220 # pylint: disable=too-many-locals
230221 """Test if power distribution raises error if any battery id is invalid."""
231- microgrid = await self .init_mock_microgrid ()
222+ await self .init_mock_microgrid (mocker )
232223
233224 channel1 = Bidirectional [Request , Result ]("user1" , "power_distributor" )
234225 service_channels = {
@@ -240,9 +231,6 @@ async def test_power_distributor_invalid_battery_id(
240231 )
241232
242233 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
243- mocker .patch (
244- "frequenz.sdk.microgrid.get" , return_value = microgrid .mock_microgrid
245- )
246234
247235 distributor = PowerDistributingActor (service_channels )
248236
@@ -271,7 +259,7 @@ async def test_power_distributor_overlapping_batteries(
271259 ) -> None :
272260 # pylint: disable=too-many-locals
273261 """Test if requests with overlapping set of batteries are processed."""
274- microgrid = await self .init_mock_microgrid ()
262+ await self .init_mock_microgrid (mocker )
275263
276264 channel1 = Bidirectional [Request , Result ]("user1" , "power_distributor" )
277265 channel2 = Bidirectional [Request , Result ]("user2" , "power_distributor" )
@@ -283,9 +271,6 @@ async def test_power_distributor_overlapping_batteries(
283271 }
284272
285273 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
286- mocker .patch (
287- "frequenz.sdk.microgrid.get" , return_value = microgrid .mock_microgrid
288- )
289274
290275 distributor = PowerDistributingActor (service_channels )
291276
@@ -350,7 +335,7 @@ async def test_power_distributor_one_user_adjust_power_consume(
350335 ) -> None :
351336 # pylint: disable=too-many-locals
352337 """Test if power distribution works with single user works."""
353- microgrid = await self .init_mock_microgrid ()
338+ await self .init_mock_microgrid (mocker )
354339
355340 channel1 = Bidirectional [Request , Result ]("user1" , "power_distributor" )
356341 service_channels = {
@@ -365,9 +350,6 @@ async def test_power_distributor_one_user_adjust_power_consume(
365350 )
366351
367352 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
368- mocker .patch (
369- "frequenz.sdk.microgrid.get" , return_value = microgrid .mock_microgrid
370- )
371353
372354 distributor = PowerDistributingActor (service_channels )
373355
@@ -398,7 +380,7 @@ async def test_power_distributor_one_user_adjust_power_supply(
398380 ) -> None :
399381 # pylint: disable=too-many-locals
400382 """Test if power distribution works with single user works."""
401- microgrid = await self .init_mock_microgrid ()
383+ await self .init_mock_microgrid (mocker )
402384
403385 channel1 = Bidirectional [Request , Result ]("user1" , "power_distributor" )
404386 service_channels = {
@@ -413,9 +395,6 @@ async def test_power_distributor_one_user_adjust_power_supply(
413395 )
414396
415397 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
416- mocker .patch (
417- "frequenz.sdk.microgrid.get" , return_value = microgrid .mock_microgrid
418- )
419398
420399 distributor = PowerDistributingActor (service_channels )
421400
@@ -446,7 +425,7 @@ async def test_power_distributor_one_user_adjust_power_success(
446425 ) -> None :
447426 # pylint: disable=too-many-locals
448427 """Test if power distribution works with single user works."""
449- microgrid = await self .init_mock_microgrid ()
428+ await self .init_mock_microgrid (mocker )
450429
451430 channel1 = Bidirectional [Request , Result ]("user1" , "power_distributor" )
452431 service_channels = {
@@ -461,9 +440,6 @@ async def test_power_distributor_one_user_adjust_power_success(
461440 )
462441
463442 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
464- mocker .patch (
465- "frequenz.sdk.microgrid.get" , return_value = microgrid .mock_microgrid
466- )
467443
468444 distributor = PowerDistributingActor (service_channels )
469445
@@ -492,7 +468,7 @@ async def test_power_distributor_one_user_adjust_power_success(
492468 async def test_not_all_batteries_are_working (self , mocker : MockerFixture ) -> None :
493469 # pylint: disable=too-many-locals
494470 """Test if power distribution works if not all batteries are working."""
495- microgrid = await self .init_mock_microgrid ()
471+ await self .init_mock_microgrid (mocker )
496472
497473 channel = Bidirectional [Request , Result ]("user1" , "power_distributor" )
498474
@@ -501,9 +477,6 @@ async def test_not_all_batteries_are_working(self, mocker: MockerFixture) -> Non
501477 )
502478
503479 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
504- mocker .patch (
505- "frequenz.sdk.microgrid.get" , return_value = microgrid .mock_microgrid
506- )
507480
508481 distributor = PowerDistributingActor ({"user1" : channel .service_handle })
509482
0 commit comments