Simplify roborock map storage test fixture (#141430)

This commit is contained in:
Allen Porter 2025-03-26 05:19:48 -07:00 committed by GitHub
parent e3f2f30395
commit 06f6c86ba5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 36 deletions

View File

@ -3,10 +3,9 @@
from collections.abc import Generator
from copy import deepcopy
import pathlib
import shutil
import tempfile
from typing import Any
from unittest.mock import Mock, patch
import uuid
import pytest
from roborock import RoborockCategory, RoomMapping
@ -19,7 +18,6 @@ from homeassistant.components.roborock.const import (
CONF_USER_DATA,
DOMAIN,
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
@ -218,7 +216,6 @@ async def setup_entry(
hass: HomeAssistant,
bypass_api_fixture,
mock_roborock_entry: MockConfigEntry,
cleanup_map_storage: pathlib.Path,
platforms: list[Platform],
) -> Generator[MockConfigEntry]:
"""Set up the Roborock platform."""
@ -228,27 +225,18 @@ async def setup_entry(
yield mock_roborock_entry
@pytest.fixture(autouse=True)
async def cleanup_map_storage(cleanup_map_storage_manual) -> Generator[pathlib.Path]:
"""Test cleanup, remove any map storage persisted during the test."""
return cleanup_map_storage_manual
@pytest.fixture
async def cleanup_map_storage_manual(
hass: HomeAssistant, mock_roborock_entry: MockConfigEntry
@pytest.fixture(autouse=True, name="storage_path")
async def storage_path_fixture(
hass: HomeAssistant,
) -> Generator[pathlib.Path]:
"""Test cleanup, remove any map storage persisted during the test."""
tmp_path = str(uuid.uuid4())
with patch(
"homeassistant.components.roborock.roborock_storage.STORAGE_PATH", new=tmp_path
):
storage_path = (
pathlib.Path(hass.config.path(tmp_path)) / mock_roborock_entry.entry_id
)
yield storage_path
# We need to first unload the config entry because unloading it will
# persist any unsaved maps to storage.
if mock_roborock_entry.state is ConfigEntryState.LOADED:
await hass.config_entries.async_unload(mock_roborock_entry.entry_id)
shutil.rmtree(str(storage_path), ignore_errors=True)
with tempfile.TemporaryDirectory() as tmp_path:
def get_storage_path(_: HomeAssistant, entry_id: str) -> pathlib.Path:
return pathlib.Path(tmp_path) / entry_id
with patch(
"homeassistant.components.roborock.roborock_storage._storage_path_prefix",
new=get_storage_path,
):
yield pathlib.Path(tmp_path)

View File

@ -331,7 +331,6 @@ async def test_discovery_already_setup(
hass: HomeAssistant,
bypass_api_fixture,
mock_roborock_entry: MockConfigEntry,
cleanup_map_storage_manual,
) -> None:
"""Handle aborting if the device is already setup."""
await hass.config_entries.async_setup(mock_roborock_entry.entry_id)

View File

@ -174,7 +174,7 @@ async def test_remove_from_hass(
bypass_api_fixture,
setup_entry: MockConfigEntry,
hass_client: ClientSessionGenerator,
cleanup_map_storage: pathlib.Path,
storage_path: pathlib.Path,
) -> None:
"""Test that removing from hass removes any existing images."""
@ -184,17 +184,18 @@ async def test_remove_from_hass(
resp = await client.get("/api/image_proxy/image.roborock_s7_maxv_upstairs")
assert resp.status == HTTPStatus.OK
assert not cleanup_map_storage.exists()
config_entry_storage = storage_path / setup_entry.entry_id
assert not config_entry_storage.exists()
# Flush to disk
await hass.config_entries.async_unload(setup_entry.entry_id)
assert cleanup_map_storage.exists()
paths = list(cleanup_map_storage.walk())
assert config_entry_storage.exists()
paths = list(config_entry_storage.walk())
assert len(paths) == 4 # Two map image and two directories
await hass.config_entries.async_remove(setup_entry.entry_id)
# After removal, directories should be empty.
assert not cleanup_map_storage.exists()
assert not config_entry_storage.exists()
@pytest.mark.parametrize("platforms", [[Platform.IMAGE]])
@ -202,7 +203,7 @@ async def test_oserror_remove_image(
hass: HomeAssistant,
bypass_api_fixture,
setup_entry: MockConfigEntry,
cleanup_map_storage: pathlib.Path,
storage_path: pathlib.Path,
hass_client: ClientSessionGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
@ -215,11 +216,12 @@ async def test_oserror_remove_image(
assert resp.status == HTTPStatus.OK
# Image content is saved when unloading
assert not cleanup_map_storage.exists()
config_entry_storage = storage_path / setup_entry.entry_id
assert not config_entry_storage.exists()
await hass.config_entries.async_unload(setup_entry.entry_id)
assert cleanup_map_storage.exists()
paths = list(cleanup_map_storage.walk())
assert config_entry_storage.exists()
paths = list(config_entry_storage.walk())
assert len(paths) == 4 # Two map image and two directories
with patch(