@@ -138,13 +138,15 @@ async def test_power_distributor_one_user(self, mocker: MockerFixture) -> None:
138138 request_timeout_sec = SAFETY_TIMEOUT ,
139139 )
140140
141+ attrs = {"get_working_batteries.return_value" : request .batteries }
142+ battery_pool_mock = MagicMock (spec = BatteryPoolStatus , ** attrs )
143+ BatteryPoolStatus .async_new = AsyncMock ( # type: ignore
144+ return_value = battery_pool_mock
145+ )
146+
141147 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
142148 distributor = PowerDistributingActor ({"user1" : channel .service_handle })
143149
144- # Mock that all requested batteries are working.
145- distributor ._battery_pool = MagicMock (spec = BatteryPoolStatus )
146- distributor ._battery_pool .get_working_batteries .return_value = request .batteries
147-
148150 client_handle = channel .client_handle
149151 await client_handle .send (request )
150152
@@ -175,14 +177,16 @@ async def test_power_distributor_two_users(self, mocker: MockerFixture) -> None:
175177 "user2" : channel2 .service_handle ,
176178 }
177179
180+ attrs = {"get_working_batteries.return_value" : {106 , 206 }}
181+ battery_pool_mock = MagicMock (spec = BatteryPoolStatus , ** attrs )
182+ BatteryPoolStatus .async_new = AsyncMock ( # type: ignore
183+ return_value = battery_pool_mock
184+ )
185+
178186 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
179187
180188 distributor = PowerDistributingActor (service_channels )
181189
182- # Mock that all requested batteries are working.
183- distributor ._battery_pool = MagicMock (spec = BatteryPoolStatus )
184- distributor ._battery_pool .get_working_batteries .return_value = {106 , 206 }
185-
186190 user1_handle = channel1 .client_handle
187191 task1 = user1_handle .send (
188192 Request (
@@ -230,14 +234,15 @@ async def test_power_distributor_invalid_battery_id(
230234 power = 1200 , batteries = {106 , 208 }, request_timeout_sec = SAFETY_TIMEOUT
231235 )
232236
237+ attrs = {"get_working_batteries.return_value" : request .batteries }
238+ battery_pool_mock = MagicMock (spec = BatteryPoolStatus , ** attrs )
239+ BatteryPoolStatus .async_new = AsyncMock ( # type: ignore
240+ return_value = battery_pool_mock
241+ )
233242 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
234243
235244 distributor = PowerDistributingActor (service_channels )
236245
237- # Mock that all requested batteries are working.
238- distributor ._battery_pool = MagicMock (spec = BatteryPoolStatus )
239- distributor ._battery_pool .get_working_batteries .return_value = request .batteries
240-
241246 user1_handle = channel1 .client_handle
242247 await user1_handle .send (request )
243248
@@ -271,17 +276,16 @@ async def test_power_distributor_overlapping_batteries(
271276 }
272277
273278 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
279+ attrs = {
280+ "get_working_batteries.side_effect" : [{106 , 206 }, {106 , 306 }, {106 , 206 }]
281+ }
282+ battery_pool_mock = MagicMock (spec = BatteryPoolStatus , ** attrs )
283+ BatteryPoolStatus .async_new = AsyncMock ( # type: ignore
284+ return_value = battery_pool_mock
285+ )
274286
275287 distributor = PowerDistributingActor (service_channels )
276288
277- # Mock that all requested batteries are working.
278- distributor ._battery_pool = MagicMock (spec = BatteryPoolStatus )
279- distributor ._battery_pool .get_working_batteries .side_effect = [
280- {106 , 206 },
281- {106 , 306 },
282- {106 , 206 },
283- ]
284-
285289 user1_handle = channel1 .client_handle
286290 task1 = user1_handle .send (
287291 Request (
@@ -349,14 +353,16 @@ async def test_power_distributor_one_user_adjust_power_consume(
349353 adjust_power = False ,
350354 )
351355
356+ attrs = {"get_working_batteries.return_value" : request .batteries }
357+ battery_pool_mock = MagicMock (spec = BatteryPoolStatus , ** attrs )
358+ BatteryPoolStatus .async_new = AsyncMock ( # type: ignore
359+ return_value = battery_pool_mock
360+ )
361+
352362 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
353363
354364 distributor = PowerDistributingActor (service_channels )
355365
356- # Mock that all requested batteries are working.
357- distributor ._battery_pool = MagicMock (spec = BatteryPoolStatus )
358- distributor ._battery_pool .get_working_batteries .return_value = request .batteries
359-
360366 user1_handle = channel1 .client_handle
361367 await user1_handle .send (request )
362368
@@ -394,14 +400,16 @@ async def test_power_distributor_one_user_adjust_power_supply(
394400 adjust_power = False ,
395401 )
396402
403+ attrs = {"get_working_batteries.return_value" : request .batteries }
404+ battery_pool_mock = MagicMock (spec = BatteryPoolStatus , ** attrs )
405+ BatteryPoolStatus .async_new = AsyncMock ( # type: ignore
406+ return_value = battery_pool_mock
407+ )
408+
397409 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
398410
399411 distributor = PowerDistributingActor (service_channels )
400412
401- # Mock that all requested batteries are working.
402- distributor ._battery_pool = MagicMock (spec = BatteryPoolStatus )
403- distributor ._battery_pool .get_working_batteries .return_value = request .batteries
404-
405413 user1_handle = channel1 .client_handle
406414 await user1_handle .send (request )
407415
@@ -439,14 +447,16 @@ async def test_power_distributor_one_user_adjust_power_success(
439447 adjust_power = False ,
440448 )
441449
450+ attrs = {"get_working_batteries.return_value" : request .batteries }
451+ battery_pool_mock = MagicMock (spec = BatteryPoolStatus , ** attrs )
452+ BatteryPoolStatus .async_new = AsyncMock ( # type: ignore
453+ return_value = battery_pool_mock
454+ )
455+
442456 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
443457
444458 distributor = PowerDistributingActor (service_channels )
445459
446- # Mock that all requested batteries are working.
447- distributor ._battery_pool = MagicMock (spec = BatteryPoolStatus )
448- distributor ._battery_pool .get_working_batteries .return_value = request .batteries
449-
450460 user1_handle = channel1 .client_handle
451461 await user1_handle .send (request )
452462
@@ -476,16 +486,16 @@ async def test_not_all_batteries_are_working(self, mocker: MockerFixture) -> Non
476486 power = 1200 , batteries = {106 , 206 }, request_timeout_sec = SAFETY_TIMEOUT
477487 )
478488
489+ attrs = {"get_working_batteries.return_value" : request .batteries - {106 }}
490+ battery_pool_mock = MagicMock (spec = BatteryPoolStatus , ** attrs )
491+ BatteryPoolStatus .async_new = AsyncMock ( # type: ignore
492+ return_value = battery_pool_mock
493+ )
494+
479495 mocker .patch ("asyncio.sleep" , new_callable = AsyncMock )
480496
481497 distributor = PowerDistributingActor ({"user1" : channel .service_handle })
482498
483- # Mock that all requested batteries are working.
484- distributor ._battery_pool = MagicMock (spec = BatteryPoolStatus )
485- distributor ._battery_pool .get_working_batteries .return_value = (
486- request .batteries - {106 }
487- )
488-
489499 client_handle = channel .client_handle
490500 await client_handle .send (request )
491501
0 commit comments