Skip to content

Commit

Permalink
Improve recorder tests to use MariaDB / PostgreSQL (home-assistant#87756
Browse files Browse the repository at this point in the history
)
  • Loading branch information
emontnemery authored Feb 9, 2023
1 parent b0f5bdd commit 58618b2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
26 changes: 19 additions & 7 deletions tests/components/recorder/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1562,13 +1570,17 @@ def _get_db_events():
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()
Expand Down
4 changes: 2 additions & 2 deletions tests/components/recorder/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 58618b2

Please sign in to comment.