Improve recorder setup in tests (#68333)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Erik Montnemery 2022-03-21 23:49:18 +01:00 committed by GitHub
parent 653305b998
commit 247af2e74f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 10 deletions

View File

@ -7,11 +7,12 @@ import pytest
from homeassistant.components.demo import DOMAIN
from homeassistant.components.device_tracker.legacy import YAML_DEVICES
from homeassistant.components.recorder import get_instance
from homeassistant.components.recorder.statistics import list_statistic_ids
from homeassistant.helpers.json import JSONEncoder
from homeassistant.setup import async_setup_component, setup_component
from homeassistant.setup import async_setup_component
from tests.components.recorder.common import wait_recording_done
from tests.components.recorder.common import async_wait_recording_done_without_instance
@pytest.fixture(autouse=True)
@ -45,16 +46,16 @@ async def test_setting_up_demo(hass):
)
def test_demo_statistics(hass_recorder):
async def test_demo_statistics(hass, recorder_mock):
"""Test that the demo components makes some statistics available."""
hass = hass_recorder()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()
await hass.async_start()
await async_wait_recording_done_without_instance(hass)
assert setup_component(hass, DOMAIN, {DOMAIN: {}})
hass.block_till_done()
hass.start()
wait_recording_done(hass)
statistic_ids = list_statistic_ids(hass)
statistic_ids = await get_instance(hass).async_add_executor_job(
list_statistic_ids, hass
)
assert {
"name": None,
"source": "demo",

View File

@ -41,6 +41,7 @@ from tests.common import ( # noqa: E402, isort:skip
MockConfigEntry,
MockUser,
async_fire_mqtt_message,
async_init_recorder_component,
async_test_home_assistant,
get_test_home_assistant,
init_recorder_component,
@ -821,6 +822,28 @@ def hass_recorder(enable_nightly_purge, enable_statistics, hass_storage):
hass.stop()
@pytest.fixture
async def recorder_mock(enable_nightly_purge, enable_statistics, hass):
"""Fixture with in-memory recorder."""
stats = recorder.Recorder.async_periodic_statistics if enable_statistics else None
nightly = recorder.Recorder.async_nightly_tasks if enable_nightly_purge else None
with patch(
"homeassistant.components.recorder.Recorder.async_periodic_statistics",
side_effect=stats,
autospec=True,
), patch(
"homeassistant.components.recorder.Recorder.async_nightly_tasks",
side_effect=nightly,
autospec=True,
):
await async_init_recorder_component(hass)
await hass.async_start()
await hass.async_block_till_done()
await hass.async_add_executor_job(
hass.data[recorder.DATA_INSTANCE].block_till_done
)
@pytest.fixture
def mock_integration_frame():
"""Mock as if we're calling code from inside an integration."""