Use async_load_fixture in async test functions (a) (#145718)

This commit is contained in:
epenet 2025-05-28 15:51:49 +02:00 committed by GitHub
parent c3ade400fb
commit bd5fef1ddb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 69 additions and 46 deletions

View File

@ -4,7 +4,7 @@ from homeassistant.components.agent_dvr.const import DOMAIN, SERVER_URL
from homeassistant.const import CONF_HOST, CONF_PORT, CONTENT_TYPE_JSON
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture
from tests.common import MockConfigEntry, async_load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
CONF_DATA = {
@ -34,12 +34,12 @@ async def init_integration(
aioclient_mock.get(
"http://example.local:8090/command.cgi?cmd=getStatus",
text=load_fixture("agent_dvr/status.json"),
text=await async_load_fixture(hass, "status.json", DOMAIN),
headers={"Content-Type": CONTENT_TYPE_JSON},
)
aioclient_mock.get(
"http://example.local:8090/command.cgi?cmd=getObjects",
text=load_fixture("agent_dvr/objects.json"),
text=await async_load_fixture(hass, "objects.json", DOMAIN),
headers={"Content-Type": CONTENT_TYPE_JSON},
)
entry = create_entry(hass)

View File

@ -2,8 +2,7 @@
import pytest
from homeassistant.components.agent_dvr import config_flow
from homeassistant.components.agent_dvr.const import SERVER_URL
from homeassistant.components.agent_dvr.const import DOMAIN, SERVER_URL
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_HOST, CONF_PORT, CONTENT_TYPE_JSON
from homeassistant.core import HomeAssistant
@ -11,7 +10,7 @@ from homeassistant.data_entry_flow import FlowResultType
from . import init_integration
from tests.common import load_fixture
from tests.common import async_load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
@ -20,7 +19,7 @@ pytestmark = pytest.mark.usefixtures("mock_setup_entry")
async def test_show_user_form(hass: HomeAssistant) -> None:
"""Test that the user set up form is served."""
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
DOMAIN,
context={"source": SOURCE_USER},
)
@ -35,7 +34,7 @@ async def test_user_device_exists_abort(
await init_integration(hass, aioclient_mock)
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
DOMAIN,
context={"source": SOURCE_USER},
data={CONF_HOST: "example.local", CONF_PORT: 8090},
)
@ -51,7 +50,7 @@ async def test_connection_error(
aioclient_mock.get("http://example.local:8090/command.cgi?cmd=getStatus", text="")
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
DOMAIN,
context={"source": SOURCE_USER},
data={CONF_HOST: "example.local", CONF_PORT: 8090},
)
@ -67,18 +66,18 @@ async def test_full_user_flow_implementation(
"""Test the full manual user flow from start to finish."""
aioclient_mock.get(
"http://example.local:8090/command.cgi?cmd=getStatus",
text=load_fixture("agent_dvr/status.json"),
text=await async_load_fixture(hass, "status.json", DOMAIN),
headers={"Content-Type": CONTENT_TYPE_JSON},
)
aioclient_mock.get(
"http://example.local:8090/command.cgi?cmd=getObjects",
text=load_fixture("agent_dvr/objects.json"),
text=await async_load_fixture(hass, "objects.json", DOMAIN),
headers={"Content-Type": CONTENT_TYPE_JSON},
)
result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN,
DOMAIN,
context={"source": SOURCE_USER},
)
@ -95,5 +94,5 @@ async def test_full_user_flow_implementation(
assert result["title"] == "DESKTOP"
assert result["type"] is FlowResultType.CREATE_ENTRY
entries = hass.config_entries.async_entries(config_flow.DOMAIN)
entries = hass.config_entries.async_entries(DOMAIN)
assert entries[0].unique_id == "c0715bba-c2d0-48ef-9e3e-bc81c9ea4447"

View File

@ -20,7 +20,7 @@ from . import setup_integration
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_fixture,
async_load_fixture,
snapshot_platform,
)
@ -81,7 +81,7 @@ async def test_cloud_creates_no_button(
assert len(hass.states.async_all()) == 0
mock_cloud_airgradient_client.get_config.return_value = Config.from_json(
load_fixture("get_config_local.json", DOMAIN)
await async_load_fixture(hass, "get_config_local.json", DOMAIN)
)
freezer.tick(timedelta(minutes=5))
@ -91,7 +91,7 @@ async def test_cloud_creates_no_button(
assert len(hass.states.async_all()) == 2
mock_cloud_airgradient_client.get_config.return_value = Config.from_json(
load_fixture("get_config_cloud.json", DOMAIN)
await async_load_fixture(hass, "get_config_cloud.json", DOMAIN)
)
freezer.tick(timedelta(minutes=5))

View File

@ -24,7 +24,7 @@ from . import setup_integration
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_fixture,
async_load_fixture,
snapshot_platform,
)
@ -83,7 +83,7 @@ async def test_cloud_creates_no_number(
assert len(hass.states.async_all()) == 0
mock_cloud_airgradient_client.get_config.return_value = Config.from_json(
load_fixture("get_config_local.json", DOMAIN)
await async_load_fixture(hass, "get_config_local.json", DOMAIN)
)
freezer.tick(timedelta(minutes=5))
@ -93,7 +93,7 @@ async def test_cloud_creates_no_number(
assert len(hass.states.async_all()) == 2
mock_cloud_airgradient_client.get_config.return_value = Config.from_json(
load_fixture("get_config_cloud.json", DOMAIN)
await async_load_fixture(hass, "get_config_cloud.json", DOMAIN)
)
freezer.tick(timedelta(minutes=5))

View File

@ -23,7 +23,7 @@ from . import setup_integration
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_fixture,
async_load_fixture,
snapshot_platform,
)
@ -77,7 +77,7 @@ async def test_cloud_creates_no_number(
assert len(hass.states.async_all()) == 1
mock_cloud_airgradient_client.get_config.return_value = Config.from_json(
load_fixture("get_config_local.json", DOMAIN)
await async_load_fixture(hass, "get_config_local.json", DOMAIN)
)
freezer.tick(timedelta(minutes=5))
@ -87,7 +87,7 @@ async def test_cloud_creates_no_number(
assert len(hass.states.async_all()) == 7
mock_cloud_airgradient_client.get_config.return_value = Config.from_json(
load_fixture("get_config_cloud.json", DOMAIN)
await async_load_fixture(hass, "get_config_cloud.json", DOMAIN)
)
freezer.tick(timedelta(minutes=5))

View File

@ -18,7 +18,7 @@ from . import setup_integration
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_fixture,
async_load_fixture,
snapshot_platform,
)
@ -46,14 +46,14 @@ async def test_create_entities(
) -> None:
"""Test creating entities."""
mock_airgradient_client.get_current_measures.return_value = Measures.from_json(
load_fixture("measures_after_boot.json", DOMAIN)
await async_load_fixture(hass, "measures_after_boot.json", DOMAIN)
)
with patch("homeassistant.components.airgradient.PLATFORMS", [Platform.SENSOR]):
await setup_integration(hass, mock_config_entry)
assert len(hass.states.async_all()) == 0
mock_airgradient_client.get_current_measures.return_value = Measures.from_json(
load_fixture("current_measures_indoor.json", DOMAIN)
await async_load_fixture(hass, "current_measures_indoor.json", DOMAIN)
)
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)

View File

@ -25,7 +25,7 @@ from . import setup_integration
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_fixture,
async_load_fixture,
snapshot_platform,
)
@ -83,7 +83,7 @@ async def test_cloud_creates_no_switch(
assert len(hass.states.async_all()) == 0
mock_cloud_airgradient_client.get_config.return_value = Config.from_json(
load_fixture("get_config_local.json", DOMAIN)
await async_load_fixture(hass, "get_config_local.json", DOMAIN)
)
freezer.tick(timedelta(minutes=5))
@ -93,7 +93,7 @@ async def test_cloud_creates_no_switch(
assert len(hass.states.async_all()) == 1
mock_cloud_airgradient_client.get_config.return_value = Config.from_json(
load_fixture("get_config_cloud.json", DOMAIN)
await async_load_fixture(hass, "get_config_cloud.json", DOMAIN)
)
freezer.tick(timedelta(minutes=5))

View File

@ -3,7 +3,7 @@
from homeassistant.components.airly.const import DOMAIN
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture
from tests.common import MockConfigEntry, async_load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
API_NEAREST_URL = "https://airapi.airly.eu/v2/measurements/nearest?lat=123.000000&lng=456.000000&maxDistanceKM=5.000000"
@ -34,7 +34,9 @@ async def init_integration(
)
aioclient_mock.get(
API_POINT_URL, text=load_fixture("valid_station.json", DOMAIN), headers=HEADERS
API_POINT_URL,
text=await async_load_fixture(hass, "valid_station.json", DOMAIN),
headers=HEADERS,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)

View File

@ -12,7 +12,7 @@ from homeassistant.data_entry_flow import FlowResultType
from . import API_NEAREST_URL, API_POINT_URL
from tests.common import MockConfigEntry, load_fixture, patch
from tests.common import MockConfigEntry, async_load_fixture, patch
from tests.test_util.aiohttp import AiohttpClientMocker
CONFIG = {
@ -55,7 +55,9 @@ async def test_invalid_location(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that errors are shown when location is invalid."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("no_station.json", "airly"))
aioclient_mock.get(
API_POINT_URL, text=await async_load_fixture(hass, "no_station.json", DOMAIN)
)
aioclient_mock.get(
API_NEAREST_URL,
@ -74,9 +76,13 @@ async def test_invalid_location_for_point_and_nearest(
) -> None:
"""Test an abort when the location is wrong for the point and nearest methods."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("no_station.json", "airly"))
aioclient_mock.get(
API_POINT_URL, text=await async_load_fixture(hass, "no_station.json", DOMAIN)
)
aioclient_mock.get(API_NEAREST_URL, text=load_fixture("no_station.json", "airly"))
aioclient_mock.get(
API_NEAREST_URL, text=await async_load_fixture(hass, "no_station.json", DOMAIN)
)
with patch("homeassistant.components.airly.async_setup_entry", return_value=True):
result = await hass.config_entries.flow.async_init(
@ -91,7 +97,9 @@ async def test_duplicate_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that errors are shown when duplicates are added."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
aioclient_mock.get(
API_POINT_URL, text=await async_load_fixture(hass, "valid_station.json", DOMAIN)
)
MockConfigEntry(domain=DOMAIN, unique_id="123-456", data=CONFIG).add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
@ -106,7 +114,9 @@ async def test_create_entry(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that the user step works."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
aioclient_mock.get(
API_POINT_URL, text=await async_load_fixture(hass, "valid_station.json", DOMAIN)
)
with patch("homeassistant.components.airly.async_setup_entry", return_value=True):
result = await hass.config_entries.flow.async_init(
@ -126,10 +136,13 @@ async def test_create_entry_with_nearest_method(
) -> None:
"""Test that the user step works with nearest method."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("no_station.json", "airly"))
aioclient_mock.get(
API_POINT_URL, text=await async_load_fixture(hass, "no_station.json", DOMAIN)
)
aioclient_mock.get(
API_NEAREST_URL, text=load_fixture("valid_station.json", "airly")
API_NEAREST_URL,
text=await async_load_fixture(hass, "valid_station.json", DOMAIN),
)
with patch("homeassistant.components.airly.async_setup_entry", return_value=True):

View File

@ -15,7 +15,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
from . import API_POINT_URL, init_integration
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
from tests.common import MockConfigEntry, async_fire_time_changed, async_load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
@ -69,7 +69,9 @@ async def test_config_without_unique_id(
},
)
aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
aioclient_mock.get(
API_POINT_URL, text=await async_load_fixture(hass, "valid_station.json", DOMAIN)
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.LOADED
@ -92,7 +94,9 @@ async def test_config_with_turned_off_station(
},
)
aioclient_mock.get(API_POINT_URL, text=load_fixture("no_station.json", "airly"))
aioclient_mock.get(
API_POINT_URL, text=await async_load_fixture(hass, "no_station.json", DOMAIN)
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.SETUP_RETRY
@ -124,7 +128,7 @@ async def test_update_interval(
aioclient_mock.get(
API_POINT_URL,
text=load_fixture("valid_station.json", "airly"),
text=await async_load_fixture(hass, "valid_station.json", DOMAIN),
headers=HEADERS,
)
entry.add_to_hass(hass)
@ -159,7 +163,7 @@ async def test_update_interval(
aioclient_mock.get(
"https://airapi.airly.eu/v2/measurements/point?lat=66.660000&lng=111.110000",
text=load_fixture("valid_station.json", "airly"),
text=await async_load_fixture(hass, "valid_station.json", DOMAIN),
headers=HEADERS,
)
entry.add_to_hass(hass)
@ -216,7 +220,9 @@ async def test_migrate_device_entry(
},
)
aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
aioclient_mock.get(
API_POINT_URL, text=await async_load_fixture(hass, "valid_station.json", DOMAIN)
)
config_entry.add_to_hass(hass)
device_entry = device_registry.async_get_or_create(

View File

@ -7,6 +7,7 @@ from unittest.mock import patch
from airly.exceptions import AirlyError
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.airly.const import DOMAIN
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@ -15,7 +16,7 @@ from homeassistant.util.dt import utcnow
from . import API_POINT_URL, init_integration
from tests.common import async_fire_time_changed, load_fixture
from tests.common import async_fire_time_changed, async_load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
@ -62,7 +63,9 @@ async def test_availability(
assert state.state == STATE_UNAVAILABLE
aioclient_mock.clear_requests()
aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
aioclient_mock.get(
API_POINT_URL, text=await async_load_fixture(hass, "valid_station.json", DOMAIN)
)
future = utcnow() + timedelta(minutes=120)
async_fire_time_changed(hass, future)
await hass.async_block_till_done()