From b0f5bdd5045a4a8c42ed650a6350f1d49e366bcc Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 9 Feb 2023 08:42:32 +0100 Subject: [PATCH] Improve migration tests to use MariaDB / PostgreSQL database (#87753) --- tests/components/recorder/test_migrate.py | 54 ++++++++++++++--------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/tests/components/recorder/test_migrate.py b/tests/components/recorder/test_migrate.py index c4902d73e22..e9c0b8cba44 100644 --- a/tests/components/recorder/test_migrate.py +++ b/tests/components/recorder/test_migrate.py @@ -46,7 +46,7 @@ def _get_native_states(hass, entity_id): ] -async def test_schema_update_calls(hass: HomeAssistant) -> None: +async def test_schema_update_calls(hass: HomeAssistant, recorder_db_url: str) -> None: """Test that schema migrations occur in correct order.""" assert recorder.util.async_migration_in_progress(hass) is False @@ -59,7 +59,7 @@ async def test_schema_update_calls(hass: HomeAssistant) -> None: ) as update: recorder_helper.async_initialize_recorder(hass) await async_setup_component( - hass, "recorder", {"recorder": {"db_url": "sqlite://"}} + hass, "recorder", {"recorder": {"db_url": recorder_db_url}} ) await async_wait_recording_done(hass) @@ -75,7 +75,7 @@ async def test_schema_update_calls(hass: HomeAssistant) -> None: ) -async def test_migration_in_progress(hass: HomeAssistant) -> None: +async def test_migration_in_progress(hass: HomeAssistant, recorder_db_url: str) -> None: """Test that we can check for migration in progress.""" assert recorder.util.async_migration_in_progress(hass) is False @@ -85,7 +85,7 @@ async def test_migration_in_progress(hass: HomeAssistant) -> None: ): recorder_helper.async_initialize_recorder(hass) await async_setup_component( - hass, "recorder", {"recorder": {"db_url": "sqlite://"}} + hass, "recorder", {"recorder": {"db_url": recorder_db_url}} ) await recorder.get_instance(hass).async_migration_event.wait() assert recorder.util.async_migration_in_progress(hass) is True @@ -95,7 +95,9 @@ async def test_migration_in_progress(hass: HomeAssistant) -> None: assert recorder.get_instance(hass).schema_version == SCHEMA_VERSION -async def test_database_migration_failed(hass: HomeAssistant) -> None: +async def test_database_migration_failed( + hass: HomeAssistant, recorder_db_url: str +) -> None: """Test we notify if the migration fails.""" assert recorder.util.async_migration_in_progress(hass) is False @@ -113,7 +115,7 @@ async def test_database_migration_failed(hass: HomeAssistant) -> None: ) as mock_dismiss: recorder_helper.async_initialize_recorder(hass) await async_setup_component( - hass, "recorder", {"recorder": {"db_url": "sqlite://"}} + hass, "recorder", {"recorder": {"db_url": recorder_db_url}} ) hass.states.async_set("my.entity", "on", {}) hass.states.async_set("my.entity", "off", {}) @@ -126,8 +128,14 @@ async def test_database_migration_failed(hass: HomeAssistant) -> None: assert len(mock_dismiss.mock_calls) == 1 -async def test_database_migration_encounters_corruption(hass: HomeAssistant) -> None: +async def test_database_migration_encounters_corruption( + hass: HomeAssistant, recorder_db_url: str +) -> None: """Test we move away the database if its corrupt.""" + if recorder_db_url.startswith(("mysql://", "postgresql://")): + # This test is specific for SQLite, wiping the database on error only happens + # with SQLite. + return assert recorder.util.async_migration_in_progress(hass) is False @@ -147,7 +155,7 @@ async def test_database_migration_encounters_corruption(hass: HomeAssistant) -> ): recorder_helper.async_initialize_recorder(hass) await async_setup_component( - hass, "recorder", {"recorder": {"db_url": "sqlite://"}} + hass, "recorder", {"recorder": {"db_url": recorder_db_url}} ) hass.states.async_set("my.entity", "on", {}) hass.states.async_set("my.entity", "off", {}) @@ -158,7 +166,7 @@ async def test_database_migration_encounters_corruption(hass: HomeAssistant) -> async def test_database_migration_encounters_corruption_not_sqlite( - hass: HomeAssistant, + hass: HomeAssistant, recorder_db_url: str ) -> None: """Test we fail on database error when we cannot recover.""" assert recorder.util.async_migration_in_progress(hass) is False @@ -179,7 +187,7 @@ async def test_database_migration_encounters_corruption_not_sqlite( ) as mock_dismiss: recorder_helper.async_initialize_recorder(hass) await async_setup_component( - hass, "recorder", {"recorder": {"db_url": "sqlite://"}} + hass, "recorder", {"recorder": {"db_url": recorder_db_url}} ) hass.states.async_set("my.entity", "on", {}) hass.states.async_set("my.entity", "off", {}) @@ -193,7 +201,9 @@ async def test_database_migration_encounters_corruption_not_sqlite( assert len(mock_dismiss.mock_calls) == 1 -async def test_events_during_migration_are_queued(hass: HomeAssistant) -> None: +async def test_events_during_migration_are_queued( + hass: HomeAssistant, recorder_db_url: str +) -> None: """Test that events during migration are queued.""" assert recorder.util.async_migration_in_progress(hass) is False @@ -209,7 +219,7 @@ async def test_events_during_migration_are_queued(hass: HomeAssistant) -> None: await async_setup_component( hass, "recorder", - {"recorder": {"db_url": "sqlite://", "commit_interval": 0}}, + {"recorder": {"db_url": recorder_db_url, "commit_interval": 0}}, ) hass.states.async_set("my.entity", "on", {}) hass.states.async_set("my.entity", "off", {}) @@ -227,7 +237,9 @@ async def test_events_during_migration_are_queued(hass: HomeAssistant) -> None: assert len(db_states) == 2 -async def test_events_during_migration_queue_exhausted(hass: HomeAssistant) -> None: +async def test_events_during_migration_queue_exhausted( + hass: HomeAssistant, recorder_db_url: str +) -> None: """Test that events during migration takes so long the queue is exhausted.""" assert recorder.util.async_migration_in_progress(hass) is False @@ -240,7 +252,7 @@ async def test_events_during_migration_queue_exhausted(hass: HomeAssistant) -> N await async_setup_component( hass, "recorder", - {"recorder": {"db_url": "sqlite://", "commit_interval": 0}}, + {"recorder": {"db_url": recorder_db_url, "commit_interval": 0}}, ) hass.states.async_set("my.entity", "on", {}) await hass.async_block_till_done() @@ -269,7 +281,9 @@ async def test_events_during_migration_queue_exhausted(hass: HomeAssistant) -> N "start_version,live", [(0, True), (16, True), (18, True), (22, True), (25, True)], ) -async def test_schema_migrate(hass, start_version, live): +async def test_schema_migrate( + hass: HomeAssistant, recorder_db_url: str, start_version, live +): """Test the full schema migration logic. We're just testing that the logic can execute successfully here without @@ -352,7 +366,7 @@ async def test_schema_migrate(hass, start_version, live): recorder_helper.async_initialize_recorder(hass) hass.async_create_task( async_setup_component( - hass, "recorder", {"recorder": {"db_url": "sqlite://"}} + hass, "recorder", {"recorder": {"db_url": recorder_db_url}} ) ) await recorder_helper.async_wait_recorder(hass) @@ -401,9 +415,9 @@ def test_modify_column(engine_type, substr): assert not connection.execute.called -def test_forgiving_add_column() -> None: +def test_forgiving_add_column(recorder_db_url: str) -> None: """Test that add column will continue if column exists.""" - engine = create_engine("sqlite://", poolclass=StaticPool) + engine = create_engine(recorder_db_url, poolclass=StaticPool) with Session(engine) as session: session.execute(text("CREATE TABLE hello (id int)")) instance = Mock() @@ -416,9 +430,9 @@ def test_forgiving_add_column() -> None: ) -def test_forgiving_add_index() -> None: +def test_forgiving_add_index(recorder_db_url: str) -> None: """Test that add index will continue if index exists.""" - engine = create_engine("sqlite://", poolclass=StaticPool) + engine = create_engine(recorder_db_url, poolclass=StaticPool) db_schema.Base.metadata.create_all(engine) with Session(engine) as session: instance = Mock()