Reduce translations_once fixture scope to module

This commit is contained in:
Erik 2025-02-17 17:13:44 +01:00
parent 4cdc3de94a
commit d82219920e
4 changed files with 5 additions and 30 deletions

View File

@ -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."""

View File

@ -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

View File

@ -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"])

View File

@ -1230,13 +1230,13 @@ def mock_get_source_ip() -> Generator[_patch]:
patcher.stop() patcher.stop()
@pytest.fixture(autouse=True, scope="session") @pytest.fixture(autouse=True, scope="module")
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: This was previously a session scope fixture, but that made tests which
create mock integrations, overriding the real integration translations mock an integration interfere with tests which set up the real integration.
with empty ones. Translations should be reset after such tests (see #131628) Unless we find a way to solve that, this needs to be module scope.
""" """
cache = _TranslationsCacheData({}, {}) cache = _TranslationsCacheData({}, {})
patcher = patch( patcher = patch(