From bd5fef1ddb6c79bd84a10e3894fb6543393fedfe Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 28 May 2025 15:51:49 +0200 Subject: [PATCH] Use async_load_fixture in async test functions (a) (#145718) --- tests/components/agent_dvr/__init__.py | 6 ++-- .../components/agent_dvr/test_config_flow.py | 19 ++++++------ tests/components/airgradient/test_button.py | 6 ++-- tests/components/airgradient/test_number.py | 6 ++-- tests/components/airgradient/test_select.py | 6 ++-- tests/components/airgradient/test_sensor.py | 6 ++-- tests/components/airgradient/test_switch.py | 6 ++-- tests/components/airly/__init__.py | 6 ++-- tests/components/airly/test_config_flow.py | 29 ++++++++++++++----- tests/components/airly/test_init.py | 18 ++++++++---- tests/components/airly/test_sensor.py | 7 +++-- 11 files changed, 69 insertions(+), 46 deletions(-) diff --git a/tests/components/agent_dvr/__init__.py b/tests/components/agent_dvr/__init__.py index 3f2fc82101a..39618ab54b8 100644 --- a/tests/components/agent_dvr/__init__.py +++ b/tests/components/agent_dvr/__init__.py @@ -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) diff --git a/tests/components/agent_dvr/test_config_flow.py b/tests/components/agent_dvr/test_config_flow.py index fee8a40f4f7..88332b833a6 100644 --- a/tests/components/agent_dvr/test_config_flow.py +++ b/tests/components/agent_dvr/test_config_flow.py @@ -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" diff --git a/tests/components/airgradient/test_button.py b/tests/components/airgradient/test_button.py index 51fbd87ba67..cdcc05413c3 100644 --- a/tests/components/airgradient/test_button.py +++ b/tests/components/airgradient/test_button.py @@ -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)) diff --git a/tests/components/airgradient/test_number.py b/tests/components/airgradient/test_number.py index 6fa1a7d3e07..9d45cc83d24 100644 --- a/tests/components/airgradient/test_number.py +++ b/tests/components/airgradient/test_number.py @@ -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)) diff --git a/tests/components/airgradient/test_select.py b/tests/components/airgradient/test_select.py index 8782af4e46a..872d87f6e58 100644 --- a/tests/components/airgradient/test_select.py +++ b/tests/components/airgradient/test_select.py @@ -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)) diff --git a/tests/components/airgradient/test_sensor.py b/tests/components/airgradient/test_sensor.py index 7679ba48546..5c2976b97ef 100644 --- a/tests/components/airgradient/test_sensor.py +++ b/tests/components/airgradient/test_sensor.py @@ -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) diff --git a/tests/components/airgradient/test_switch.py b/tests/components/airgradient/test_switch.py index 12b319379f6..2bbd3ea808b 100644 --- a/tests/components/airgradient/test_switch.py +++ b/tests/components/airgradient/test_switch.py @@ -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)) diff --git a/tests/components/airly/__init__.py b/tests/components/airly/__init__.py index c87c41b5162..401bf641350 100644 --- a/tests/components/airly/__init__.py +++ b/tests/components/airly/__init__.py @@ -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) diff --git a/tests/components/airly/test_config_flow.py b/tests/components/airly/test_config_flow.py index 7c0cac805d3..482c97799f6 100644 --- a/tests/components/airly/test_config_flow.py +++ b/tests/components/airly/test_config_flow.py @@ -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): diff --git a/tests/components/airly/test_init.py b/tests/components/airly/test_init.py index 6fc26110186..b7fa8a44360 100644 --- a/tests/components/airly/test_init.py +++ b/tests/components/airly/test_init.py @@ -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( diff --git a/tests/components/airly/test_sensor.py b/tests/components/airly/test_sensor.py index f45bbb65f6f..970ec4e0e2b 100644 --- a/tests/components/airly/test_sensor.py +++ b/tests/components/airly/test_sensor.py @@ -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()