Convert recorder util tests to use async API (#116926)
parent
9f9493c504
commit
5150557372
tests/components/recorder
|
@ -1,6 +1,5 @@
|
|||
"""Test util methods."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from datetime import UTC, datetime, timedelta
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
@ -15,7 +14,7 @@ from sqlalchemy.sql.elements import TextClause
|
|||
from sqlalchemy.sql.lambdas import StatementLambdaElement
|
||||
|
||||
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.db_schema import RecorderRuns
|
||||
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.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.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."""
|
||||
hass = hass_recorder()
|
||||
with (
|
||||
patch.object(util.get_instance(hass), "get_session", return_value=None),
|
||||
pytest.raises(RuntimeError),
|
||||
|
@ -54,12 +71,10 @@ def test_session_scope_not_setup(hass_recorder: Callable[..., HomeAssistant]) ->
|
|||
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."""
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
hass_recorder()
|
||||
|
||||
def to_native(validate_entity_id=True):
|
||||
"""Raise exception."""
|
||||
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
|
||||
|
||||
|
||||
def test_basic_sanity_check(
|
||||
hass_recorder: Callable[..., HomeAssistant], recorder_db_url
|
||||
async def testbasic_sanity_check(
|
||||
hass: HomeAssistant, setup_recorder: None, recorder_db_url
|
||||
) -> None:
|
||||
"""Test the basic sanity checks with a missing table."""
|
||||
if recorder_db_url.startswith(("mysql://", "postgresql://")):
|
||||
# This test is specific for SQLite
|
||||
return
|
||||
|
||||
hass = hass_recorder()
|
||||
|
||||
cursor = util.get_instance(hass).engine.raw_connection().cursor()
|
||||
|
||||
assert util.basic_sanity_check(cursor) is True
|
||||
|
@ -720,8 +733,9 @@ def test_basic_sanity_check(
|
|||
util.basic_sanity_check(cursor)
|
||||
|
||||
|
||||
def test_combined_checks(
|
||||
hass_recorder: Callable[..., HomeAssistant],
|
||||
async def testcombined_checks(
|
||||
hass: HomeAssistant,
|
||||
setup_recorder: None,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
recorder_db_url,
|
||||
) -> None:
|
||||
|
@ -730,7 +744,6 @@ def test_combined_checks(
|
|||
# This test is specific for SQLite
|
||||
return
|
||||
|
||||
hass = hass_recorder()
|
||||
instance = util.get_instance(hass)
|
||||
instance.db_retry_wait = 0
|
||||
|
||||
|
@ -788,12 +801,10 @@ def test_combined_checks(
|
|||
util.run_checks_on_open_db("fake_db_path", cursor)
|
||||
|
||||
|
||||
def test_end_incomplete_runs(
|
||||
hass_recorder: Callable[..., HomeAssistant], caplog: pytest.LogCaptureFixture
|
||||
async def testend_incomplete_runs(
|
||||
hass: HomeAssistant, setup_recorder: None, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Ensure we can end incomplete runs."""
|
||||
hass = hass_recorder()
|
||||
|
||||
with session_scope(hass=hass) as session:
|
||||
run_info = run_information_with_session(session)
|
||||
assert isinstance(run_info, RecorderRuns)
|
||||
|
@ -814,15 +825,14 @@ def test_end_incomplete_runs(
|
|||
assert "Ended unfinished session" in caplog.text
|
||||
|
||||
|
||||
def test_periodic_db_cleanups(
|
||||
hass_recorder: Callable[..., HomeAssistant], recorder_db_url
|
||||
async def testperiodic_db_cleanups(
|
||||
hass: HomeAssistant, setup_recorder: None, recorder_db_url
|
||||
) -> None:
|
||||
"""Test periodic db cleanups."""
|
||||
if recorder_db_url.startswith(("mysql://", "postgresql://")):
|
||||
# This test is specific for SQLite
|
||||
return
|
||||
|
||||
hass = hass_recorder()
|
||||
with patch.object(util.get_instance(hass).engine, "connect") as connect_mock:
|
||||
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)
|
||||
def test_execute_stmt_lambda_element(
|
||||
hass_recorder: Callable[..., HomeAssistant],
|
||||
async def testexecute_stmt_lambda_element(
|
||||
hass: HomeAssistant,
|
||||
setup_recorder: None,
|
||||
) -> None:
|
||||
"""Test executing with execute_stmt_lambda_element."""
|
||||
hass = hass_recorder()
|
||||
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")
|
||||
wait_recording_done(hass)
|
||||
await async_wait_recording_done(hass)
|
||||
now = dt_util.utcnow()
|
||||
tomorrow = now + timedelta(days=1)
|
||||
one_week_from_now = now + timedelta(days=7)
|
||||
|
|
Loading…
Reference in New Issue