Fix flaky statistics tests (#51214)

* Fix flaky statistics tests

* Tweak
This commit is contained in:
Erik Montnemery 2021-05-28 22:37:17 +02:00 committed by GitHub
parent 538a189168
commit 39c94e8daa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 36 deletions

View file

@ -3,6 +3,8 @@
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch, sentinel from unittest.mock import patch, sentinel
from pytest import approx
from homeassistant.components.recorder import history from homeassistant.components.recorder import history
from homeassistant.components.recorder.const import DATA_INSTANCE from homeassistant.components.recorder.const import DATA_INSTANCE
from homeassistant.components.recorder.models import process_timestamp_to_utc_isoformat from homeassistant.components.recorder.models import process_timestamp_to_utc_isoformat
@ -30,9 +32,9 @@ def test_compile_hourly_statistics(hass_recorder):
{ {
"statistic_id": "sensor.test1", "statistic_id": "sensor.test1",
"start": process_timestamp_to_utc_isoformat(zero), "start": process_timestamp_to_utc_isoformat(zero),
"mean": 14.915254237288135, "mean": approx(14.915254237288135),
"min": 10.0, "min": approx(10.0),
"max": 20.0, "max": approx(20.0),
"last_reset": None, "last_reset": None,
"state": None, "state": None,
"sum": None, "sum": None,

View file

@ -3,6 +3,8 @@
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch, sentinel from unittest.mock import patch, sentinel
from pytest import approx
from homeassistant.components.recorder import history from homeassistant.components.recorder import history
from homeassistant.components.recorder.const import DATA_INSTANCE from homeassistant.components.recorder.const import DATA_INSTANCE
from homeassistant.components.recorder.models import process_timestamp_to_utc_isoformat from homeassistant.components.recorder.models import process_timestamp_to_utc_isoformat
@ -31,9 +33,9 @@ def test_compile_hourly_statistics(hass_recorder):
{ {
"statistic_id": "sensor.test1", "statistic_id": "sensor.test1",
"start": process_timestamp_to_utc_isoformat(zero), "start": process_timestamp_to_utc_isoformat(zero),
"mean": 16.440677966101696, "mean": approx(16.440677966101696),
"min": 10.0, "min": approx(10.0),
"max": 30.0, "max": approx(30.0),
"last_reset": None, "last_reset": None,
"state": None, "state": None,
"sum": None, "sum": None,
@ -75,8 +77,8 @@ def test_compile_hourly_energy_statistics(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(zero), "last_reset": process_timestamp_to_utc_isoformat(zero),
"state": 20.0, "state": approx(20.0),
"sum": 10.0, "sum": approx(10.0),
}, },
{ {
"statistic_id": "sensor.test1", "statistic_id": "sensor.test1",
@ -85,8 +87,8 @@ def test_compile_hourly_energy_statistics(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(four), "last_reset": process_timestamp_to_utc_isoformat(four),
"state": 40.0, "state": approx(40.0),
"sum": 10.0, "sum": approx(10.0),
}, },
{ {
"statistic_id": "sensor.test1", "statistic_id": "sensor.test1",
@ -95,8 +97,8 @@ def test_compile_hourly_energy_statistics(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(four), "last_reset": process_timestamp_to_utc_isoformat(four),
"state": 70.0, "state": approx(70.0),
"sum": 40.0, "sum": approx(40.0),
}, },
] ]
} }
@ -135,8 +137,8 @@ def test_compile_hourly_energy_statistics2(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(zero), "last_reset": process_timestamp_to_utc_isoformat(zero),
"state": 20.0, "state": approx(20.0),
"sum": 10.0, "sum": approx(10.0),
}, },
{ {
"statistic_id": "sensor.test1", "statistic_id": "sensor.test1",
@ -145,8 +147,8 @@ def test_compile_hourly_energy_statistics2(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(four), "last_reset": process_timestamp_to_utc_isoformat(four),
"state": 40.0, "state": approx(40.0),
"sum": 10.0, "sum": approx(10.0),
}, },
{ {
"statistic_id": "sensor.test1", "statistic_id": "sensor.test1",
@ -155,8 +157,8 @@ def test_compile_hourly_energy_statistics2(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(four), "last_reset": process_timestamp_to_utc_isoformat(four),
"state": 70.0, "state": approx(70.0),
"sum": 40.0, "sum": approx(40.0),
}, },
], ],
"sensor.test2": [ "sensor.test2": [
@ -167,8 +169,8 @@ def test_compile_hourly_energy_statistics2(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(zero), "last_reset": process_timestamp_to_utc_isoformat(zero),
"state": 130.0, "state": approx(130.0),
"sum": 20.0, "sum": approx(20.0),
}, },
{ {
"statistic_id": "sensor.test2", "statistic_id": "sensor.test2",
@ -177,8 +179,8 @@ def test_compile_hourly_energy_statistics2(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(four), "last_reset": process_timestamp_to_utc_isoformat(four),
"state": 45.0, "state": approx(45.0),
"sum": -95.0, "sum": approx(-95.0),
}, },
{ {
"statistic_id": "sensor.test2", "statistic_id": "sensor.test2",
@ -187,8 +189,8 @@ def test_compile_hourly_energy_statistics2(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(four), "last_reset": process_timestamp_to_utc_isoformat(four),
"state": 75.0, "state": approx(75.0),
"sum": -65.0, "sum": approx(-65.0),
}, },
], ],
"sensor.test3": [ "sensor.test3": [
@ -199,8 +201,8 @@ def test_compile_hourly_energy_statistics2(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(zero), "last_reset": process_timestamp_to_utc_isoformat(zero),
"state": 5.0, "state": approx(5.0),
"sum": 5.0, "sum": approx(5.0),
}, },
{ {
"statistic_id": "sensor.test3", "statistic_id": "sensor.test3",
@ -209,8 +211,8 @@ def test_compile_hourly_energy_statistics2(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(four), "last_reset": process_timestamp_to_utc_isoformat(four),
"state": 50.0, "state": approx(50.0),
"sum": 30.0, "sum": approx(30.0),
}, },
{ {
"statistic_id": "sensor.test3", "statistic_id": "sensor.test3",
@ -219,8 +221,8 @@ def test_compile_hourly_energy_statistics2(hass_recorder):
"mean": None, "mean": None,
"min": None, "min": None,
"last_reset": process_timestamp_to_utc_isoformat(four), "last_reset": process_timestamp_to_utc_isoformat(four),
"state": 90.0, "state": approx(90.0),
"sum": 70.0, "sum": approx(70.0),
}, },
], ],
} }
@ -243,9 +245,9 @@ def test_compile_hourly_statistics_unchanged(hass_recorder):
{ {
"statistic_id": "sensor.test1", "statistic_id": "sensor.test1",
"start": process_timestamp_to_utc_isoformat(four), "start": process_timestamp_to_utc_isoformat(four),
"mean": 30.0, "mean": approx(30.0),
"min": 30.0, "min": approx(30.0),
"max": 30.0, "max": approx(30.0),
"last_reset": None, "last_reset": None,
"state": None, "state": None,
"sum": None, "sum": None,
@ -271,9 +273,9 @@ def test_compile_hourly_statistics_partially_unavailable(hass_recorder):
{ {
"statistic_id": "sensor.test1", "statistic_id": "sensor.test1",
"start": process_timestamp_to_utc_isoformat(zero), "start": process_timestamp_to_utc_isoformat(zero),
"mean": 21.1864406779661, "mean": approx(21.1864406779661),
"min": 10.0, "min": approx(10.0),
"max": 25.0, "max": approx(25.0),
"last_reset": None, "last_reset": None,
"state": None, "state": None,
"sum": None, "sum": None,