Use async_setup_recorder_instance fixture in recorder migration tests (#121050)
parent
e7ffd7b9ad
commit
87f7703f3c
|
@ -19,7 +19,6 @@ from sqlalchemy.exc import (
|
|||
from sqlalchemy.orm import Session, scoped_session, sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.components import persistent_notification as pn, recorder
|
||||
from homeassistant.components.recorder import db_schema, migration
|
||||
from homeassistant.components.recorder.db_schema import (
|
||||
|
@ -36,6 +35,14 @@ import homeassistant.util.dt as dt_util
|
|||
from .common import async_wait_recording_done, create_engine_test
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
from tests.typing import RecorderInstanceGenerator
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mock_recorder_before_hass(
|
||||
async_setup_recorder_instance: RecorderInstanceGenerator,
|
||||
) -> None:
|
||||
"""Set up recorder."""
|
||||
|
||||
|
||||
def _get_native_states(hass, entity_id):
|
||||
|
@ -49,12 +56,13 @@ def _get_native_states(hass, entity_id):
|
|||
return states
|
||||
|
||||
|
||||
async def test_schema_update_calls(recorder_db_url: str, hass: HomeAssistant) -> None:
|
||||
async def test_schema_update_calls(
|
||||
hass: HomeAssistant, async_setup_recorder_instance: RecorderInstanceGenerator
|
||||
) -> None:
|
||||
"""Test that schema migrations occur in correct order."""
|
||||
assert recorder.util.async_migration_in_progress(hass) is False
|
||||
|
||||
with (
|
||||
patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True),
|
||||
patch(
|
||||
"homeassistant.components.recorder.core.create_engine",
|
||||
new=create_engine_test,
|
||||
|
@ -64,10 +72,7 @@ async def test_schema_update_calls(recorder_db_url: str, hass: HomeAssistant) ->
|
|||
wraps=migration._apply_update,
|
||||
) as update,
|
||||
):
|
||||
recorder_helper.async_initialize_recorder(hass)
|
||||
await async_setup_component(
|
||||
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
|
||||
)
|
||||
await async_setup_recorder_instance(hass)
|
||||
await async_wait_recording_done(hass)
|
||||
|
||||
assert recorder.util.async_migration_in_progress(hass) is False
|
||||
|
@ -82,7 +87,11 @@ async def test_schema_update_calls(recorder_db_url: str, hass: HomeAssistant) ->
|
|||
)
|
||||
|
||||
|
||||
async def test_migration_in_progress(recorder_db_url: str, hass: HomeAssistant) -> None:
|
||||
async def test_migration_in_progress(
|
||||
hass: HomeAssistant,
|
||||
recorder_db_url: str,
|
||||
async_setup_recorder_instance: RecorderInstanceGenerator,
|
||||
) -> None:
|
||||
"""Test that we can check for migration in progress."""
|
||||
if recorder_db_url.startswith("mysql://"):
|
||||
# The database drop at the end of this test currently hangs on MySQL
|
||||
|
@ -95,16 +104,12 @@ async def test_migration_in_progress(recorder_db_url: str, hass: HomeAssistant)
|
|||
assert recorder.util.async_migration_in_progress(hass) is False
|
||||
|
||||
with (
|
||||
patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True),
|
||||
patch(
|
||||
"homeassistant.components.recorder.core.create_engine",
|
||||
new=create_engine_test,
|
||||
),
|
||||
):
|
||||
recorder_helper.async_initialize_recorder(hass)
|
||||
await async_setup_component(
|
||||
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
|
||||
)
|
||||
await async_setup_recorder_instance(hass, wait_recorder=False)
|
||||
await recorder.get_instance(hass).async_migration_event.wait()
|
||||
assert recorder.util.async_migration_in_progress(hass) is True
|
||||
await async_wait_recording_done(hass)
|
||||
|
@ -114,13 +119,12 @@ async def test_migration_in_progress(recorder_db_url: str, hass: HomeAssistant)
|
|||
|
||||
|
||||
async def test_database_migration_failed(
|
||||
recorder_db_url: str, hass: HomeAssistant
|
||||
hass: HomeAssistant, async_setup_recorder_instance: RecorderInstanceGenerator
|
||||
) -> None:
|
||||
"""Test we notify if the migration fails."""
|
||||
assert recorder.util.async_migration_in_progress(hass) is False
|
||||
|
||||
with (
|
||||
patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True),
|
||||
patch(
|
||||
"homeassistant.components.recorder.core.create_engine",
|
||||
new=create_engine_test,
|
||||
|
@ -138,10 +142,7 @@ async def test_database_migration_failed(
|
|||
side_effect=pn.dismiss,
|
||||
) as mock_dismiss,
|
||||
):
|
||||
recorder_helper.async_initialize_recorder(hass)
|
||||
await async_setup_component(
|
||||
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
|
||||
)
|
||||
await async_setup_recorder_instance(hass, wait_recorder=False)
|
||||
hass.states.async_set("my.entity", "on", {})
|
||||
hass.states.async_set("my.entity", "off", {})
|
||||
await hass.async_block_till_done()
|
||||
|
@ -154,7 +155,9 @@ async def test_database_migration_failed(
|
|||
|
||||
|
||||
async def test_database_migration_encounters_corruption(
|
||||
recorder_db_url: str, hass: HomeAssistant
|
||||
hass: HomeAssistant,
|
||||
recorder_db_url: str,
|
||||
async_setup_recorder_instance: RecorderInstanceGenerator,
|
||||
) -> None:
|
||||
"""Test we move away the database if its corrupt."""
|
||||
if recorder_db_url.startswith(("mysql://", "postgresql://")):
|
||||
|
@ -168,7 +171,6 @@ async def test_database_migration_encounters_corruption(
|
|||
sqlite3_exception.__cause__ = sqlite3.DatabaseError()
|
||||
|
||||
with (
|
||||
patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True),
|
||||
patch(
|
||||
"homeassistant.components.recorder.migration._schema_is_current",
|
||||
side_effect=[False],
|
||||
|
@ -180,14 +182,8 @@ async def test_database_migration_encounters_corruption(
|
|||
patch(
|
||||
"homeassistant.components.recorder.core.move_away_broken_database"
|
||||
) as move_away,
|
||||
patch(
|
||||
"homeassistant.components.recorder.Recorder._schedule_compile_missing_statistics",
|
||||
),
|
||||
):
|
||||
recorder_helper.async_initialize_recorder(hass)
|
||||
await async_setup_component(
|
||||
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
|
||||
)
|
||||
await async_setup_recorder_instance(hass)
|
||||
hass.states.async_set("my.entity", "on", {})
|
||||
hass.states.async_set("my.entity", "off", {})
|
||||
await async_wait_recording_done(hass)
|
||||
|
@ -197,13 +193,12 @@ async def test_database_migration_encounters_corruption(
|
|||
|
||||
|
||||
async def test_database_migration_encounters_corruption_not_sqlite(
|
||||
recorder_db_url: str, hass: HomeAssistant
|
||||
hass: HomeAssistant, async_setup_recorder_instance: RecorderInstanceGenerator
|
||||
) -> None:
|
||||
"""Test we fail on database error when we cannot recover."""
|
||||
assert recorder.util.async_migration_in_progress(hass) is False
|
||||
|
||||
with (
|
||||
patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True),
|
||||
patch(
|
||||
"homeassistant.components.recorder.migration._schema_is_current",
|
||||
side_effect=[False],
|
||||
|
@ -224,10 +219,7 @@ async def test_database_migration_encounters_corruption_not_sqlite(
|
|||
side_effect=pn.dismiss,
|
||||
) as mock_dismiss,
|
||||
):
|
||||
recorder_helper.async_initialize_recorder(hass)
|
||||
await async_setup_component(
|
||||
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
|
||||
)
|
||||
await async_setup_recorder_instance(hass, wait_recorder=False)
|
||||
hass.states.async_set("my.entity", "on", {})
|
||||
hass.states.async_set("my.entity", "off", {})
|
||||
await hass.async_block_till_done()
|
||||
|
@ -241,28 +233,19 @@ async def test_database_migration_encounters_corruption_not_sqlite(
|
|||
|
||||
|
||||
async def test_events_during_migration_are_queued(
|
||||
recorder_db_url: str, hass: HomeAssistant
|
||||
hass: HomeAssistant, async_setup_recorder_instance: RecorderInstanceGenerator
|
||||
) -> None:
|
||||
"""Test that events during migration are queued."""
|
||||
|
||||
assert recorder.util.async_migration_in_progress(hass) is False
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.recorder.ALLOW_IN_MEMORY_DB",
|
||||
True,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.recorder.core.create_engine",
|
||||
new=create_engine_test,
|
||||
),
|
||||
):
|
||||
recorder_helper.async_initialize_recorder(hass)
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"recorder",
|
||||
{"recorder": {"db_url": recorder_db_url, "commit_interval": 0}},
|
||||
)
|
||||
await async_setup_recorder_instance(hass, {"commit_interval": 0})
|
||||
hass.states.async_set("my.entity", "on", {})
|
||||
hass.states.async_set("my.entity", "off", {})
|
||||
await hass.async_block_till_done()
|
||||
|
@ -280,14 +263,13 @@ async def test_events_during_migration_are_queued(
|
|||
|
||||
|
||||
async def test_events_during_migration_queue_exhausted(
|
||||
recorder_db_url: str, hass: HomeAssistant
|
||||
hass: HomeAssistant, async_setup_recorder_instance: RecorderInstanceGenerator
|
||||
) -> None:
|
||||
"""Test that events during migration takes so long the queue is exhausted."""
|
||||
|
||||
assert recorder.util.async_migration_in_progress(hass) is False
|
||||
|
||||
with (
|
||||
patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True),
|
||||
patch(
|
||||
"homeassistant.components.recorder.core.create_engine",
|
||||
new=create_engine_test,
|
||||
|
@ -295,11 +277,8 @@ async def test_events_during_migration_queue_exhausted(
|
|||
patch.object(recorder.core, "MAX_QUEUE_BACKLOG_MIN_VALUE", 1),
|
||||
patch.object(recorder.core, "QUEUE_PERCENTAGE_ALLOWED_AVAILABLE_MEMORY", 0),
|
||||
):
|
||||
recorder_helper.async_initialize_recorder(hass)
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"recorder",
|
||||
{"recorder": {"db_url": recorder_db_url, "commit_interval": 0}},
|
||||
await async_setup_recorder_instance(
|
||||
hass, {"commit_interval": 0}, wait_recorder=False
|
||||
)
|
||||
hass.states.async_set("my.entity", "on", {})
|
||||
await hass.async_block_till_done()
|
||||
|
@ -329,7 +308,11 @@ async def test_events_during_migration_queue_exhausted(
|
|||
[(0, True), (16, True), (18, True), (22, True), (25, True)],
|
||||
)
|
||||
async def test_schema_migrate(
|
||||
recorder_db_url: str, hass: HomeAssistant, start_version, live
|
||||
hass: HomeAssistant,
|
||||
recorder_db_url: str,
|
||||
async_setup_recorder_instance: RecorderInstanceGenerator,
|
||||
start_version,
|
||||
live,
|
||||
) -> None:
|
||||
"""Test the full schema migration logic.
|
||||
|
||||
|
@ -408,7 +391,6 @@ async def test_schema_migrate(
|
|||
real_create_index(*args)
|
||||
|
||||
with (
|
||||
patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True),
|
||||
patch(
|
||||
"homeassistant.components.recorder.core.create_engine",
|
||||
new=_create_engine_test,
|
||||
|
@ -431,9 +413,6 @@ async def test_schema_migrate(
|
|||
"homeassistant.components.recorder.migration._create_index",
|
||||
wraps=_sometimes_failing_create_index,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.recorder.Recorder._schedule_compile_missing_statistics",
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.recorder.Recorder._process_state_changed_event_into_session",
|
||||
),
|
||||
|
@ -444,12 +423,7 @@ async def test_schema_migrate(
|
|||
"homeassistant.components.recorder.Recorder._pre_process_startup_events",
|
||||
),
|
||||
):
|
||||
recorder_helper.async_initialize_recorder(hass)
|
||||
hass.async_create_task(
|
||||
async_setup_component(
|
||||
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
|
||||
)
|
||||
)
|
||||
await async_setup_recorder_instance(hass, wait_recorder=False)
|
||||
await recorder_helper.async_wait_recorder(hass)
|
||||
|
||||
assert recorder.util.async_migration_in_progress(hass) is True
|
||||
|
|
|
@ -1478,14 +1478,17 @@ async def async_setup_recorder_instance(
|
|||
):
|
||||
|
||||
async def async_setup_recorder(
|
||||
hass: HomeAssistant, config: ConfigType | None = None
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType | None = None,
|
||||
*,
|
||||
wait_recorder: bool = True,
|
||||
) -> recorder.Recorder:
|
||||
"""Setup and return recorder instance.""" # noqa: D401
|
||||
await _async_init_recorder_component(hass, config, recorder_db_url)
|
||||
await hass.async_block_till_done()
|
||||
instance = hass.data[recorder.DATA_INSTANCE]
|
||||
# The recorder's worker is not started until Home Assistant is running
|
||||
if hass.state is CoreState.running:
|
||||
if hass.state is CoreState.running and wait_recorder:
|
||||
await async_recorder_block_till_done(hass)
|
||||
return instance
|
||||
|
||||
|
|
Loading…
Reference in New Issue