mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Convert recorder util tests to use async API (#116926)
This commit is contained in:
parent
9f9493c504
commit
5150557372
@ -1,6 +1,5 @@
|
|||||||
"""Test util methods."""
|
"""Test util methods."""
|
||||||
|
|
||||||
from collections.abc import Callable
|
|
||||||
from datetime import UTC, datetime, timedelta
|
from datetime import UTC, datetime, timedelta
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@ -15,7 +14,7 @@ from sqlalchemy.sql.elements import TextClause
|
|||||||
from sqlalchemy.sql.lambdas import StatementLambdaElement
|
from sqlalchemy.sql.lambdas import StatementLambdaElement
|
||||||
|
|
||||||
from homeassistant.components import recorder
|
from homeassistant.components import recorder
|
||||||
from homeassistant.components.recorder import util
|
from homeassistant.components.recorder import Recorder, util
|
||||||
from homeassistant.components.recorder.const import DOMAIN, SQLITE_URL_PREFIX
|
from homeassistant.components.recorder.const import DOMAIN, SQLITE_URL_PREFIX
|
||||||
from homeassistant.components.recorder.db_schema import RecorderRuns
|
from homeassistant.components.recorder.db_schema import RecorderRuns
|
||||||
from homeassistant.components.recorder.history.modern import (
|
from homeassistant.components.recorder.history.modern import (
|
||||||
@ -37,15 +36,33 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.issue_registry import async_get as async_get_issue_registry
|
from homeassistant.helpers.issue_registry import async_get as async_get_issue_registry
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .common import corrupt_db_file, run_information_with_session, wait_recording_done
|
from .common import (
|
||||||
|
async_wait_recording_done,
|
||||||
|
corrupt_db_file,
|
||||||
|
run_information_with_session,
|
||||||
|
)
|
||||||
|
|
||||||
from tests.common import async_test_home_assistant
|
from tests.common import async_test_home_assistant
|
||||||
from tests.typing import RecorderInstanceGenerator
|
from tests.typing import RecorderInstanceGenerator
|
||||||
|
|
||||||
|
|
||||||
def test_session_scope_not_setup(hass_recorder: Callable[..., HomeAssistant]) -> None:
|
@pytest.fixture
|
||||||
|
async def mock_recorder_before_hass(
|
||||||
|
async_setup_recorder_instance: RecorderInstanceGenerator,
|
||||||
|
) -> None:
|
||||||
|
"""Set up recorder."""
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def setup_recorder(recorder_mock: Recorder) -> None:
|
||||||
|
"""Set up recorder."""
|
||||||
|
|
||||||
|
|
||||||
|
async def testsession_scope_not_setup(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
setup_recorder: None,
|
||||||
|
) -> None:
|
||||||
"""Try to create a session scope when not setup."""
|
"""Try to create a session scope when not setup."""
|
||||||
hass = hass_recorder()
|
|
||||||
with (
|
with (
|
||||||
patch.object(util.get_instance(hass), "get_session", return_value=None),
|
patch.object(util.get_instance(hass), "get_session", return_value=None),
|
||||||
pytest.raises(RuntimeError),
|
pytest.raises(RuntimeError),
|
||||||
@ -54,12 +71,10 @@ def test_session_scope_not_setup(hass_recorder: Callable[..., HomeAssistant]) ->
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def test_recorder_bad_execute(hass_recorder: Callable[..., HomeAssistant]) -> None:
|
async def testrecorder_bad_execute(hass: HomeAssistant, setup_recorder: None) -> None:
|
||||||
"""Bad execute, retry 3 times."""
|
"""Bad execute, retry 3 times."""
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
|
||||||
hass_recorder()
|
|
||||||
|
|
||||||
def to_native(validate_entity_id=True):
|
def to_native(validate_entity_id=True):
|
||||||
"""Raise exception."""
|
"""Raise exception."""
|
||||||
raise SQLAlchemyError
|
raise SQLAlchemyError
|
||||||
@ -700,16 +715,14 @@ async def test_no_issue_for_mariadb_with_MDEV_25020(
|
|||||||
assert database_engine.optimizer.slow_range_in_select is False
|
assert database_engine.optimizer.slow_range_in_select is False
|
||||||
|
|
||||||
|
|
||||||
def test_basic_sanity_check(
|
async def testbasic_sanity_check(
|
||||||
hass_recorder: Callable[..., HomeAssistant], recorder_db_url
|
hass: HomeAssistant, setup_recorder: None, recorder_db_url
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test the basic sanity checks with a missing table."""
|
"""Test the basic sanity checks with a missing table."""
|
||||||
if recorder_db_url.startswith(("mysql://", "postgresql://")):
|
if recorder_db_url.startswith(("mysql://", "postgresql://")):
|
||||||
# This test is specific for SQLite
|
# This test is specific for SQLite
|
||||||
return
|
return
|
||||||
|
|
||||||
hass = hass_recorder()
|
|
||||||
|
|
||||||
cursor = util.get_instance(hass).engine.raw_connection().cursor()
|
cursor = util.get_instance(hass).engine.raw_connection().cursor()
|
||||||
|
|
||||||
assert util.basic_sanity_check(cursor) is True
|
assert util.basic_sanity_check(cursor) is True
|
||||||
@ -720,8 +733,9 @@ def test_basic_sanity_check(
|
|||||||
util.basic_sanity_check(cursor)
|
util.basic_sanity_check(cursor)
|
||||||
|
|
||||||
|
|
||||||
def test_combined_checks(
|
async def testcombined_checks(
|
||||||
hass_recorder: Callable[..., HomeAssistant],
|
hass: HomeAssistant,
|
||||||
|
setup_recorder: None,
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
recorder_db_url,
|
recorder_db_url,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -730,7 +744,6 @@ def test_combined_checks(
|
|||||||
# This test is specific for SQLite
|
# This test is specific for SQLite
|
||||||
return
|
return
|
||||||
|
|
||||||
hass = hass_recorder()
|
|
||||||
instance = util.get_instance(hass)
|
instance = util.get_instance(hass)
|
||||||
instance.db_retry_wait = 0
|
instance.db_retry_wait = 0
|
||||||
|
|
||||||
@ -788,12 +801,10 @@ def test_combined_checks(
|
|||||||
util.run_checks_on_open_db("fake_db_path", cursor)
|
util.run_checks_on_open_db("fake_db_path", cursor)
|
||||||
|
|
||||||
|
|
||||||
def test_end_incomplete_runs(
|
async def testend_incomplete_runs(
|
||||||
hass_recorder: Callable[..., HomeAssistant], caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, setup_recorder: None, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Ensure we can end incomplete runs."""
|
"""Ensure we can end incomplete runs."""
|
||||||
hass = hass_recorder()
|
|
||||||
|
|
||||||
with session_scope(hass=hass) as session:
|
with session_scope(hass=hass) as session:
|
||||||
run_info = run_information_with_session(session)
|
run_info = run_information_with_session(session)
|
||||||
assert isinstance(run_info, RecorderRuns)
|
assert isinstance(run_info, RecorderRuns)
|
||||||
@ -814,15 +825,14 @@ def test_end_incomplete_runs(
|
|||||||
assert "Ended unfinished session" in caplog.text
|
assert "Ended unfinished session" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
def test_periodic_db_cleanups(
|
async def testperiodic_db_cleanups(
|
||||||
hass_recorder: Callable[..., HomeAssistant], recorder_db_url
|
hass: HomeAssistant, setup_recorder: None, recorder_db_url
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test periodic db cleanups."""
|
"""Test periodic db cleanups."""
|
||||||
if recorder_db_url.startswith(("mysql://", "postgresql://")):
|
if recorder_db_url.startswith(("mysql://", "postgresql://")):
|
||||||
# This test is specific for SQLite
|
# This test is specific for SQLite
|
||||||
return
|
return
|
||||||
|
|
||||||
hass = hass_recorder()
|
|
||||||
with patch.object(util.get_instance(hass).engine, "connect") as connect_mock:
|
with patch.object(util.get_instance(hass).engine, "connect") as connect_mock:
|
||||||
util.periodic_db_cleanups(util.get_instance(hass))
|
util.periodic_db_cleanups(util.get_instance(hass))
|
||||||
|
|
||||||
@ -894,15 +904,15 @@ def test_build_mysqldb_conv() -> None:
|
|||||||
|
|
||||||
|
|
||||||
@patch("homeassistant.components.recorder.util.QUERY_RETRY_WAIT", 0)
|
@patch("homeassistant.components.recorder.util.QUERY_RETRY_WAIT", 0)
|
||||||
def test_execute_stmt_lambda_element(
|
async def testexecute_stmt_lambda_element(
|
||||||
hass_recorder: Callable[..., HomeAssistant],
|
hass: HomeAssistant,
|
||||||
|
setup_recorder: None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test executing with execute_stmt_lambda_element."""
|
"""Test executing with execute_stmt_lambda_element."""
|
||||||
hass = hass_recorder()
|
|
||||||
instance = recorder.get_instance(hass)
|
instance = recorder.get_instance(hass)
|
||||||
hass.states.set("sensor.on", "on")
|
hass.states.async_set("sensor.on", "on")
|
||||||
new_state = hass.states.get("sensor.on")
|
new_state = hass.states.get("sensor.on")
|
||||||
wait_recording_done(hass)
|
await async_wait_recording_done(hass)
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
tomorrow = now + timedelta(days=1)
|
tomorrow = now + timedelta(days=1)
|
||||||
one_week_from_now = now + timedelta(days=7)
|
one_week_from_now = now + timedelta(days=7)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user