Skip to content

Commit 94d1e87

Browse files
committed
Extract reusable create_components method outside test class
This allows us to not pack all tests in the same class, but group them separately in smaller units. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent ca12c4a commit 94d1e87

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

tests/power/test_distribution_algorithm.py

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ def inverter_msg(
9393
)
9494

9595

96+
def create_components(
97+
num: int,
98+
capacity: List[Metric],
99+
soc: List[Metric],
100+
power: List[PowerBounds],
101+
) -> List[InvBatPair]:
102+
"""Create components with given arguments.
103+
104+
Args:
105+
num: Number of components
106+
capacity: Capacity for each battery
107+
soc: SoC for each battery
108+
soc_bounds: SoC bounds for each battery
109+
supply_bounds: Supply bounds for each battery and inverter
110+
consumption_bounds: Consumption bounds for each battery and inverter
111+
112+
Returns:
113+
List of the components
114+
"""
115+
components: List[InvBatPair] = []
116+
for i in range(0, num):
117+
battery = battery_msg(2 * i, capacity[i], soc[i], power[2 * i])
118+
inverter = inverter_msg(2 * i + 1, power[2 * i + 1])
119+
components.append(InvBatPair(battery, inverter))
120+
return components
121+
122+
96123
class TestDistributionAlgorithm: # pylint: disable=too-many-public-methods
97124
"""Test whether the algorithm works as expected."""
98125

@@ -266,34 +293,6 @@ def test_distribute_power_three_batteries_3(self) -> None:
266293
assert result.distribution == approx({1: 0, 3: 300, 5: 0})
267294
assert result.remaining_power == approx(700.0)
268295

269-
def create_components( # pylint: disable=too-many-arguments
270-
self,
271-
num: int,
272-
capacity: List[Metric],
273-
soc: List[Metric],
274-
power: List[PowerBounds],
275-
) -> List[InvBatPair]:
276-
"""Create components with given arguments.
277-
278-
Args:
279-
num: Number of components
280-
capacity: Capacity for each battery
281-
soc: SoC for each battery
282-
soc_bounds: SoC bounds for each battery
283-
supply_bounds: Supply bounds for each battery and inverter
284-
consumption_bounds: Consumption bounds for each battery and inverter
285-
286-
Returns:
287-
List of the components
288-
"""
289-
290-
components: List[InvBatPair] = []
291-
for i in range(0, num):
292-
battery = battery_msg(2 * i, capacity[i], soc[i], power[2 * i])
293-
inverter = inverter_msg(2 * i + 1, power[2 * i + 1])
294-
components.append(InvBatPair(battery, inverter))
295-
return components
296-
297296
# Test distribute supply power
298297
def test_supply_three_batteries_1(self) -> None:
299298
"""Test distribute supply power for batteries with different SoC."""
@@ -314,7 +313,7 @@ def test_supply_three_batteries_1(self) -> None:
314313
PowerBounds(-900, 0, 0, 0),
315314
PowerBounds(-900, 0, 0, 0),
316315
]
317-
components = self.create_components(3, capacity, soc, bounds)
316+
components = create_components(3, capacity, soc, bounds)
318317

319318
algorithm = DistributionAlgorithm(distributor_exponent=1)
320319
result = algorithm.distribute_power(-1200, components)
@@ -339,7 +338,7 @@ def test_supply_three_batteries_2(self) -> None:
339338
PowerBounds(-900, 0, 0, 0),
340339
PowerBounds(-900, 0, 0, 0),
341340
]
342-
components = self.create_components(3, capacity, soc, bounds)
341+
components = create_components(3, capacity, soc, bounds)
343342

344343
algorithm = DistributionAlgorithm(distributor_exponent=1)
345344
result = algorithm.distribute_power(-1400, components)
@@ -364,7 +363,7 @@ def test_supply_three_batteries_3(self) -> None:
364363
PowerBounds(-800, 0, 0, 0),
365364
PowerBounds(-900, 0, 0, 0),
366365
]
367-
components = self.create_components(3, capacity, soc, supply_bounds)
366+
components = create_components(3, capacity, soc, supply_bounds)
368367

369368
algorithm = DistributionAlgorithm(distributor_exponent=1)
370369
result = algorithm.distribute_power(-1400, components)
@@ -389,7 +388,7 @@ def test_supply_three_batteries_4(self) -> None:
389388
PowerBounds(-800, 0, 0, 0),
390389
PowerBounds(-900, 0, 0, 0),
391390
]
392-
components = self.create_components(3, capacity, soc, bounds)
391+
components = create_components(3, capacity, soc, bounds)
393392

