Improve recorder schema migration error test (#134518)

This commit is contained in:
Erik Montnemery 2025-01-03 10:05:07 +01:00 committed by GitHub
parent c901352bef
commit 0bd22eabc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,7 +9,7 @@ import sqlite3
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
@ -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:
@ -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(