Use async_test_recorder fixture in recorder auto_repairs tests (#121125)

pull/121139/head
Erik Montnemery 2024-07-04 01:38:01 +02:00 committed by GitHub
parent b6a23fad35
commit cc2782edc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 133 additions and 124 deletions

View File

@ -11,11 +11,18 @@ from ...common import async_wait_recording_done
from tests.typing import RecorderInstanceGenerator
@pytest.fixture
async def mock_recorder_before_hass(
async_test_recorder: RecorderInstanceGenerator,
) -> None:
"""Set up recorder."""
@pytest.mark.parametrize("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql", "postgresql"])
async def test_validate_db_schema_fix_float_issue(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
db_engine: str,
recorder_dialect_name: None,
@ -33,8 +40,8 @@ async def test_validate_db_schema_fix_float_issue(
"homeassistant.components.recorder.migration._modify_columns"
) as modify_columns_mock,
):
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
async with async_test_recorder(hass):
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert (
@ -50,8 +57,8 @@ async def test_validate_db_schema_fix_float_issue(
@pytest.mark.parametrize("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql"])
async def test_validate_db_schema_fix_utf8_issue_event_data(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
db_engine: str,
recorder_dialect_name: None,
@ -66,8 +73,8 @@ async def test_validate_db_schema_fix_utf8_issue_event_data(
return_value={"event_data.4-byte UTF-8"},
),
):
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
async with async_test_recorder(hass):
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert (
@ -83,8 +90,8 @@ async def test_validate_db_schema_fix_utf8_issue_event_data(
@pytest.mark.parametrize("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql"])
async def test_validate_db_schema_fix_collation_issue(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
db_engine: str,
recorder_dialect_name: None,
@ -99,8 +106,8 @@ async def test_validate_db_schema_fix_collation_issue(
return_value={"events.utf8mb4_unicode_ci"},
),
):
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
async with async_test_recorder(hass):
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert (

View File

@ -11,11 +11,18 @@ from ...common import async_wait_recording_done
from tests.typing import RecorderInstanceGenerator
@pytest.fixture
async def mock_recorder_before_hass(
async_test_recorder: RecorderInstanceGenerator,
) -> None:
"""Set up recorder."""
@pytest.mark.parametrize("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql", "postgresql"])
async def test_validate_db_schema_fix_float_issue(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
db_engine: str,
recorder_dialect_name: None,
@ -33,8 +40,8 @@ async def test_validate_db_schema_fix_float_issue(
"homeassistant.components.recorder.migration._modify_columns"
) as modify_columns_mock,
):
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
async with async_test_recorder(hass):
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert (
@ -52,8 +59,8 @@ async def test_validate_db_schema_fix_float_issue(
@pytest.mark.parametrize("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql"])
async def test_validate_db_schema_fix_utf8_issue_states(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
db_engine: str,
recorder_dialect_name: None,
@ -68,8 +75,8 @@ async def test_validate_db_schema_fix_utf8_issue_states(
return_value={"states.4-byte UTF-8"},
),
):
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
async with async_test_recorder(hass):
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert (
@ -84,8 +91,8 @@ async def test_validate_db_schema_fix_utf8_issue_states(
@pytest.mark.parametrize("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql"])
async def test_validate_db_schema_fix_utf8_issue_state_attributes(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
db_engine: str,
recorder_dialect_name: None,
@ -100,8 +107,8 @@ async def test_validate_db_schema_fix_utf8_issue_state_attributes(
return_value={"state_attributes.4-byte UTF-8"},
),
):
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
async with async_test_recorder(hass):
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert (
@ -117,8 +124,8 @@ async def test_validate_db_schema_fix_utf8_issue_state_attributes(
@pytest.mark.parametrize("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql"])
async def test_validate_db_schema_fix_collation_issue(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
db_engine: str,
recorder_dialect_name: None,
@ -133,8 +140,8 @@ async def test_validate_db_schema_fix_collation_issue(
return_value={"states.utf8mb4_unicode_ci"},
),
):
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
async with async_test_recorder(hass):
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert (

View File

@ -10,7 +10,7 @@ from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from homeassistant.components import recorder
from homeassistant.components.recorder import Recorder, statistics
from homeassistant.components.recorder import statistics
from homeassistant.components.recorder.auto_repairs.statistics.duplicates import (
delete_statistics_duplicates,
delete_statistics_meta_duplicates,
@ -34,15 +34,10 @@ async def mock_recorder_before_hass(
"""Set up recorder."""
@pytest.fixture
def setup_recorder(recorder_mock: Recorder) -> None:
"""Set up recorder."""
@pytest.mark.usefixtures("recorder_mock")
async def test_delete_duplicates_no_duplicates(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
setup_recorder: None,
) -> None:
"""Test removal of duplicated statistics."""
await async_wait_recording_done(hass)
@ -54,10 +49,10 @@ async def test_delete_duplicates_no_duplicates(
assert "Found duplicated" not in caplog.text
@pytest.mark.usefixtures("recorder_mock")
async def test_duplicate_statistics_handle_integrity_error(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
setup_recorder: None,
) -> None:
"""Test the recorder does not blow up if statistics is duplicated."""
await async_wait_recording_done(hass)
@ -139,7 +134,7 @@ def _create_engine_28(*args, **kwargs):
async def test_delete_metadata_duplicates(
async_setup_recorder_instance: RecorderInstanceGenerator,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None:
@ -205,8 +200,10 @@ async def test_delete_metadata_duplicates(
new=_create_engine_28,
),
):
async with async_test_home_assistant() as hass:
await async_setup_recorder_instance(hass, {"db_url": dburl})
async with (
async_test_home_assistant() as hass,
async_test_recorder(hass, {"db_url": dburl}),
):
await async_wait_recording_done(hass)
await async_wait_recording_done(hass)
@ -225,8 +222,10 @@ async def test_delete_metadata_duplicates(
await hass.async_stop()
# Test that the duplicates are removed during migration from schema 28
async with async_test_home_assistant() as hass:
await async_setup_recorder_instance(hass, {"db_url": dburl})
async with (
async_test_home_assistant() as hass,
async_test_recorder(hass, {"db_url": dburl}),
):
await hass.async_start()
await async_wait_recording_done(hass)
await async_wait_recording_done(hass)
@ -244,7 +243,7 @@ async def test_delete_metadata_duplicates(
async def test_delete_metadata_duplicates_many(
async_setup_recorder_instance: RecorderInstanceGenerator,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
) -> None:
@ -322,8 +321,10 @@ async def test_delete_metadata_duplicates_many(
new=_create_engine_28,
),
):
async with async_test_home_assistant() as hass:
await async_setup_recorder_instance(hass, {"db_url": dburl})
async with (
async_test_home_assistant() as hass,
async_test_recorder(hass, {"db_url": dburl}),
):
await async_wait_recording_done(hass)
await async_wait_recording_done(hass)
@ -333,8 +334,10 @@ async def test_delete_metadata_duplicates_many(
await hass.async_stop()
# Test that the duplicates are removed during migration from schema 28
async with async_test_home_assistant() as hass:
await async_setup_recorder_instance(hass, {"db_url": dburl})
async with (
async_test_home_assistant() as hass,
async_test_recorder(hass, {"db_url": dburl}),
):
await hass.async_start()
await async_wait_recording_done(hass)
await async_wait_recording_done(hass)
@ -353,8 +356,9 @@ async def test_delete_metadata_duplicates_many(
await hass.async_stop()
@pytest.mark.usefixtures("recorder_mock")
async def test_delete_metadata_duplicates_no_duplicates(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, setup_recorder: None
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test removal of duplicated statistics."""
await async_wait_recording_done(hass)

View File

@ -11,11 +11,18 @@ from ...common import async_wait_recording_done
from tests.typing import RecorderInstanceGenerator
@pytest.fixture
async def mock_recorder_before_hass(
async_test_recorder: RecorderInstanceGenerator,
) -> None:
"""Set up recorder."""
@pytest.mark.parametrize("db_engine", ["mysql"])
@pytest.mark.parametrize("enable_schema_validation", [True])
async def test_validate_db_schema_fix_utf8_issue(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
db_engine: str,
recorder_dialect_name: None,
@ -30,8 +37,8 @@ async def test_validate_db_schema_fix_utf8_issue(
return_value={"statistics_meta.4-byte UTF-8"},
),
):
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
async with async_test_recorder(hass):
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert (
@ -48,8 +55,8 @@ async def test_validate_db_schema_fix_utf8_issue(
@pytest.mark.parametrize("table", ["statistics_short_term", "statistics"])
@pytest.mark.parametrize("db_engine", ["mysql", "postgresql"])
async def test_validate_db_schema_fix_float_issue(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
table: str,
db_engine: str,
@ -68,8 +75,8 @@ async def test_validate_db_schema_fix_float_issue(
"homeassistant.components.recorder.migration._modify_columns"
) as modify_columns_mock,
):
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
async with async_test_recorder(hass):
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert (
@ -92,8 +99,8 @@ async def test_validate_db_schema_fix_float_issue(
@pytest.mark.parametrize("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql"])
async def test_validate_db_schema_fix_collation_issue(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture,
recorder_dialect_name: None,
db_engine: str,
@ -108,8 +115,8 @@ async def test_validate_db_schema_fix_collation_issue(
return_value={"statistics.utf8mb4_unicode_ci"},
),
):
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
async with async_test_recorder(hass):
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert (

View File

@ -3,6 +3,7 @@
import pytest
from sqlalchemy import text
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.auto_repairs.schema import (
correct_db_schema_precision,
correct_db_schema_utf8,
@ -12,7 +13,7 @@ from homeassistant.components.recorder.auto_repairs.schema import (
)
from homeassistant.components.recorder.db_schema import States
from homeassistant.components.recorder.migration import _modify_columns
from homeassistant.components.recorder.util import get_instance, session_scope
from homeassistant.components.recorder.util import session_scope
from homeassistant.core import HomeAssistant
from ..common import async_wait_recording_done
@ -20,11 +21,18 @@ from ..common import async_wait_recording_done
from tests.typing import RecorderInstanceGenerator
@pytest.fixture
async def mock_recorder_before_hass(
async_test_recorder: RecorderInstanceGenerator,
) -> None:
"""Set up recorder."""
@pytest.mark.parametrize("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql", "postgresql"])
async def test_validate_db_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture,
db_engine: str,
recorder_dialect_name: None,
@ -33,7 +41,6 @@ async def test_validate_db_schema(
Note: The test uses SQLite, the purpose is only to exercise the code.
"""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text
assert "Detected statistics schema errors" not in caplog.text
@ -43,17 +50,14 @@ async def test_validate_db_schema(
@pytest.mark.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_fix_utf8_issue_good_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
recorder_db_url: str,
recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test validating DB schema with MySQL when the schema is correct."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
instance = get_instance(hass)
schema_errors = await instance.async_add_executor_job(
validate_table_schema_supports_utf8, instance, States, (States.state,)
schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_supports_utf8, recorder_mock, States, (States.state,)
)
assert schema_errors == set()
@ -61,16 +65,13 @@ async def test_validate_db_schema_fix_utf8_issue_good_schema(
@pytest.mark.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_fix_utf8_issue_with_broken_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
recorder_db_url: str,
recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test validating DB schema with MySQL when the schema is broken and repairing it."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
instance = get_instance(hass)
session_maker = instance.get_session
session_maker = recorder_mock.get_session
def _break_states_schema():
with session_scope(session=session_maker()) as session:
@ -82,20 +83,20 @@ async def test_validate_db_schema_fix_utf8_issue_with_broken_schema(
)
)
await instance.async_add_executor_job(_break_states_schema)
schema_errors = await instance.async_add_executor_job(
validate_table_schema_supports_utf8, instance, States, (States.state,)
await recorder_mock.async_add_executor_job(_break_states_schema)
schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_supports_utf8, recorder_mock, States, (States.state,)
)
assert schema_errors == {"states.4-byte UTF-8"}
# Now repair the schema
await instance.async_add_executor_job(
correct_db_schema_utf8, instance, States, schema_errors
await recorder_mock.async_add_executor_job(
correct_db_schema_utf8, recorder_mock, States, schema_errors
)
# Now validate the schema again
schema_errors = await instance.async_add_executor_job(
validate_table_schema_supports_utf8, instance, States, ("state",)
schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_supports_utf8, recorder_mock, States, ("state",)
)
assert schema_errors == set()
@ -103,16 +104,13 @@ async def test_validate_db_schema_fix_utf8_issue_with_broken_schema(
@pytest.mark.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_fix_incorrect_collation(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
recorder_db_url: str,
recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test validating DB schema with MySQL when the collation is incorrect."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
instance = get_instance(hass)
session_maker = instance.get_session
session_maker = recorder_mock.get_session
def _break_states_schema():
with session_scope(session=session_maker()) as session:
@ -123,20 +121,20 @@ async def test_validate_db_schema_fix_incorrect_collation(
)
)
await instance.async_add_executor_job(_break_states_schema)
schema_errors = await instance.async_add_executor_job(
validate_table_schema_has_correct_collation, instance, States
await recorder_mock.async_add_executor_job(_break_states_schema)
schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_has_correct_collation, recorder_mock, States
)
assert schema_errors == {"states.utf8mb4_unicode_ci"}
# Now repair the schema
await instance.async_add_executor_job(
correct_db_schema_utf8, instance, States, schema_errors
await recorder_mock.async_add_executor_job(
correct_db_schema_utf8, recorder_mock, States, schema_errors
)
# Now validate the schema again
schema_errors = await instance.async_add_executor_job(
validate_table_schema_has_correct_collation, instance, States
schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_has_correct_collation, recorder_mock, States
)
assert schema_errors == set()
@ -144,18 +142,15 @@ async def test_validate_db_schema_fix_incorrect_collation(
@pytest.mark.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_precision_correct_collation(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
recorder_db_url: str,
recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test validating DB schema when the schema is correct with the correct collation."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
instance = get_instance(hass)
schema_errors = await instance.async_add_executor_job(
schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_has_correct_collation,
instance,
recorder_mock,
States,
)
assert schema_errors == set()
@ -164,16 +159,13 @@ async def test_validate_db_schema_precision_correct_collation(
@pytest.mark.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_fix_utf8_issue_with_broken_schema_unrepairable(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
recorder_db_url: str,
recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test validating DB schema with MySQL when the schema is broken and cannot be repaired."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
instance = get_instance(hass)
session_maker = instance.get_session
session_maker = recorder_mock.get_session
def _break_states_schema():
with session_scope(session=session_maker()) as session:
@ -186,16 +178,16 @@ async def test_validate_db_schema_fix_utf8_issue_with_broken_schema_unrepairable
)
_modify_columns(
session_maker,
instance.engine,
recorder_mock.engine,
"states",
[
"entity_id VARCHAR(255) NOT NULL",
],
)
await instance.async_add_executor_job(_break_states_schema)
schema_errors = await instance.async_add_executor_job(
validate_table_schema_supports_utf8, instance, States, ("state",)
await recorder_mock.async_add_executor_job(_break_states_schema)
schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_supports_utf8, recorder_mock, States, ("state",)
)
assert schema_errors == set()
assert "Error when validating DB schema" in caplog.text
@ -204,18 +196,15 @@ async def test_validate_db_schema_fix_utf8_issue_with_broken_schema_unrepairable
@pytest.mark.skip_on_db_engine(["sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_precision_good_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
recorder_db_url: str,
recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test validating DB schema when the schema is correct."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
instance = get_instance(hass)
schema_errors = await instance.async_add_executor_job(
schema_errors = await recorder_mock.async_add_executor_job(
validate_db_schema_precision,
instance,
recorder_mock,
States,
)
assert schema_errors == set()
@ -224,21 +213,18 @@ async def test_validate_db_schema_precision_good_schema(
@pytest.mark.skip_on_db_engine(["sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_precision_with_broken_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
recorder_db_url: str,
recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test validating DB schema when the schema is broken and than repair it."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
instance = get_instance(hass)
session_maker = instance.get_session
session_maker = recorder_mock.get_session
def _break_states_schema():
_modify_columns(
session_maker,
instance.engine,
recorder_mock.engine,
"states",
[
"last_updated_ts FLOAT(4)",
@ -246,23 +232,23 @@ async def test_validate_db_schema_precision_with_broken_schema(
],
)
await instance.async_add_executor_job(_break_states_schema)
schema_errors = await instance.async_add_executor_job(
await recorder_mock.async_add_executor_job(_break_states_schema)
schema_errors = await recorder_mock.async_add_executor_job(
validate_db_schema_precision,
instance,
recorder_mock,
States,
)
assert schema_errors == {"states.double precision"}
# Now repair the schema
await instance.async_add_executor_job(
correct_db_schema_precision, instance, States, schema_errors
await recorder_mock.async_add_executor_job(
correct_db_schema_precision, recorder_mock, States, schema_errors
)
# Now validate the schema again
schema_errors = await instance.async_add_executor_job(
schema_errors = await recorder_mock.async_add_executor_job(
validate_db_schema_precision,
instance,
recorder_mock,
States,
)
assert schema_errors == set()
@ -271,21 +257,19 @@ async def test_validate_db_schema_precision_with_broken_schema(
@pytest.mark.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_precision_with_unrepairable_broken_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant,
recorder_mock: Recorder,
recorder_db_url: str,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test validating DB schema when the schema is broken and cannot be repaired."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass)
instance = get_instance(hass)
session_maker = instance.get_session
session_maker = recorder_mock.get_session
def _break_states_schema():
_modify_columns(
session_maker,
instance.engine,
recorder_mock.engine,
"states",
[
"state VARCHAR(255) NOT NULL",
@ -294,10 +278,10 @@ async def test_validate_db_schema_precision_with_unrepairable_broken_schema(
],
)
await instance.async_add_executor_job(_break_states_schema)
schema_errors = await instance.async_add_executor_job(
await recorder_mock.async_add_executor_job(_break_states_schema)
schema_errors = await recorder_mock.async_add_executor_job(
validate_db_schema_precision,
instance,
recorder_mock,
States,
)
assert "Error when validating DB schema" in caplog.text