Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions tests/components/recorder/test_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _get_native_states(hass, entity_id):
]


async def test_schema_update_calls(hass: HomeAssistant) -> None:
async def test_schema_update_calls(hass: HomeAssistant, recorder_db_url: str) -> None:
"""Test that schema migrations occur in correct order."""
assert recorder.util.async_migration_in_progress(hass) is False

Expand All @@ -59,7 +59,7 @@ async def test_schema_update_calls(hass: HomeAssistant) -> None:
) as update:
recorder_helper.async_initialize_recorder(hass)
await async_setup_component(
hass, "recorder", {"recorder": {"db_url": "sqlite://"}}
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
)
await async_wait_recording_done(hass)

Expand All @@ -75,7 +75,7 @@ async def test_schema_update_calls(hass: HomeAssistant) -> None:
)


async def test_migration_in_progress(hass: HomeAssistant) -> None:
async def test_migration_in_progress(hass: HomeAssistant, recorder_db_url: str) -> None:
"""Test that we can check for migration in progress."""
assert recorder.util.async_migration_in_progress(hass) is False

Expand All @@ -85,7 +85,7 @@ async def test_migration_in_progress(hass: HomeAssistant) -> None:
):
recorder_helper.async_initialize_recorder(hass)
await async_setup_component(
hass, "recorder", {"recorder": {"db_url": "sqlite://"}}
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
)
await recorder.get_instance(hass).async_migration_event.wait()
assert recorder.util.async_migration_in_progress(hass) is True
Expand All @@ -95,7 +95,9 @@ async def test_migration_in_progress(hass: HomeAssistant) -> None:
assert recorder.get_instance(hass).schema_version == SCHEMA_VERSION


async def test_database_migration_failed(hass: HomeAssistant) -> None:
async def test_database_migration_failed(
hass: HomeAssistant, recorder_db_url: str
) -> None:
"""Test we notify if the migration fails."""
assert recorder.util.async_migration_in_progress(hass) is False

Expand All @@ -113,7 +115,7 @@ async def test_database_migration_failed(hass: HomeAssistant) -> None:
) as mock_dismiss:
recorder_helper.async_initialize_recorder(hass)
await async_setup_component(
hass, "recorder", {"recorder": {"db_url": "sqlite://"}}
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
)
hass.states.async_set("my.entity", "on", {})
hass.states.async_set("my.entity", "off", {})
Expand All @@ -126,8 +128,14 @@ async def test_database_migration_failed(hass: HomeAssistant) -> None:
assert len(mock_dismiss.mock_calls) == 1


async def test_database_migration_encounters_corruption(hass: HomeAssistant) -> None:
async def test_database_migration_encounters_corruption(
hass: HomeAssistant, recorder_db_url: str
) -> None:
"""Test we move away the database if its corrupt."""
if recorder_db_url.startswith(("mysql://", "postgresql://")):
# This test is specific for SQLite, wiping the database on error only happens
# with SQLite.
return

assert recorder.util.async_migration_in_progress(hass) is False

Expand All @@ -147,7 +155,7 @@ async def test_database_migration_encounters_corruption(hass: HomeAssistant) ->
):
recorder_helper.async_initialize_recorder(hass)
await async_setup_component(
hass, "recorder", {"recorder": {"db_url": "sqlite://"}}
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
)
hass.states.async_set("my.entity", "on", {})
hass.states.async_set("my.entity", "off", {})
Expand All @@ -158,7 +166,7 @@ async def test_database_migration_encounters_corruption(hass: HomeAssistant) ->


