mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Add test for invalid mean type in StatisticsMeta (#141475)
This commit is contained in:
parent
46ee3d2b26
commit
002ca9611d
@ -2,10 +2,19 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import threading
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components import recorder
|
from homeassistant.components import recorder
|
||||||
|
from homeassistant.components.recorder.db_schema import StatisticsMeta
|
||||||
|
from homeassistant.components.recorder.models import (
|
||||||
|
StatisticMeanType,
|
||||||
|
StatisticMetaData,
|
||||||
|
)
|
||||||
from homeassistant.components.recorder.util import session_scope
|
from homeassistant.components.recorder.util import session_scope
|
||||||
|
from homeassistant.const import DEGREE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from tests.typing import RecorderInstanceGenerator
|
from tests.typing import RecorderInstanceGenerator
|
||||||
@ -55,3 +64,78 @@ async def test_unsafe_calls_to_statistics_meta_manager(
|
|||||||
session,
|
session,
|
||||||
statistic_ids=["light.kitchen"],
|
statistic_ids=["light.kitchen"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_invalid_mean_types(
|
||||||
|
async_setup_recorder_instance: RecorderInstanceGenerator,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""Test passing invalid mean types will be skipped and logged."""
|
||||||
|
instance = await async_setup_recorder_instance(
|
||||||
|
hass, {recorder.CONF_COMMIT_INTERVAL: 0}
|
||||||
|
)
|
||||||
|
instance.recorder_and_worker_thread_ids.add(threading.get_ident())
|
||||||
|
|
||||||
|
valid_metadata: dict[str, tuple[int, StatisticMetaData]] = {
|
||||||
|
"sensor.energy": (
|
||||||
|
1,
|
||||||
|
{
|
||||||
|
"mean_type": StatisticMeanType.NONE,
|
||||||
|
"has_mean": False,
|
||||||
|
"has_sum": True,
|
||||||
|
"name": "Total imported energy",
|
||||||
|
"source": "recorder",
|
||||||
|
"statistic_id": "sensor.energy",
|
||||||
|
"unit_of_measurement": "kWh",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
"sensor.wind_direction": (
|
||||||
|
2,
|
||||||
|
{
|
||||||
|
"mean_type": StatisticMeanType.CIRCULAR,
|
||||||
|
"has_mean": False,
|
||||||
|
"has_sum": False,
|
||||||
|
"name": "Wind direction",
|
||||||
|
"source": "recorder",
|
||||||
|
"statistic_id": "sensor.wind_direction",
|
||||||
|
"unit_of_measurement": DEGREE,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
"sensor.wind_speed": (
|
||||||
|
3,
|
||||||
|
{
|
||||||
|
"mean_type": StatisticMeanType.ARITHMETIC,
|
||||||
|
"has_mean": True,
|
||||||
|
"has_sum": False,
|
||||||
|
"name": "Wind speed",
|
||||||
|
"source": "recorder",
|
||||||
|
"statistic_id": "sensor.wind_speed",
|
||||||
|
"unit_of_measurement": "km/h",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
}
|
||||||
|
manager = instance.statistics_meta_manager
|
||||||
|
with instance.get_session() as session:
|
||||||
|
for _, metadata in valid_metadata.values():
|
||||||
|
session.add(StatisticsMeta.from_meta(metadata))
|
||||||
|
|
||||||
|
# Add invalid mean type
|
||||||
|
session.add(
|
||||||
|
StatisticsMeta(
|
||||||
|
statistic_id="sensor.invalid",
|
||||||
|
source="recorder",
|
||||||
|
has_sum=False,
|
||||||
|
name="Invalid",
|
||||||
|
mean_type=12345,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
# Check that the invalid mean type was skipped
|
||||||
|
assert manager.get_many(session) == valid_metadata
|
||||||
|
assert (
|
||||||
|
"homeassistant.components.recorder.table_managers.statistics_meta",
|
||||||
|
logging.WARNING,
|
||||||
|
"Invalid mean type found for statistic_id: sensor.invalid, mean_type: 12345. Skipping",
|
||||||
|
) in caplog.record_tuples
|
||||||
|
Loading…
x
Reference in New Issue
Block a user