394393
algorithm = DistributionAlgorithm(distributor_exponent=1)
395394
result = algorithm.distribute_power(-1700, components)
@@ -414,7 +413,7 @@ def test_supply_three_batteries_5(self) -> None:
414413
PowerBounds(-800, 0, 0, 0),
415414
PowerBounds(-900, 0, 0, 0),
416415
]
417-
components = self.create_components(3, capacity, soc, supply_bounds)
416+
components = create_components(3, capacity, soc, supply_bounds)
418417

419418
algorithm = DistributionAlgorithm(distributor_exponent=1)
420419
result = algorithm.distribute_power(-1700, components)
@@ -437,7 +436,7 @@ def test_supply_two_batteries_1(self) -> None:
437436
PowerBounds(-600, 0, 0, 0),
438437
PowerBounds(-1000, 0, 0, 0),
439438
]
440-
components = self.create_components(2, capacity, soc, supply_bounds)
439+
components = create_components(2, capacity, soc, supply_bounds)
441440

442441
algorithm = DistributionAlgorithm(distributor_exponent=1)
443442
result = algorithm.distribute_power(-600, components)
@@ -459,7 +458,7 @@ def test_supply_two_batteries_2(self) -> None:
459458
PowerBounds(-600, 0, 0, 0),
460459
PowerBounds(-1000, 0, 0, 0),
461460
]
462-
components = self.create_components(2, capacity, soc, supply_bounds)
461+
components = create_components(2, capacity, soc, supply_bounds)
463462

464463
algorithm = DistributionAlgorithm(distributor_exponent=1)
465464
result = algorithm.distribute_power(-600, components)
@@ -485,7 +484,7 @@ def test_consumption_three_batteries_1(self) -> None:
485484
PowerBounds(0, 0, 0, 900),
486485
PowerBounds(0, 0, 0, 900),
487486
]
488-
components = self.create_components(3, capacity, soc, bounds)
487+
components = create_components(3, capacity, soc, bounds)
489488

490489
algorithm = DistributionAlgorithm(distributor_exponent=1)
491490
result = algorithm.distribute_power(1200, components)
@@ -510,7 +509,7 @@ def test_consumption_three_batteries_2(self) -> None:
510509
PowerBounds(0, 0, 0, 900),
511510
PowerBounds(0, 0, 0, 900),
512511
]
513-
components = self.create_components(3, capacity, soc, bounds)
512+
components = create_components(3, capacity, soc, bounds)
514513

515514
algorithm = DistributionAlgorithm(distributor_exponent=1)
516515
result = algorithm.distribute_power(1400, components)
@@ -535,7 +534,7 @@ def test_consumption_three_batteries_3(self) -> None:
535534
PowerBounds(0, 0, 0, 800),
536535
PowerBounds(0, 0, 0, 900),
537536
]
538-
components = self.create_components(3, capacity, soc, bounds)
537+
components = create_components(3, capacity, soc, bounds)
539538

540539
algorithm = DistributionAlgorithm(distributor_exponent=1)
541540
result = algorithm.distribute_power(1400, components)
@@ -560,7 +559,7 @@ def test_consumption_three_batteries_4(self) -> None:
560559
PowerBounds(0, 0, 0, 800),
561560
PowerBounds(0, 0, 0, 900),
562561
]
563-
components = self.create_components(3, capacity, soc, bounds)
562+
components = create_components(3, capacity, soc, bounds)
564563

565564
algorithm = DistributionAlgorithm(distributor_exponent=1)
566565
result = algorithm.distribute_power(1700, components)
@@ -585,7 +584,7 @@ def test_consumption_three_batteries_5(self) -> None:
585584
PowerBounds(0, 0, 0, 800),
586585
PowerBounds(0, 0, 0, 900),
587586
]
588-
components = self.create_components(3, capacity, soc, bounds)
587+
components = create_components(3, capacity, soc, bounds)
589588

590589
algorithm = DistributionAlgorithm(distributor_exponent=1)
591590
result = algorithm.distribute_power(1700, components)
@@ -610,7 +609,7 @@ def test_consumption_three_batteries_6(self) -> None:
610609
PowerBounds(0, 0, 0, 800),
611610
PowerBounds(0, 0, 0, 900),
612611
]
613-
components = self.create_components(3, capacity, soc, bounds)
612+
components = create_components(3, capacity, soc, bounds)
614613

