From 2fae82a9efa81747020964e369e6833d9b7b0ecf Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 9 Feb 2023 18:43:14 +0100 Subject: [PATCH] Really ensure recorder test fixture is setup before hass fixture (#87796) --- tests/components/recorder/test_init.py | 12 ++++++------ tests/components/recorder/test_migrate.py | 16 ++++++++-------- tests/conftest.py | 10 ++++++---- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index b332a59d232..5f3ad2a91fa 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -324,7 +324,7 @@ async def test_saving_state_with_intermixed_time_changes( assert db_states[0].event_id is None -def test_saving_state_with_exception(hass, hass_recorder, caplog): +def test_saving_state_with_exception(hass_recorder, hass, caplog): """Test saving and restoring a state.""" hass = hass_recorder() @@ -362,7 +362,7 @@ def test_saving_state_with_exception(hass, hass_recorder, caplog): assert "Error saving events" not in caplog.text -def test_saving_state_with_sqlalchemy_exception(hass, hass_recorder, caplog): +def test_saving_state_with_sqlalchemy_exception(hass_recorder, hass, caplog): """Test saving state when there is an SQLAlchemyError.""" hass = hass_recorder() @@ -430,7 +430,7 @@ async def test_force_shutdown_with_queue_of_writes_that_generate_exceptions( assert "Error saving events" not in caplog.text -def test_saving_event(hass, hass_recorder): +def test_saving_event(hass_recorder): """Test saving and restoring an event.""" hass = hass_recorder() @@ -690,7 +690,7 @@ def test_saving_state_include_domain_glob_exclude_entity(hass_recorder): assert _state_with_context(hass, "test.ok").state == "state2" -def test_saving_state_and_removing_entity(hass, hass_recorder): +def test_saving_state_and_removing_entity(hass_recorder): """Test saving the state of a removed entity.""" hass = hass_recorder() entity_id = "lock.mine" @@ -1255,7 +1255,7 @@ def test_has_services(hass_recorder): assert hass.services.has_service(DOMAIN, SERVICE_PURGE_ENTITIES) -def test_service_disable_events_not_recording(hass, hass_recorder): +def test_service_disable_events_not_recording(hass_recorder): """Test that events are not recorded when recorder is disabled using service.""" hass = hass_recorder() @@ -1330,7 +1330,7 @@ def test_service_disable_events_not_recording(hass, hass_recorder): ) -def test_service_disable_states_not_recording(hass, hass_recorder): +def test_service_disable_states_not_recording(hass_recorder): """Test that state changes are not recorded when recorder is disabled using service.""" hass = hass_recorder() diff --git a/tests/components/recorder/test_migrate.py b/tests/components/recorder/test_migrate.py index 557aa67c575..7a2fdfd65d9 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, recorder_db_url: str) -> None: +async def test_schema_update_calls(recorder_db_url: str, hass: HomeAssistant) -> None: """Test that schema migrations occur in correct order.""" assert recorder.util.async_migration_in_progress(hass) is False @@ -75,7 +75,7 @@ async def test_schema_update_calls(hass: HomeAssistant, recorder_db_url: str) -> ) -async def test_migration_in_progress(hass: HomeAssistant, recorder_db_url: str) -> None: +async def test_migration_in_progress(recorder_db_url: str, hass: HomeAssistant) -> None: """Test that we can check for migration in progress.""" if recorder_db_url.startswith("mysql://"): # The database drop at the end of this test currently hangs on MySQL @@ -104,7 +104,7 @@ async def test_migration_in_progress(hass: HomeAssistant, recorder_db_url: str) async def test_database_migration_failed( - hass: HomeAssistant, recorder_db_url: str + recorder_db_url: str, hass: HomeAssistant ) -> None: """Test we notify if the migration fails.""" assert recorder.util.async_migration_in_progress(hass) is False @@ -137,7 +137,7 @@ async def test_database_migration_failed( async def test_database_migration_encounters_corruption( - hass: HomeAssistant, recorder_db_url: str + recorder_db_url: str, hass: HomeAssistant ) -> None: """Test we move away the database if its corrupt.""" if recorder_db_url.startswith(("mysql://", "postgresql://")): @@ -174,7 +174,7 @@ async def test_database_migration_encounters_corruption( async def test_database_migration_encounters_corruption_not_sqlite( - hass: HomeAssistant, recorder_db_url: str + recorder_db_url: str, hass: HomeAssistant ) -> None: """Test we fail on database error when we cannot recover.""" assert recorder.util.async_migration_in_progress(hass) is False @@ -210,7 +210,7 @@ async def test_database_migration_encounters_corruption_not_sqlite( async def test_events_during_migration_are_queued( - hass: HomeAssistant, recorder_db_url: str + recorder_db_url: str, hass: HomeAssistant ) -> None: """Test that events during migration are queued.""" @@ -246,7 +246,7 @@ async def test_events_during_migration_are_queued( async def test_events_during_migration_queue_exhausted( - hass: HomeAssistant, recorder_db_url: str + recorder_db_url: str, hass: HomeAssistant ) -> None: """Test that events during migration takes so long the queue is exhausted.""" @@ -290,7 +290,7 @@ async def test_events_during_migration_queue_exhausted( [(0, True), (16, True), (18, True), (22, True), (25, True)], ) async def test_schema_migrate( - hass: HomeAssistant, recorder_db_url: str, start_version, live + recorder_db_url: str, hass: HomeAssistant, start_version, live ): """Test the full schema migration logic. diff --git a/tests/conftest.py b/tests/conftest.py index 1d66380229e..0a36278939b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1034,8 +1034,13 @@ def recorder_config(): @pytest.fixture -def recorder_db_url(pytestconfig): +def recorder_db_url( + pytestconfig, + hass_fixture_setup, +): """Prepare a default database for tests and return a connection URL.""" + assert not hass_fixture_setup + db_url: str = pytestconfig.getoption("dburl") if db_url.startswith(("postgresql://", "mysql://")): import sqlalchemy_utils @@ -1169,14 +1174,11 @@ async def _async_init_recorder_component(hass, add_config=None, db_url=None): @pytest.fixture async def async_setup_recorder_instance( recorder_db_url, - hass_fixture_setup, enable_nightly_purge, enable_statistics, enable_statistics_table_validation, ) -> AsyncGenerator[SetupRecorderInstanceT, None]: """Yield callable to setup recorder instance.""" - assert not hass_fixture_setup - # Local import to avoid processing recorder and SQLite modules when running a # testcase which does not use the recorder. from homeassistant.components import recorder