mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Clean up translations for mocked integrations inbetween tests
This commit is contained in:
parent
4cdc3de94a
commit
264ebd3beb
@ -1867,23 +1867,6 @@ async def snapshot_platform(
|
|||||||
assert state == snapshot(name=f"{entity_entry.entity_id}-state")
|
assert state == snapshot(name=f"{entity_entry.entity_id}-state")
|
||||||
|
|
||||||
|
|
||||||
def reset_translation_cache(hass: HomeAssistant, components: list[str]) -> None:
|
|
||||||
"""Reset translation cache for specified components.
|
|
||||||
|
|
||||||
Use this if you are mocking a core component (for example via
|
|
||||||
mock_integration), to ensure that the mocked translations are not
|
|
||||||
persisted in the shared session cache.
|
|
||||||
"""
|
|
||||||
translations_cache = translation._async_get_translations_cache(hass)
|
|
||||||
for loaded_components in translations_cache.cache_data.loaded.values():
|
|
||||||
for component_to_unload in components:
|
|
||||||
loaded_components.discard(component_to_unload)
|
|
||||||
for loaded_categories in translations_cache.cache_data.cache.values():
|
|
||||||
for loaded_components in loaded_categories.values():
|
|
||||||
for component_to_unload in components:
|
|
||||||
loaded_components.pop(component_to_unload, None)
|
|
||||||
|
|
||||||
|
|
||||||
@lru_cache
|
@lru_cache
|
||||||
def get_quality_scale(integration: str) -> dict[str, QualityScaleStatus]:
|
def get_quality_scale(integration: str) -> dict[str, QualityScaleStatus]:
|
||||||
"""Load quality scale for integration."""
|
"""Load quality scale for integration."""
|
||||||
|
@ -34,7 +34,6 @@ from tests.common import (
|
|||||||
mock_integration,
|
mock_integration,
|
||||||
mock_platform,
|
mock_platform,
|
||||||
mock_restore_cache,
|
mock_restore_cache,
|
||||||
reset_translation_cache,
|
|
||||||
)
|
)
|
||||||
from tests.typing import ClientSessionGenerator, WebSocketGenerator
|
from tests.typing import ClientSessionGenerator, WebSocketGenerator
|
||||||
|
|
||||||
@ -519,9 +518,6 @@ async def test_default_engine_prefer_cloud_entity(
|
|||||||
assert provider_engine.name == "test"
|
assert provider_engine.name == "test"
|
||||||
assert async_default_engine(hass) == "stt.cloud_stt_entity"
|
assert async_default_engine(hass) == "stt.cloud_stt_entity"
|
||||||
|
|
||||||
# Reset the `cloud` translations cache to avoid flaky translation checks
|
|
||||||
reset_translation_cache(hass, ["cloud"])
|
|
||||||
|
|
||||||
|
|
||||||
async def test_get_engine_legacy(
|
async def test_get_engine_legacy(
|
||||||
hass: HomeAssistant, tmp_path: Path, mock_provider: MockSTTProvider
|
hass: HomeAssistant, tmp_path: Path, mock_provider: MockSTTProvider
|
||||||
|
@ -44,7 +44,6 @@ from tests.common import (
|
|||||||
mock_integration,
|
mock_integration,
|
||||||
mock_platform,
|
mock_platform,
|
||||||
mock_restore_cache,
|
mock_restore_cache,
|
||||||
reset_translation_cache,
|
|
||||||
)
|
)
|
||||||
from tests.typing import ClientSessionGenerator, WebSocketGenerator
|
from tests.typing import ClientSessionGenerator, WebSocketGenerator
|
||||||
|
|
||||||
@ -1987,6 +1986,3 @@ async def test_default_engine_prefer_cloud_entity(
|
|||||||
provider_engine = tts.async_resolve_engine(hass, "test")
|
provider_engine = tts.async_resolve_engine(hass, "test")
|
||||||
assert provider_engine == "test"
|
assert provider_engine == "test"
|
||||||
assert tts.async_default_engine(hass) == "tts.cloud_tts_entity"
|
assert tts.async_default_engine(hass) == "tts.cloud_tts_entity"
|
||||||
|
|
||||||
# Reset the `cloud` translations cache to avoid flaky translation checks
|
|
||||||
reset_translation_cache(hass, ["cloud"])
|
|
||||||
|
@ -85,6 +85,7 @@ from homeassistant.helpers import (
|
|||||||
issue_registry as ir,
|
issue_registry as ir,
|
||||||
label_registry as lr,
|
label_registry as lr,
|
||||||
recorder as recorder_helper,
|
recorder as recorder_helper,
|
||||||
|
translation as translation_helper,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.translation import _TranslationsCacheData
|
from homeassistant.helpers.translation import _TranslationsCacheData
|
||||||
@ -1232,11 +1233,10 @@ def mock_get_source_ip() -> Generator[_patch]:
|
|||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="session")
|
@pytest.fixture(autouse=True, scope="session")
|
||||||
def translations_once() -> Generator[_patch]:
|
def translations_once() -> Generator[_patch]:
|
||||||
"""Only load translations once per session.
|
"""Only load translations once per module.
|
||||||
|
|
||||||
Warning: having this as a session fixture can cause issues with tests that
|
Note: To avoid issues with tests that mock integrations, translations for
|
||||||
create mock integrations, overriding the real integration translations
|
mocked integrations are cleaned up by the evict_faked_translations fixture.
|
||||||
with empty ones. Translations should be reset after such tests (see #131628)
|
|
||||||
"""
|
"""
|
||||||
cache = _TranslationsCacheData({}, {})
|
cache = _TranslationsCacheData({}, {})
|
||||||
patcher = patch(
|
patcher = patch(
|
||||||
@ -1250,6 +1250,27 @@ def translations_once() -> Generator[_patch]:
|
|||||||
patcher.stop()
|
patcher.stop()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True, scope="module")
|
||||||
|
def evict_faked_translations(translations_once) -> Generator[_patch]:
|
||||||
|
"""Clear translations for mocked integrations from the cache after each module."""
|
||||||
|
real_component_strings = translation_helper._async_get_component_strings
|
||||||
|
with patch(
|
||||||
|
"homeassistant.helpers.translation._async_get_component_strings",
|
||||||
|
wraps=real_component_strings,
|
||||||
|
) as mock_component_strings:
|
||||||
|
yield
|
||||||
|
cache: _TranslationsCacheData = translations_once.kwargs["return_value"]
|
||||||
|
for call in mock_component_strings.mock_calls:
|
||||||
|
integrations: dict[str, loader.Integration] = call.args[3]
|
||||||
|
for domain, integration in integrations.items():
|
||||||
|
if str(integration.file_path).endswith(
|
||||||
|
f"homeassistant/components/{domain}"
|
||||||
|
):
|
||||||
|
continue
|
||||||
|
cache.loaded["en"].discard(domain)
|
||||||
|
cache.cache["en"].pop(domain, None)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def disable_translations_once(
|
def disable_translations_once(
|
||||||
translations_once: _patch,
|
translations_once: _patch,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user