Use async_load_fixture in twitch tests (#146016)

This commit is contained in:
epenet 2025-06-02 09:27:53 +02:00 committed by GitHub
parent 89a40f1c48
commit ebfbea39ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 14 deletions

View File

@ -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

View File

@ -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(

View File

@ -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

View File

@ -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