Skip to content

Commit 2a816b8

Browse files
committed
Fix tests depending on the order of inverters
When testing the power distribution algorithm, we can't rely on which amount of power goes to each inverter when all inverters have the same bounds, because they are stored in a `set`, which provide no order guarantee, and when distributing power, power is only prioritized using bounds. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent e2f903d commit 2a816b8

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

tests/actor/power_distributing/test_battery_distribution_algorithm.py

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# pylint: disable=too-many-lines
55
"""Tests for distribution algorithm."""
66
import math
7+
from collections.abc import Sequence, Set
78
from dataclasses import dataclass
89
from datetime import datetime, timezone
910

@@ -1023,6 +1024,20 @@ def assert_result(result: DistributionResult, expected: DistributionResult) -> N
10231024
assert result.distribution == approx(expected.distribution, abs=0.01)
10241025
assert result.remaining_power == approx(expected.remaining_power, abs=0.01)
10251026

1027+
@staticmethod
1028+
def assert_any_result(
1029+
result: DistributionResult,
1030+
expected_distribution_ids: Set[int],
1031+
expected_distribution_powers: Sequence[float],
1032+
expected_remaining_power: float,
1033+
) -> None:
1034+
"""Assert the result is as expected, disregarding which power goes to which component."""
1035+
assert result.remaining_power == expected_remaining_power
1036+
assert {*result.distribution.keys()} == expected_distribution_ids
1037+
assert list(sorted(result.distribution.values())) == list(
1038+
sorted(expected_distribution_powers)
1039+
)
1040+
10261041
def test_scenario_1(self) -> None:
10271042
"""Test scenario 1.
10281043
@@ -1349,19 +1364,27 @@ def test_scenario_4(self) -> None:
13491364

13501365
algorithm = BatteryDistributionAlgorithm()
13511366

1352-
self.assert_result(
1367+
# The assignment of power to each inverter can be swapped, as they both have the
1368+
# same bounds, none will be preferred
1369+
self.assert_any_result(
13531370
algorithm.distribute_power(-300, components),
1354-
DistributionResult({2: -300, 3: 0}, remaining_power=0.0),
1371+
expected_distribution_ids={2, 3},
1372+
expected_distribution_powers=[-300, 0],
1373+
expected_remaining_power=0,
13551374
)
13561375

1357-
self.assert_result(
1376+
self.assert_any_result(
13581377
algorithm.distribute_power(300, components),
1359-
DistributionResult({2: 300, 3: 0}, remaining_power=0.0),
1378+
expected_distribution_ids={2, 3},
1379+
expected_distribution_powers=[300, 0],
1380+
expected_remaining_power=0,
13601381
)
13611382

1362-
self.assert_result(
1383+
self.assert_any_result(
13631384
algorithm.distribute_power(-1800, components),
1364-
DistributionResult({2: -1000, 3: -500}, remaining_power=-300.0),
1385+
expected_distribution_ids={2, 3},
1386+
expected_distribution_powers=[-1000, -500],
1387+
expected_remaining_power=-300,
13651388
)
13661389

13671390
def test_scenario_5(self) -> None:
@@ -1431,33 +1454,37 @@ def test_scenario_5(self) -> None:
14311454

14321455
algorithm = BatteryDistributionAlgorithm()
14331456

1434-
self.assert_result(
1457+
self.assert_any_result(
14351458
algorithm.distribute_power(-300, components),
1436-
DistributionResult({10: -200, 11: 0, 20: -100, 21: 0}, remaining_power=0.0),
1459+
expected_distribution_ids={10, 11, 20, 21},
1460+
expected_distribution_powers=[-200, 0, -100, 0],
1461+
expected_remaining_power=0.0,
14371462
)
14381463

1439-
self.assert_result(
1464+
self.assert_any_result(
14401465
algorithm.distribute_power(300, components),
1441-
DistributionResult({10: 200, 11: 0, 20: 100, 21: 0}, remaining_power=0.0),
1466+
expected_distribution_ids={10, 11, 20, 21},
1467+
expected_distribution_powers=[200, 0, 100, 0],
1468+
expected_remaining_power=0.0,
14421469
)
14431470

1444-
self.assert_result(
1471+
self.assert_any_result(
14451472
algorithm.distribute_power(-1800, components),
1446-
DistributionResult(
1447-
{10: -300, 11: 0, 20: -1000, 21: -500}, remaining_power=0.0
1448-
),
1473+
expected_distribution_ids={10, 11, 20, 21},
1474+
expected_distribution_powers=[-300, 0, -1000, -500],
1475+
expected_remaining_power=0.0,
14491476
)
14501477

1451-
self.assert_result(
1478+
self.assert_any_result(
14521479
algorithm.distribute_power(3000, components),
1453-
DistributionResult(
1454-
{10: 1000, 11: 500, 20: 1000, 21: 500}, remaining_power=0.0
1455-
),
1480+
expected_distribution_ids={10, 11, 20, 21},
1481+
expected_distribution_powers=[1000, 500, 1000, 500],
1482+
expected_remaining_power=0.0,
14561483
)
14571484

1458-
self.assert_result(
1485+
self.assert_any_result(
14591486
algorithm.distribute_power(3500, components),
1460-
DistributionResult(
1461-
{10: 1000, 11: 500, 20: 1000, 21: 500}, remaining_power=500.0
1462-
),
1487+
expected_distribution_ids={10, 11, 20, 21},
1488+
expected_distribution_powers=[1000, 500, 1000, 500],
1489+
expected_remaining_power=500.0,
14631490
)

0 commit comments

Comments
 (0)