From 58618b297841cd8ec5b6b587a50c0f9a3a36700b Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 9 Feb 2023 09:18:20 +0100 Subject: [PATCH] Improve recorder tests to use MariaDB / PostgreSQL (#87756) --- tests/components/recorder/test_init.py | 26 +++++++++++++++++------- tests/components/recorder/test_models.py | 4 ++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index 7e7e563f22f..d2252cbba4c 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -97,14 +97,19 @@ def _default_recorder(hass): async def test_shutdown_before_startup_finishes( - async_setup_recorder_instance: SetupRecorderInstanceT, hass: HomeAssistant, tmp_path + async_setup_recorder_instance: SetupRecorderInstanceT, + hass: HomeAssistant, + recorder_db_url: str, + tmp_path, ): """Test shutdown before recorder starts is clean.""" - # On-disk database because this test does not play nice with the - # MutexPool + if recorder_db_url == "sqlite://": + # On-disk database because this test does not play nice with the + # MutexPool + recorder_db_url = "sqlite:///" + str(tmp_path / "pytest.db") config = { - recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"), + recorder.CONF_DB_URL: recorder_db_url, recorder.CONF_COMMIT_INTERVAL: 1, } hass.state = CoreState.not_running @@ -1520,13 +1525,16 @@ def test_entity_id_filter(hass_recorder): async def test_database_lock_and_unlock( async_setup_recorder_instance: SetupRecorderInstanceT, hass: HomeAssistant, + recorder_db_url: str, tmp_path, ): """Test writing events during lock getting written after unlocking.""" - # Use file DB, in memory DB cannot do write locks. + if recorder_db_url == "sqlite://": + # Use file DB, in memory DB cannot do write locks. + recorder_db_url = "sqlite:///" + str(tmp_path / "pytest.db") config = { recorder.CONF_COMMIT_INTERVAL: 0, - recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"), + recorder.CONF_DB_URL: recorder_db_url, } await async_setup_recorder_instance(hass, config) await hass.async_block_till_done() @@ -1562,13 +1570,17 @@ async def test_database_lock_and_unlock( async def test_database_lock_and_overflow( async_setup_recorder_instance: SetupRecorderInstanceT, hass: HomeAssistant, + recorder_db_url: str, tmp_path, ): """Test writing events during lock leading to overflow the queue causes the database to unlock.""" # Use file DB, in memory DB cannot do write locks. + if recorder_db_url == "sqlite://": + # Use file DB, in memory DB cannot do write locks. + recorder_db_url = "sqlite:///" + str(tmp_path / "pytest.db") config = { recorder.CONF_COMMIT_INTERVAL: 0, - recorder.CONF_DB_URL: "sqlite:///" + str(tmp_path / "pytest.db"), + recorder.CONF_DB_URL: recorder_db_url, } await async_setup_recorder_instance(hass, config) await hass.async_block_till_done() diff --git a/tests/components/recorder/test_models.py b/tests/components/recorder/test_models.py index 8fa991d1b58..bd8c7f2f201 100644 --- a/tests/components/recorder/test_models.py +++ b/tests/components/recorder/test_models.py @@ -147,9 +147,9 @@ def test_from_event_to_delete_state() -> None: assert db_state.last_updated_ts == event.time_fired.timestamp() -def test_entity_ids() -> None: +def test_entity_ids(recorder_db_url: str) -> None: """Test if entity ids helper method works.""" - engine = create_engine("sqlite://") + engine = create_engine(recorder_db_url) Base.metadata.create_all(engine) session_factory = sessionmaker(bind=engine)