Skip to content

Commit c547a39

Browse files
committed
Use math.isclose to compare float values in logical meter tests
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 6bc7172 commit c547a39

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

tests/timeseries/test_logical_meter.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from __future__ import annotations
77

88
from datetime import datetime
9+
from math import isclose
10+
911
from frequenz.channels import Receiver, Sender
1012
from pytest_mock import MockerFixture
1113

@@ -54,6 +56,13 @@ async def _get_resampled_stream( # pylint: disable=too-many-arguments
5456
)
5557
# pylint: enable=protected-access
5658

59+
def _equal_float_lists(self, list1: list[float], list2: list[float]) -> bool:
60+
return (
61+
len(list1) > 0
62+
and len(list1) == len(list2)
63+
and all(isclose(v1, v2) for v1, v2 in zip(list1, list2))
64+
)
65+
5766
async def _synchronize_receivers(
5867
self,
5968
receivers: list[FormulaReceiver | FormulaReceiver3Phase | Receiver[Sample]],
@@ -116,8 +125,7 @@ async def test_grid_power_1(self, mocker: MockerFixture) -> None:
116125
assert val is not None
117126
results.append(val.value)
118127
await mockgrid.cleanup()
119-
120-
assert results == main_meter_data
128+
assert self._equal_float_lists(results, main_meter_data)
121129

122130
async def test_grid_power_2(
123131
self,
@@ -166,7 +174,7 @@ async def test_grid_power_2(
166174
await mockgrid.cleanup()
167175

168176
assert len(results) == 10
169-
assert results == meter_sums
177+
assert self._equal_float_lists(results, meter_sums)
170178

171179
async def test_battery_and_pv_power( # pylint: disable=too-many-locals
172180
self,
@@ -242,9 +250,9 @@ async def test_battery_and_pv_power( # pylint: disable=too-many-locals
242250
await mockgrid.cleanup()
243251

244252
assert len(battery_results) == 10
245-
assert battery_results == battery_inv_sums
253+
assert self._equal_float_lists(battery_results, battery_inv_sums)
246254
assert len(pv_results) == 10
247-
assert pv_results == pv_inv_sums
255+
assert self._equal_float_lists(pv_results, pv_inv_sums)
248256

249257
async def test_soc(self, mocker: MockerFixture) -> None:
250258
"""Test the soc calculation."""
@@ -287,7 +295,9 @@ async def test_soc(self, mocker: MockerFixture) -> None:
287295
# So we drop it from out control value as well.
288296
if ctr >= 7:
289297
bat_vals = bat_vals[:2]
290-
assert (await soc_recv.receive()).value == sum(bat_vals) / len(bat_vals)
298+
assert isclose(
299+
(await soc_recv.receive()).value, sum(bat_vals) / len(bat_vals)
300+
)
291301

292302
await mockgrid.cleanup()
293303

@@ -333,9 +343,8 @@ async def test_formula_composition( # pylint: disable=too-many-locals
333343
assert bat_pow is not None and bat_pow.value is not None
334344
assert pv_pow is not None and pv_pow.value is not None
335345
assert main_pow is not None and main_pow.value is not None
336-
337-
assert inv_calc_pow.value == pv_pow.value + bat_pow.value
338-
assert grid_pow.value == inv_calc_pow.value + main_pow.value
346+
assert isclose(inv_calc_pow.value, pv_pow.value + bat_pow.value)
347+
assert isclose(grid_pow.value, inv_calc_pow.value + main_pow.value)
339348
count += 1
340349

341350
await mockgrid.cleanup()

0 commit comments

Comments
 (0)