async def test_database_migration_encounters_corruption_not_sqlite(
hass: HomeAssistant,
hass: HomeAssistant, recorder_db_url: str
) -> None:
"""Test we fail on database error when we cannot recover."""
assert recorder.util.async_migration_in_progress(hass) is False
Expand All @@ -179,7 +187,7 @@ async def test_database_migration_encounters_corruption_not_sqlite(
) as mock_dismiss:
recorder_helper.async_initialize_recorder(hass)
await async_setup_component(
hass, "recorder", {"recorder": {"db_url": "sqlite://"}}
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
)
hass.states.async_set("my.entity", "on", {})
hass.states.async_set("my.entity", "off", {})
Expand All @@ -193,7 +201,9 @@ async def test_database_migration_encounters_corruption_not_sqlite(
assert len(mock_dismiss.mock_calls) == 1


async def test_events_during_migration_are_queued(hass: HomeAssistant) -> None:
async def test_events_during_migration_are_queued(
hass: HomeAssistant, recorder_db_url: str
) -> None:
"""Test that events during migration are queued."""

assert recorder.util.async_migration_in_progress(hass) is False
Expand All @@ -209,7 +219,7 @@ async def test_events_during_migration_are_queued(hass: HomeAssistant) -> None:
await async_setup_component(
hass,
"recorder",
{"recorder": {"db_url": "sqlite://", "commit_interval": 0}},
{"recorder": {"db_url": recorder_db_url, "commit_interval": 0}},
)
hass.states.async_set("my.entity", "on", {})
hass.states.async_set("my.entity", "off", {})
Expand All @@ -227,7 +237,9 @@ async def test_events_during_migration_are_queued(hass: HomeAssistant) -> None:
assert len(db_states) == 2


async def test_events_during_migration_queue_exhausted(hass: HomeAssistant) -> None:
async def test_events_during_migration_queue_exhausted(
hass: HomeAssistant, recorder_db_url: str
) -> None:
"""Test that events during migration takes so long the queue is exhausted."""

assert recorder.util.async_migration_in_progress(hass) is False
Expand All @@ -240,7 +252,7 @@ async def test_events_during_migration_queue_exhausted(hass: HomeAssistant) -> N
await async_setup_component(
hass,
"recorder",
{"recorder": {"db_url": "sqlite://", "commit_interval": 0}},
{"recorder": {"db_url": recorder_db_url, "commit_interval": 0}},
)
hass.states.async_set("my.entity", "on", {})
await hass.async_block_till_done()
Expand Down Expand Up @@ -269,7 +281,9 @@ async def test_events_during_migration_queue_exhausted(hass: HomeAssistant) -> N
"start_version,live",
[(0, True), (16, True), (18, True), (22, True), (25, True)],
)
async def test_schema_migrate(hass, start_version, live):
async def test_schema_migrate(
hass: HomeAssistant, recorder_db_url: str, start_version, live
):
"""Test the full schema migration logic.

We're just testing that the logic can execute successfully here without
Expand Down Expand Up @@ -352,7 +366,7 @@ def _instrument_apply_update(*args):
recorder_helper.async_initialize_recorder(hass)
hass.async_create_task(
async_setup_component(
hass, "recorder", {"recorder": {"db_url": "sqlite://"}}
hass, "recorder", {"recorder": {"db_url": recorder_db_url}}
)
)
await recorder_helper.async_wait_recorder(hass)
Expand Down Expand Up @@ -401,9 +415,9 @@ def test_modify_column(engine_type, substr):
assert not connection.execute.called


def test_forgiving_add_column() -> None:
def test_forgiving_add_column(recorder_db_url: str) -> None:
"""Test that add column will continue if column exists."""
engine = create_engine("sqlite://", poolclass=StaticPool)
engine = create_engine(recorder_db_url, poolclass=StaticPool)
with Session(engine) as session:
session.execute(text("CREATE TABLE hello (id int)"))
instance = Mock()
Expand All @@ -416,9 +430,9 @@ def test_forgiving_add_column() -> None:
)


def test_forgiving_add_index() -> None:
def test_forgiving_add_index(recorder_db_url: str) -> None:
"""Test that add index will continue if index exists."""
engine = create_engine("sqlite://", poolclass=StaticPool)
engine = create_engine(recorder_db_url, poolclass=StaticPool)
db_schema.Base.metadata.create_all(engine)
with Session(engine) as session:
instance = Mock()
Expand Down