615614
algorithm = DistributionAlgorithm(distributor_exponent=1)
616615
result = algorithm.distribute_power(1700, components)
@@ -635,7 +634,7 @@ def test_consumption_three_batteries_7(self) -> None:
635634
PowerBounds(0, 0, 0, 800),
636635
PowerBounds(0, 0, 0, 900),
637636
]
638-
components = self.create_components(3, capacity, soc, bounds)
637+
components = create_components(3, capacity, soc, bounds)
639638

640639
algorithm = DistributionAlgorithm(distributor_exponent=1)
641640
result = algorithm.distribute_power(500, components)
@@ -657,7 +656,7 @@ def test_consumption_two_batteries_1(self) -> None:
657656
PowerBounds(0, 0, 0, 600),
658657
PowerBounds(0, 0, 0, 1000),
659658
]
660-
components = self.create_components(2, capacity, soc, bounds)
659+
components = create_components(2, capacity, soc, bounds)
661660

662661
algorithm = DistributionAlgorithm(distributor_exponent=1)
663662
result = algorithm.distribute_power(600, components)
@@ -679,7 +678,7 @@ def test_consumption_two_batteries_distribution_exponent(self) -> None:
679678
PowerBounds(0, 0, 0, 9000),
680679
PowerBounds(0, 0, 0, 9000),
681680
]
682-
components = self.create_components(2, capacity, soc, bounds)
681+
components = create_components(2, capacity, soc, bounds)
683682

684683
algorithm = DistributionAlgorithm(distributor_exponent=1)
685684
result = algorithm.distribute_power(8000, components)
@@ -713,7 +712,7 @@ def test_consumption_two_batteries_distribution_exponent_1(self) -> None:
713712
PowerBounds(0, 0, 0, 9000),
714713
PowerBounds(0, 0, 0, 9000),
715714
]
716-
components = self.create_components(2, capacity, soc, bounds)
715+
components = create_components(2, capacity, soc, bounds)
717716

718717
algorithm = DistributionAlgorithm(distributor_exponent=1)
719718
result = algorithm.distribute_power(900, components)
@@ -765,7 +764,7 @@ def test_supply_two_batteries_distribution_exponent(self) -> None:
765764
PowerBounds(-9000, 0, 0, 0),
766765
PowerBounds(-9000, 0, 0, 0),
767766
]
768-
components = self.create_components(2, capacity, soc, bounds)
767+
components = create_components(2, capacity, soc, bounds)
769768

770769
algorithm = DistributionAlgorithm(distributor_exponent=1)
771770
result = algorithm.distribute_power(-8000, components)
@@ -799,7 +798,7 @@ def test_supply_two_batteries_distribution_exponent_1(self) -> None:
799798
PowerBounds(-9000, 0, 0, 0),
800799
PowerBounds(-9000, 0, 0, 0),
801800
]
802-
components = self.create_components(2, capacity, soc, supply_bounds)
801+
components = create_components(2, capacity, soc, supply_bounds)
803802

804803
algorithm = DistributionAlgorithm(distributor_exponent=1)
805804
result = algorithm.distribute_power(-8000, components)
@@ -836,7 +835,7 @@ def test_supply_three_batteries_distribution_exponent_2(self) -> None:
836835
PowerBounds(-9000, 0, 0, 0),
837836
PowerBounds(-9000, 0, 0, 0),
838837
]
839-
components = self.create_components(3, capacity, soc, bounds)
838+
components = create_components(3, capacity, soc, bounds)
840839

841840
algorithm = DistributionAlgorithm(distributor_exponent=1)
842841
result = algorithm.distribute_power(-8000, components)
@@ -879,7 +878,7 @@ def test_supply_three_batteries_distribution_exponent_3(self) -> None:
879878
PowerBounds(-9000, 0, 0, 0),
880879
PowerBounds(-9000, 0, 0, 0),
881880
]
882-
components = self.create_components(3, capacity, soc, supply_bounds)
881+
components = create_components(3, capacity, soc, supply_bounds)
883882

884883
algorithm = DistributionAlgorithm(distributor_exponent=0.5)
885884
result = algorithm.distribute_power(-1300, components)
@@ -907,7 +906,7 @@ def test_supply_two_batteries_distribution_exponent_less_then_1(self) -> None:
907906
PowerBounds(0, 0, 0, 9000),
908907
PowerBounds(0, 0, 0, 9000),
909908
]
910-
components = self.create_components(2, capacity, soc, bounds)
909+
components = create_components(2, capacity, soc, bounds)
911910

912911
algorithm = DistributionAlgorithm(distributor_exponent=0.5)
913912
result = algorithm.distribute_power(1000, components)

0 commit comments

Comments
 (0)