From ebfbea39ff434c3e8717de29922e1f523a2fa7db Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 2 Jun 2025 09:27:53 +0200 Subject: [PATCH] Use async_load_fixture in twitch tests (#146016) --- tests/components/twitch/__init__.py | 26 +++++++++++++++------ tests/components/twitch/conftest.py | 8 +++---- tests/components/twitch/test_config_flow.py | 2 +- tests/components/twitch/test_sensor.py | 4 ++-- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/tests/components/twitch/__init__.py b/tests/components/twitch/__init__.py index 1887861f6e5..d961e1ed4f0 100644 --- a/tests/components/twitch/__init__.py +++ b/tests/components/twitch/__init__.py @@ -7,8 +7,9 @@ from twitchAPI.object.base import TwitchObject from homeassistant.components.twitch.const import DOMAIN from homeassistant.core import HomeAssistant +from homeassistant.util.json import JsonArrayType -from tests.common import MockConfigEntry, load_json_array_fixture +from tests.common import MockConfigEntry, async_load_json_array_fixture async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: @@ -25,24 +26,35 @@ TwitchType = TypeVar("TwitchType", bound=TwitchObject) class TwitchIterObject(Generic[TwitchType]): """Twitch object iterator.""" - def __init__(self, fixture: str, target_type: type[TwitchType]) -> None: + raw_data: JsonArrayType + data: list + total: int + + def __init__( + self, hass: HomeAssistant, fixture: str, target_type: type[TwitchType] + ) -> None: """Initialize object.""" - self.raw_data = load_json_array_fixture(fixture, DOMAIN) - self.data = [target_type(**item) for item in self.raw_data] - self.total = len(self.raw_data) + self.hass = hass + self.fixture = fixture self.target_type = target_type async def __aiter__(self) -> AsyncIterator[TwitchType]: """Return async iterator.""" + if not hasattr(self, "raw_data"): + self.raw_data = await async_load_json_array_fixture( + self.hass, self.fixture, DOMAIN + ) + self.data = [self.target_type(**item) for item in self.raw_data] + self.total = len(self.raw_data) async for item in get_generator_from_data(self.raw_data, self.target_type): yield item async def get_generator( - fixture: str, target_type: type[TwitchType] + hass: HomeAssistant, fixture: str, target_type: type[TwitchType] ) -> AsyncGenerator[TwitchType]: """Return async generator.""" - data = load_json_array_fixture(fixture, DOMAIN) + data = await async_load_json_array_fixture(hass, fixture, DOMAIN) async for item in get_generator_from_data(data, target_type): yield item diff --git a/tests/components/twitch/conftest.py b/tests/components/twitch/conftest.py index 07732de1b0c..bc48bb4bd44 100644 --- a/tests/components/twitch/conftest.py +++ b/tests/components/twitch/conftest.py @@ -93,7 +93,7 @@ def mock_connection(aioclient_mock: AiohttpClientMocker) -> None: @pytest.fixture -def twitch_mock() -> Generator[AsyncMock]: +def twitch_mock(hass: HomeAssistant) -> Generator[AsyncMock]: """Return as fixture to inject other mocks.""" with ( patch( @@ -106,13 +106,13 @@ def twitch_mock() -> Generator[AsyncMock]: ), ): mock_client.return_value.get_users = lambda *args, **kwargs: get_generator( - "get_users.json", TwitchUser + hass, "get_users.json", TwitchUser ) mock_client.return_value.get_followed_channels.return_value = TwitchIterObject( - "get_followed_channels.json", FollowedChannel + hass, "get_followed_channels.json", FollowedChannel ) mock_client.return_value.get_followed_streams.return_value = get_generator( - "get_followed_streams.json", Stream + hass, "get_followed_streams.json", Stream ) mock_client.return_value.check_user_subscription.return_value = ( UserSubscription( diff --git a/tests/components/twitch/test_config_flow.py b/tests/components/twitch/test_config_flow.py index fc53b17551c..249f47ed308 100644 --- a/tests/components/twitch/test_config_flow.py +++ b/tests/components/twitch/test_config_flow.py @@ -175,7 +175,7 @@ async def test_reauth_wrong_account( """Check reauth flow.""" await setup_integration(hass, config_entry) twitch_mock.return_value.get_users = lambda *args, **kwargs: get_generator( - "get_users_2.json", TwitchUser + hass, "get_users_2.json", TwitchUser ) result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM diff --git a/tests/components/twitch/test_sensor.py b/tests/components/twitch/test_sensor.py index 8f4bfb40e4f..6bfc311c65d 100644 --- a/tests/components/twitch/test_sensor.py +++ b/tests/components/twitch/test_sensor.py @@ -53,7 +53,7 @@ async def test_oauth_without_sub_and_follow( ) -> None: """Test state with oauth.""" twitch_mock.return_value.get_followed_channels.return_value = TwitchIterObject( - "empty_response.json", FollowedChannel + hass, "empty_response.json", FollowedChannel ) twitch_mock.return_value.check_user_subscription.side_effect = ( TwitchResourceNotFound @@ -70,7 +70,7 @@ async def test_oauth_with_sub( ) -> None: """Test state with oauth and sub.""" twitch_mock.return_value.get_followed_channels.return_value = TwitchIterObject( - "empty_response.json", FollowedChannel + hass, "empty_response.json", FollowedChannel ) subscription = await async_load_json_object_fixture( hass, "check_user_subscription_2.json", DOMAIN