Improve recorder tests to use MariaDB / PostgreSQL (#87756)

This commit is contained in:
Erik Montnemery 2023-02-09 09:18:20 +01:00 committed by GitHub
parent b0f5bdd504
commit 58618b2978
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View file

@ -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()

View file

@ -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)