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

View File

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