Add ability to load test fixtures on the executor (#144534)

This commit is contained in:
epenet 2025-05-26 21:50:28 +02:00 committed by GitHub
parent 34f92d584b
commit 8abbd35c54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 12 deletions

View File

@ -575,6 +575,13 @@ def load_fixture(filename: str, integration: str | None = None) -> str:
return get_fixture_path(filename, integration).read_text(encoding="utf8") return get_fixture_path(filename, integration).read_text(encoding="utf8")
async def async_load_fixture(
hass: HomeAssistant, filename: str, integration: str | None = None
) -> str:
"""Load a fixture."""
return await hass.async_add_executor_job(load_fixture, filename, integration)
def load_json_value_fixture( def load_json_value_fixture(
filename: str, integration: str | None = None filename: str, integration: str | None = None
) -> JsonValueType: ) -> JsonValueType:
@ -589,6 +596,13 @@ def load_json_array_fixture(
return json_loads_array(load_fixture(filename, integration)) return json_loads_array(load_fixture(filename, integration))
async def async_load_json_array_fixture(
hass: HomeAssistant, filename: str, integration: str | None = None
) -> JsonArrayType:
"""Load a JSON object from a fixture."""
return json_loads_array(await async_load_fixture(hass, filename, integration))
def load_json_object_fixture( def load_json_object_fixture(
filename: str, integration: str | None = None filename: str, integration: str | None = None
) -> JsonObjectType: ) -> JsonObjectType:
@ -596,6 +610,13 @@ def load_json_object_fixture(
return json_loads_object(load_fixture(filename, integration)) return json_loads_object(load_fixture(filename, integration))
async def async_load_json_object_fixture(
hass: HomeAssistant, filename: str, integration: str | None = None
) -> JsonObjectType:
"""Load a JSON object from a fixture."""
return json_loads_object(await async_load_fixture(hass, filename, integration))
def json_round_trip(obj: Any) -> Any: def json_round_trip(obj: Any) -> Any:
"""Round trip an object to JSON.""" """Round trip an object to JSON."""
return json_loads(json_dumps(obj)) return json_loads(json_dumps(obj))

View File

@ -1,7 +1,6 @@
"""Fixtures for easyEnergy integration tests.""" """Fixtures for easyEnergy integration tests."""
from collections.abc import Generator from collections.abc import AsyncGenerator, Generator
import json
from unittest.mock import AsyncMock, MagicMock, patch from unittest.mock import AsyncMock, MagicMock, patch
from easyenergy import Electricity, Gas from easyenergy import Electricity, Gas
@ -10,7 +9,7 @@ import pytest
from homeassistant.components.easyenergy.const import DOMAIN from homeassistant.components.easyenergy.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture from tests.common import MockConfigEntry, async_load_json_array_fixture
@pytest.fixture @pytest.fixture
@ -34,17 +33,17 @@ def mock_config_entry() -> MockConfigEntry:
@pytest.fixture @pytest.fixture
def mock_easyenergy() -> Generator[MagicMock]: async def mock_easyenergy(hass: HomeAssistant) -> AsyncGenerator[MagicMock]:
"""Return a mocked easyEnergy client.""" """Return a mocked easyEnergy client."""
with patch( with patch(
"homeassistant.components.easyenergy.coordinator.EasyEnergy", autospec=True "homeassistant.components.easyenergy.coordinator.EasyEnergy", autospec=True
) as easyenergy_mock: ) as easyenergy_mock:
client = easyenergy_mock.return_value client = easyenergy_mock.return_value
client.energy_prices.return_value = Electricity.from_dict( client.energy_prices.return_value = Electricity.from_dict(
json.loads(load_fixture("today_energy.json", DOMAIN)) await async_load_json_array_fixture(hass, "today_energy.json", DOMAIN)
) )
client.gas_prices.return_value = Gas.from_dict( client.gas_prices.return_value = Gas.from_dict(
json.loads(load_fixture("today_gas.json", DOMAIN)) await async_load_json_array_fixture(hass, "today_gas.json", DOMAIN)
) )
yield client yield client

View File

@ -1,7 +1,6 @@
"""Fixtures for EnergyZero integration tests.""" """Fixtures for EnergyZero integration tests."""
from collections.abc import Generator from collections.abc import AsyncGenerator, Generator
import json
from unittest.mock import AsyncMock, MagicMock, patch from unittest.mock import AsyncMock, MagicMock, patch
from energyzero import Electricity, Gas from energyzero import Electricity, Gas
@ -10,7 +9,7 @@ import pytest
from homeassistant.components.energyzero.const import DOMAIN from homeassistant.components.energyzero.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture from tests.common import MockConfigEntry, async_load_json_object_fixture
@pytest.fixture @pytest.fixture
@ -35,17 +34,17 @@ def mock_config_entry() -> MockConfigEntry:
@pytest.fixture @pytest.fixture
def mock_energyzero() -> Generator[MagicMock]: async def mock_energyzero(hass: HomeAssistant) -> AsyncGenerator[MagicMock]:
"""Return a mocked EnergyZero client.""" """Return a mocked EnergyZero client."""
with patch( with patch(
"homeassistant.components.energyzero.coordinator.EnergyZero", autospec=True "homeassistant.components.energyzero.coordinator.EnergyZero", autospec=True
) as energyzero_mock: ) as energyzero_mock:
client = energyzero_mock.return_value client = energyzero_mock.return_value
client.energy_prices.return_value = Electricity.from_dict( client.energy_prices.return_value = Electricity.from_dict(
json.loads(load_fixture("today_energy.json", DOMAIN)) await async_load_json_object_fixture(hass, "today_energy.json", DOMAIN)
) )
client.gas_prices.return_value = Gas.from_dict( client.gas_prices.return_value = Gas.from_dict(
json.loads(load_fixture("today_gas.json", DOMAIN)) await async_load_json_object_fixture(hass, "today_gas.json", DOMAIN)
) )
yield client yield client