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 sys
import threading import threading
from typing import Any, cast from typing import Any, cast
from unittest.mock import MagicMock, Mock, patch from unittest.mock import Mock, patch
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
@ -2575,23 +2575,25 @@ async def test_clean_shutdown_when_recorder_thread_raises_during_validate_db_sch
@pytest.mark.parametrize( @pytest.mark.parametrize(
("func_to_patch", "expected_setup_result"), ("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( 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: ) -> None:
"""Test we still shutdown cleanly when schema migration fails.""" """Test we still shutdown cleanly when schema migration fails."""
with ( with (
patch.object( patch.object(migration, "_get_current_schema_version", side_effect=[None, 1]),
migration,
"validate_db_schema",
return_value=MagicMock(valid=False, current_version=1),
),
patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True), patch("homeassistant.components.recorder.ALLOW_IN_MEMORY_DB", True),
patch.object( patch.object(
migration, migration,
func_to_patch, func_to_patch,
side_effect=Exception, side_effect=Exception("Boom!"),
), ),
): ):
if recorder.DOMAIN not in hass.data: if recorder.DOMAIN not in hass.data:
@ -2614,6 +2616,10 @@ async def test_clean_shutdown_when_schema_migration_fails(
await hass.async_stop() await hass.async_stop()
assert instance.engine is None 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( async def test_setup_fails_after_downgrade(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture