Use async_test_recorder fixture in recorder auto_repairs tests (#121125)

This commit is contained in:
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 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("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql", "postgresql"]) @pytest.mark.parametrize("db_engine", ["mysql", "postgresql"])
async def test_validate_db_schema_fix_float_issue( async def test_validate_db_schema_fix_float_issue(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
db_engine: str, db_engine: str,
recorder_dialect_name: None, recorder_dialect_name: None,
@ -33,7 +40,7 @@ async def test_validate_db_schema_fix_float_issue(
"homeassistant.components.recorder.migration._modify_columns" "homeassistant.components.recorder.migration._modify_columns"
) as modify_columns_mock, ) as modify_columns_mock,
): ):
await async_setup_recorder_instance(hass) async with async_test_recorder(hass):
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text assert "Schema validation failed" not in caplog.text
@ -50,8 +57,8 @@ async def test_validate_db_schema_fix_float_issue(
@pytest.mark.parametrize("enable_schema_validation", [True]) @pytest.mark.parametrize("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql"]) @pytest.mark.parametrize("db_engine", ["mysql"])
async def test_validate_db_schema_fix_utf8_issue_event_data( async def test_validate_db_schema_fix_utf8_issue_event_data(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
db_engine: str, db_engine: str,
recorder_dialect_name: None, recorder_dialect_name: None,
@ -66,7 +73,7 @@ async def test_validate_db_schema_fix_utf8_issue_event_data(
return_value={"event_data.4-byte UTF-8"}, return_value={"event_data.4-byte UTF-8"},
), ),
): ):
await async_setup_recorder_instance(hass) async with async_test_recorder(hass):
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text assert "Schema validation failed" not in caplog.text
@ -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("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql"]) @pytest.mark.parametrize("db_engine", ["mysql"])
async def test_validate_db_schema_fix_collation_issue( async def test_validate_db_schema_fix_collation_issue(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
async_test_recorder: RecorderInstanceGenerator,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
db_engine: str, db_engine: str,
recorder_dialect_name: None, recorder_dialect_name: None,
@ -99,7 +106,7 @@ async def test_validate_db_schema_fix_collation_issue(
return_value={"events.utf8mb4_unicode_ci"}, return_value={"events.utf8mb4_unicode_ci"},
), ),
): ):
await async_setup_recorder_instance(hass) async with async_test_recorder(hass):
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text assert "Schema validation failed" not in caplog.text

View File

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

View File

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

View File

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

View File

