Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve recorder schema migration error test #134518

Merged
merged 1 commit into from
Jan 3, 2025
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
30 changes: 18 additions & 12 deletions tests/components/recorder/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
import threading
from typing import Any, cast
from unittest.mock import MagicMock, Mock, patch
from unittest.mock import Mock, patch

from freezegun.api import FrozenDateTimeFactory
import pytest
Expand Down Expand Up @@ -2575,23 +2575,25 @@ async def test_clean_shutdown_when_recorder_thread_raises_during_validate_db_sch

@pytest.mark.parametrize(
("func_to_patch", "expected_setup_result"),
[("migrate_schema_non_live", False), ("migrate_schema_live", False)],
[
("migrate_schema_non_live", False),
("migrate_schema_live", True),
],
)
async def test_clean_shutdown_when_schema_migration_fails(
hass: HomeAssistant, func_to_patch: str, expected_setup_result: bool
hass: HomeAssistant,
func_to_patch: str,
expected_setup_result: bool,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test we still shutdown cleanly when schema migration fails."""
with (
patch.object(
migration,
"validate_db_schema",
return_value=MagicMock(valid=False, current_version=1),
),
patch.object(migration, "_get_current_schema_version", side_effect=[None, 1]),
patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True),
patch.object(
migration,
func_to_patch,
side_effect=Exception,
side_effect=Exception("Boom!"),
),
):
if recorder.DOMAIN not in hass.data:
Expand All @@ -2610,9 +2612,13 @@ async def test_clean_shutdown_when_schema_migration_fails(
assert setup_result == expected_setup_result
await hass.async_block_till_done()

instance = recorder.get_instance(hass)
await hass.async_stop()
assert instance.engine is None
instance = recorder.get_instance(hass)
await hass.async_stop()
assert instance.engine is None

assert "Error during schema migration" in caplog.text
# Check the injected exception was logged
assert "Boom!" in caplog.text


async def test_setup_fails_after_downgrade(
Expand Down