Use recorder test fixtures in tests (#70652)

This commit is contained in:
Erik Montnemery 2022-04-25 14:23:52 +02:00 committed by GitHub
parent 09b4b7eb37
commit d045e8678d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 30 deletions

View file

@ -56,7 +56,6 @@ from .common import async_wait_recording_done, corrupt_db_file, wait_recording_d
from tests.common import ( from tests.common import (
SetupRecorderInstanceT, SetupRecorderInstanceT,
async_fire_time_changed, async_fire_time_changed,
async_init_recorder_component,
fire_time_changed, fire_time_changed,
get_test_home_assistant, get_test_home_assistant,
) )
@ -79,7 +78,9 @@ def _default_recorder(hass):
) )
async def test_shutdown_before_startup_finishes(hass, tmp_path): async def test_shutdown_before_startup_finishes(
hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT, tmp_path
):
"""Test shutdown before recorder starts is clean.""" """Test shutdown before recorder starts is clean."""
# On-disk database because this test does not play nice with the # On-disk database because this test does not play nice with the
@ -90,7 +91,7 @@ async def test_shutdown_before_startup_finishes(hass, tmp_path):
} }
hass.state = CoreState.not_running hass.state = CoreState.not_running
await async_init_recorder_component(hass, config) await async_setup_recorder_instance(hass, config)
await hass.data[DATA_INSTANCE].async_db_ready await hass.data[DATA_INSTANCE].async_db_ready
await hass.async_block_till_done() await hass.async_block_till_done()
@ -138,7 +139,7 @@ async def test_state_gets_saved_when_set_before_start_event(
hass.state = CoreState.not_running hass.state = CoreState.not_running
await async_init_recorder_component(hass) await async_setup_recorder_instance(hass)
entity_id = "test.recorder" entity_id = "test.recorder"
state = "restoring_from_db" state = "restoring_from_db"
@ -156,12 +157,8 @@ async def test_state_gets_saved_when_set_before_start_event(
assert db_states[0].event_id > 0 assert db_states[0].event_id > 0
async def test_saving_state( async def test_saving_state(hass: HomeAssistant, recorder_mock):
hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT
):
"""Test saving and restoring a state.""" """Test saving and restoring a state."""
await async_setup_recorder_instance(hass)
entity_id = "test.recorder" entity_id = "test.recorder"
state = "restoring_from_db" state = "restoring_from_db"
attributes = {"test_attr": 5, "test_attr_10": "nice"} attributes = {"test_attr": 5, "test_attr_10": "nice"}
@ -209,11 +206,9 @@ async def test_saving_many_states(
async def test_saving_state_with_intermixed_time_changes( async def test_saving_state_with_intermixed_time_changes(
hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT hass: HomeAssistant, recorder_mock
): ):
"""Test saving states with intermixed time changes.""" """Test saving states with intermixed time changes."""
await async_setup_recorder_instance(hass)
entity_id = "test.recorder" entity_id = "test.recorder"
state = "restoring_from_db" state = "restoring_from_db"
attributes = {"test_attr": 5, "test_attr_10": "nice"} attributes = {"test_attr": 5, "test_attr_10": "nice"}
@ -1297,14 +1292,16 @@ def test_entity_id_filter(hass_recorder):
assert len(db_events) == idx + 1, data assert len(db_events) == idx + 1, data
async def test_database_lock_and_unlock(hass: HomeAssistant, tmp_path): async def test_database_lock_and_unlock(
hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT, tmp_path
):
"""Test writing events during lock getting written after unlocking.""" """Test writing events during lock getting written after unlocking."""
# Use file DB, in memory DB cannot do write locks. # Use file DB, in memory DB cannot do write locks.
config = { config = {
recorder.CONF_COMMIT_INTERVAL: 0, recorder.CONF_COMMIT_INTERVAL: 0,
recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"), recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"),
} }
await async_init_recorder_component(hass, config) await async_setup_recorder_instance(hass, config)
await hass.async_block_till_done() await hass.async_block_till_done()
instance: Recorder = hass.data[DATA_INSTANCE] instance: Recorder = hass.data[DATA_INSTANCE]
@ -1334,14 +1331,16 @@ async def test_database_lock_and_unlock(hass: HomeAssistant, tmp_path):
assert len(db_events) == 1 assert len(db_events) == 1
async def test_database_lock_and_overflow(hass: HomeAssistant, tmp_path): async def test_database_lock_and_overflow(
hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT, tmp_path
):
"""Test writing events during lock leading to overflow the queue causes the database to unlock.""" """Test writing events during lock leading to overflow the queue causes the database to unlock."""
# Use file DB, in memory DB cannot do write locks. # Use file DB, in memory DB cannot do write locks.
config = { config = {
recorder.CONF_COMMIT_INTERVAL: 0, recorder.CONF_COMMIT_INTERVAL: 0,
recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"), recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"),
} }
await async_init_recorder_component(hass, config) await async_setup_recorder_instance(hass, config)
await hass.async_block_till_done() await hass.async_block_till_done()
instance: Recorder = hass.data[DATA_INSTANCE] instance: Recorder = hass.data[DATA_INSTANCE]
@ -1366,9 +1365,8 @@ async def test_database_lock_and_overflow(hass: HomeAssistant, tmp_path):
assert not instance.unlock_database() assert not instance.unlock_database()
async def test_database_lock_timeout(hass): async def test_database_lock_timeout(hass, recorder_mock):
"""Test locking database timeout when recorder stopped.""" """Test locking database timeout when recorder stopped."""
await async_init_recorder_component(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
instance: Recorder = hass.data[DATA_INSTANCE] instance: Recorder = hass.data[DATA_INSTANCE]
@ -1390,9 +1388,8 @@ async def test_database_lock_timeout(hass):
block_task.event.set() block_task.event.set()
async def test_database_lock_without_instance(hass): async def test_database_lock_without_instance(hass, recorder_mock):
"""Test database lock doesn't fail if instance is not initialized.""" """Test database lock doesn't fail if instance is not initialized."""
await async_init_recorder_component(hass)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
instance: Recorder = hass.data[DATA_INSTANCE] instance: Recorder = hass.data[DATA_INSTANCE]

View file

@ -18,11 +18,12 @@ from homeassistant.components.recorder.util import (
session_scope, session_scope,
) )
from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from .common import corrupt_db_file from .common import corrupt_db_file
from tests.common import async_init_recorder_component, async_test_home_assistant from tests.common import SetupRecorderInstanceT, async_test_home_assistant
def test_session_scope_not_setup(hass_recorder): def test_session_scope_not_setup(hass_recorder):
@ -96,7 +97,9 @@ def test_validate_or_move_away_sqlite_database(hass, tmpdir, caplog):
assert util.validate_or_move_away_sqlite_database(dburl) is True assert util.validate_or_move_away_sqlite_database(dburl) is True
async def test_last_run_was_recently_clean(hass, tmp_path): async def test_last_run_was_recently_clean(
loop, async_setup_recorder_instance: SetupRecorderInstanceT, tmp_path
):
"""Test we can check if the last recorder run was recently clean.""" """Test we can check if the last recorder run was recently clean."""
config = { config = {
recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"), recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"),
@ -116,7 +119,7 @@ async def test_last_run_was_recently_clean(hass, tmp_path):
"homeassistant.components.recorder.util.last_run_was_recently_clean", "homeassistant.components.recorder.util.last_run_was_recently_clean",
wraps=_last_run_was_recently_clean, wraps=_last_run_was_recently_clean,
) as last_run_was_recently_clean_mock: ) as last_run_was_recently_clean_mock:
await async_init_recorder_component(hass, config) await async_setup_recorder_instance(hass, config)
await hass.async_block_till_done() await hass.async_block_till_done()
last_run_was_recently_clean_mock.assert_not_called() last_run_was_recently_clean_mock.assert_not_called()
@ -130,7 +133,7 @@ async def test_last_run_was_recently_clean(hass, tmp_path):
wraps=_last_run_was_recently_clean, wraps=_last_run_was_recently_clean,
) as last_run_was_recently_clean_mock: ) as last_run_was_recently_clean_mock:
hass = await async_test_home_assistant(None) hass = await async_test_home_assistant(None)
await async_init_recorder_component(hass, config) await async_setup_recorder_instance(hass, config)
last_run_was_recently_clean_mock.assert_called_once() last_run_was_recently_clean_mock.assert_called_once()
assert return_values[-1] is True assert return_values[-1] is True
@ -149,7 +152,7 @@ async def test_last_run_was_recently_clean(hass, tmp_path):
return_value=thirty_min_future_time, return_value=thirty_min_future_time,
): ):
hass = await async_test_home_assistant(None) hass = await async_test_home_assistant(None)
await async_init_recorder_component(hass, config) await async_setup_recorder_instance(hass, config)
last_run_was_recently_clean_mock.assert_called_once() last_run_was_recently_clean_mock.assert_called_once()
assert return_values[-1] is False assert return_values[-1] is False
@ -594,7 +597,9 @@ def test_periodic_db_cleanups(hass_recorder):
assert str(text_obj) == "PRAGMA wal_checkpoint(TRUNCATE);" assert str(text_obj) == "PRAGMA wal_checkpoint(TRUNCATE);"
async def test_write_lock_db(hass, tmp_path): async def test_write_lock_db(
hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT, tmp_path
):
"""Test database write lock.""" """Test database write lock."""
from sqlalchemy.exc import OperationalError from sqlalchemy.exc import OperationalError
@ -602,7 +607,7 @@ async def test_write_lock_db(hass, tmp_path):
config = { config = {
recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db?timeout=0.1") recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db?timeout=0.1")
} }
await async_init_recorder_component(hass, config) await async_setup_recorder_instance(hass, config)
await hass.async_block_till_done() await hass.async_block_till_done()
instance = hass.data[DATA_INSTANCE] instance = hass.data[DATA_INSTANCE]

View file

@ -2,12 +2,15 @@
from unittest.mock import patch from unittest.mock import patch
from homeassistant.core import HomeAssistant
from homeassistant.helpers import recorder from homeassistant.helpers import recorder
from tests.common import async_init_recorder_component from tests.common import SetupRecorderInstanceT
async def test_async_migration_in_progress(hass): async def test_async_migration_in_progress(
hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT
):
"""Test async_migration_in_progress wraps the recorder.""" """Test async_migration_in_progress wraps the recorder."""
with patch( with patch(
"homeassistant.components.recorder.util.async_migration_in_progress", "homeassistant.components.recorder.util.async_migration_in_progress",
@ -22,7 +25,7 @@ async def test_async_migration_in_progress(hass):
): ):
assert recorder.async_migration_in_progress(hass) is False assert recorder.async_migration_in_progress(hass) is False
await async_init_recorder_component(hass) await async_setup_recorder_instance(hass)
# The recorder is now loaded # The recorder is now loaded
with patch( with patch(