Translation fixes and tweaks (#34489)
Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
96649a7e27
commit
e9ff1940d6
7 files changed
+189
-83
No files matched your search
@@ -6,8 +6,9 @@ import pathlib
|
||||
from asynctest import Mock, patch
|
||||
import pytest
|
||||
|
||||
from homeassistant.const import EVENT_COMPONENT_LOADED
|
||||
from homeassistant.generated import config_flows
|
||||
import homeassistant.helpers.translation as translation
|
||||
from homeassistant.helpers import translation
|
||||
from homeassistant.loader import async_get_integration
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
@@ -214,26 +215,53 @@ async def test_translation_merging(hass, caplog):
|
||||
assert "component.sensor.state.moon__phase.first_quarter" in translations
|
||||
assert "component.sensor.state.season__season.summer" in translations
|
||||
|
||||
# Merge in some bad translation data
|
||||
integration = Mock(file_path=pathlib.Path(__file__))
|
||||
hass.config.components.add("sensor.bad_translations")
|
||||
# Clear cache
|
||||
hass.bus.async_fire(EVENT_COMPONENT_LOADED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Patch in some bad translation data
|
||||
|
||||
orig_load_translations = translation.load_translations_files
|
||||
|
||||
def mock_load_translations_files(files):
|
||||
"""Mock loading."""
|
||||
result = orig_load_translations(files)
|
||||
result["sensor.season"] = {"state": "bad data"}
|
||||
return result
|
||||
|
||||
with patch.object(
|
||||
translation, "component_translation_path", return_value="bla.json"
|
||||
), patch.object(
|
||||
translation,
|
||||
"load_translations_files",
|
||||
return_value={"sensor.bad_translations": {"state": "bad data"}},
|
||||
), patch(
|
||||
"homeassistant.helpers.translation.async_get_integration",
|
||||
return_value=integration,
|
||||
side_effect=mock_load_translations_files,
|
||||
):
|
||||
translations = await translation.async_get_translations(hass, "en", "state")
|
||||
|
||||
assert "component.sensor.state.moon__phase.first_quarter" in translations
|
||||
assert "component.sensor.state.season__season.summer" in translations
|
||||
|
||||
assert (
|
||||
"An integration providing translations for sensor provided invalid data: bad data"
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
|
||||
async def test_caching(hass):
|
||||
"""Test we cache data."""
|
||||
hass.config.components.add("sensor")
|
||||
|
||||
# Patch with same method so we can count invocations
|
||||
with patch(
|
||||
"homeassistant.helpers.translation.merge_resources",
|
||||
side_effect=translation.merge_resources,
|
||||
) as mock_merge:
|
||||
await translation.async_get_translations(hass, "en", "state")
|
||||
assert len(mock_merge.mock_calls) == 1
|
||||
|
||||
await translation.async_get_translations(hass, "en", "state")
|
||||
assert len(mock_merge.mock_calls) == 1
|
||||
|
||||
# This event clears the cache so we should record another call
|
||||
hass.bus.async_fire(EVENT_COMPONENT_LOADED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await translation.async_get_translations(hass, "en", "state")
|
||||
assert len(mock_merge.mock_calls) == 2
|
||||
Reference in new issue
Block a user