|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
8 | 8 | from datetime import datetime |
| 9 | +from math import isclose |
| 10 | + |
9 | 11 | from frequenz.channels import Receiver, Sender |
10 | 12 | from pytest_mock import MockerFixture |
11 | 13 |
|
@@ -54,6 +56,13 @@ async def _get_resampled_stream( # pylint: disable=too-many-arguments |
54 | 56 | ) |
55 | 57 | # pylint: enable=protected-access |
56 | 58 |
|
| 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 | + |
57 | 66 | async def _synchronize_receivers( |
58 | 67 | self, |
59 | 68 | receivers: list[FormulaReceiver | FormulaReceiver3Phase | Receiver[Sample]], |
@@ -116,8 +125,7 @@ async def test_grid_power_1(self, mocker: MockerFixture) -> None: |
116 | 125 | assert val is not None |
117 | 126 | results.append(val.value) |
118 | 127 | await mockgrid.cleanup() |
119 | | - |
120 | | - assert results == main_meter_data |
| 128 | + assert self._equal_float_lists(results, main_meter_data) |
121 | 129 |
|
122 | 130 | async def test_grid_power_2( |
123 | 131 | self, |
@@ -166,7 +174,7 @@ async def test_grid_power_2( |
166 | 174 | await mockgrid.cleanup() |
167 | 175 |
|
168 | 176 | assert len(results) == 10 |
169 | | - assert results == meter_sums |
| 177 | + assert self._equal_float_lists(results, meter_sums) |
170 | 178 |
|
171 | 179 | async def test_battery_and_pv_power( # pylint: disable=too-many-locals |
172 | 180 | self, |
@@ -242,9 +250,9 @@ async def test_battery_and_pv_power( # pylint: disable=too-many-locals |
242 | 250 | await mockgrid.cleanup() |
243 | 251 |
|
244 | 252 | assert len(battery_results) == 10 |
245 | | - assert battery_results == battery_inv_sums |
| 253 | + assert self._equal_float_lists(battery_results, battery_inv_sums) |
246 | 254 | assert len(pv_results) == 10 |
247 | | - assert pv_results == pv_inv_sums |
| 255 | + assert self._equal_float_lists(pv_results, pv_inv_sums) |
248 | 256 |
|
249 | 257 | async def test_soc(self, mocker: MockerFixture) -> None: |
250 | 258 | """Test the soc calculation.""" |
@@ -287,7 +295,9 @@ async def test_soc(self, mocker: MockerFixture) -> None: |
287 | 295 | # So we drop it from out control value as well. |
288 | 296 | if ctr >= 7: |
289 | 297 | 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 | + ) |
291 | 301 |
|
292 | 302 | await mockgrid.cleanup() |
293 | 303 |
|
@@ -333,9 +343,8 @@ async def test_formula_composition( # pylint: disable=too-many-locals |
333 | 343 | assert bat_pow is not None and bat_pow.value is not None |
334 | 344 | assert pv_pow is not None and pv_pow.value is not None |
335 | 345 | 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) |
339 | 348 | count += 1 |
340 | 349 |
|
341 | 350 | await mockgrid.cleanup() |
|
0 commit comments