@ -3,6 +3,7 @@
import pytest import pytest
from sqlalchemy import text from sqlalchemy import text
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.auto_repairs.schema import ( from homeassistant.components.recorder.auto_repairs.schema import (
correct_db_schema_precision, correct_db_schema_precision,
correct_db_schema_utf8, 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.db_schema import States
from homeassistant.components.recorder.migration import _modify_columns 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 homeassistant.core import HomeAssistant
from ..common import async_wait_recording_done from ..common import async_wait_recording_done
@ -20,11 +21,18 @@ from ..common import async_wait_recording_done
from tests.typing import RecorderInstanceGenerator 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("enable_schema_validation", [True])
@pytest.mark.parametrize("db_engine", ["mysql", "postgresql"]) @pytest.mark.parametrize("db_engine", ["mysql", "postgresql"])
async def test_validate_db_schema( async def test_validate_db_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
db_engine: str, db_engine: str,
recorder_dialect_name: None, 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. 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) await async_wait_recording_done(hass)
assert "Schema validation failed" not in caplog.text assert "Schema validation failed" not in caplog.text
assert "Detected statistics schema errors" 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.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine") @pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_fix_utf8_issue_good_schema( async def test_validate_db_schema_fix_utf8_issue_good_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
recorder_db_url: str, recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test validating DB schema with MySQL when the schema is correct.""" """Test validating DB schema with MySQL when the schema is correct."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
instance = get_instance(hass) schema_errors = await recorder_mock.async_add_executor_job(
schema_errors = await instance.async_add_executor_job( validate_table_schema_supports_utf8, recorder_mock, States, (States.state,)
validate_table_schema_supports_utf8, instance, States, (States.state,)
) )
assert schema_errors == set() 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.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine") @pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_fix_utf8_issue_with_broken_schema( async def test_validate_db_schema_fix_utf8_issue_with_broken_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
recorder_db_url: str, recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test validating DB schema with MySQL when the schema is broken and repairing it.""" """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) await async_wait_recording_done(hass)
instance = get_instance(hass) session_maker = recorder_mock.get_session
session_maker = instance.get_session
def _break_states_schema(): def _break_states_schema():
with session_scope(session=session_maker()) as session: 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) await recorder_mock.async_add_executor_job(_break_states_schema)
schema_errors = await instance.async_add_executor_job( schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_supports_utf8, instance, States, (States.state,) validate_table_schema_supports_utf8, recorder_mock, States, (States.state,)
) )
assert schema_errors == {"states.4-byte UTF-8"} assert schema_errors == {"states.4-byte UTF-8"}
# Now repair the schema # Now repair the schema
await instance.async_add_executor_job( await recorder_mock.async_add_executor_job(
correct_db_schema_utf8, instance, States, schema_errors correct_db_schema_utf8, recorder_mock, States, schema_errors
) )
# Now validate the schema again # Now validate the schema again
schema_errors = await instance.async_add_executor_job( schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_supports_utf8, instance, States, ("state",) validate_table_schema_supports_utf8, recorder_mock, States, ("state",)
) )
assert schema_errors == set() 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.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine") @pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_fix_incorrect_collation( async def test_validate_db_schema_fix_incorrect_collation(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
recorder_db_url: str, recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test validating DB schema with MySQL when the collation is incorrect.""" """Test validating DB schema with MySQL when the collation is incorrect."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
instance = get_instance(hass) session_maker = recorder_mock.get_session
session_maker = instance.get_session
def _break_states_schema(): def _break_states_schema():
with session_scope(session=session_maker()) as session: 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) await recorder_mock.async_add_executor_job(_break_states_schema)
schema_errors = await instance.async_add_executor_job( schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_has_correct_collation, instance, States validate_table_schema_has_correct_collation, recorder_mock, States
) )
assert schema_errors == {"states.utf8mb4_unicode_ci"} assert schema_errors == {"states.utf8mb4_unicode_ci"}
# Now repair the schema # Now repair the schema
await instance.async_add_executor_job( await recorder_mock.async_add_executor_job(
correct_db_schema_utf8, instance, States, schema_errors correct_db_schema_utf8, recorder_mock, States, schema_errors
) )
# Now validate the schema again # Now validate the schema again
schema_errors = await instance.async_add_executor_job( schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_has_correct_collation, instance, States validate_table_schema_has_correct_collation, recorder_mock, States
) )
assert schema_errors == set() 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.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine") @pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_precision_correct_collation( async def test_validate_db_schema_precision_correct_collation(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
recorder_db_url: str, recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test validating DB schema when the schema is correct with the correct collation.""" """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) await async_wait_recording_done(hass)
instance = get_instance(hass) schema_errors = await recorder_mock.async_add_executor_job(
schema_errors = await instance.async_add_executor_job(
validate_table_schema_has_correct_collation, validate_table_schema_has_correct_collation,
instance, recorder_mock,
States, States,
) )
assert schema_errors == set() 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.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine") @pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_fix_utf8_issue_with_broken_schema_unrepairable( async def test_validate_db_schema_fix_utf8_issue_with_broken_schema_unrepairable(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
recorder_db_url: str, recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test validating DB schema with MySQL when the schema is broken and cannot be repaired.""" """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) await async_wait_recording_done(hass)
instance = get_instance(hass) session_maker = recorder_mock.get_session
session_maker = instance.get_session
def _break_states_schema(): def _break_states_schema():
with session_scope(session=session_maker()) as session: 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( _modify_columns(
session_maker, session_maker,
instance.engine, recorder_mock.engine,
"states", "states",
[ [
"entity_id VARCHAR(255) NOT NULL", "entity_id VARCHAR(255) NOT NULL",
], ],
) )
await instance.async_add_executor_job(_break_states_schema) await recorder_mock.async_add_executor_job(_break_states_schema)
schema_errors = await instance.async_add_executor_job( schema_errors = await recorder_mock.async_add_executor_job(
validate_table_schema_supports_utf8, instance, States, ("state",) validate_table_schema_supports_utf8, recorder_mock, States, ("state",)
) )
assert schema_errors == set() assert schema_errors == set()
assert "Error when validating DB schema" in caplog.text 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.skip_on_db_engine(["sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine") @pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_precision_good_schema( async def test_validate_db_schema_precision_good_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
recorder_db_url: str, recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test validating DB schema when the schema is correct.""" """Test validating DB schema when the schema is correct."""
await async_setup_recorder_instance(hass)
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
instance = get_instance(hass) schema_errors = await recorder_mock.async_add_executor_job(
schema_errors = await instance.async_add_executor_job(
validate_db_schema_precision, validate_db_schema_precision,
instance, recorder_mock,
States, States,
) )
assert schema_errors == set() 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.skip_on_db_engine(["sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine") @pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_precision_with_broken_schema( async def test_validate_db_schema_precision_with_broken_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
recorder_db_url: str, recorder_mock: Recorder,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test validating DB schema when the schema is broken and than repair it.""" """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) await async_wait_recording_done(hass)
instance = get_instance(hass) session_maker = recorder_mock.get_session
session_maker = instance.get_session
def _break_states_schema(): def _break_states_schema():
_modify_columns( _modify_columns(
session_maker, session_maker,
instance.engine, recorder_mock.engine,
"states", "states",
[ [
"last_updated_ts FLOAT(4)", "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) await recorder_mock.async_add_executor_job(_break_states_schema)
schema_errors = await instance.async_add_executor_job( schema_errors = await recorder_mock.async_add_executor_job(
validate_db_schema_precision, validate_db_schema_precision,
instance, recorder_mock,
States, States,
) )
assert schema_errors == {"states.double precision"} assert schema_errors == {"states.double precision"}
# Now repair the schema # Now repair the schema
await instance.async_add_executor_job( await recorder_mock.async_add_executor_job(
correct_db_schema_precision, instance, States, schema_errors correct_db_schema_precision, recorder_mock, States, schema_errors
) )
# Now validate the schema again # 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, validate_db_schema_precision,
instance, recorder_mock,
States, States,
) )
assert schema_errors == set() 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.skip_on_db_engine(["postgresql", "sqlite"])
@pytest.mark.usefixtures("skip_by_db_engine") @pytest.mark.usefixtures("skip_by_db_engine")
async def test_validate_db_schema_precision_with_unrepairable_broken_schema( async def test_validate_db_schema_precision_with_unrepairable_broken_schema(
async_setup_recorder_instance: RecorderInstanceGenerator,
hass: HomeAssistant, hass: HomeAssistant,
recorder_mock: Recorder,
recorder_db_url: str, recorder_db_url: str,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test validating DB schema when the schema is broken and cannot be repaired.""" """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) await async_wait_recording_done(hass)
instance = get_instance(hass) session_maker = recorder_mock.get_session
session_maker = instance.get_session
def _break_states_schema(): def _break_states_schema():
_modify_columns( _modify_columns(
session_maker, session_maker,
instance.engine, recorder_mock.engine,
"states", "states",
[ [
"state VARCHAR(255) NOT NULL", "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) await recorder_mock.async_add_executor_job(_break_states_schema)
schema_errors = await instance.async_add_executor_job( schema_errors = await recorder_mock.async_add_executor_job(
validate_db_schema_precision, validate_db_schema_precision,
instance, recorder_mock,
States, States,
) )
assert "Error when validating DB schema" in caplog.text assert "Error when validating DB schema" in caplog.text