mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Use async_load_fixture in youtube tests (#146018)
This commit is contained in:
parent
7b699f7733
commit
8afec8ada9
@ -6,7 +6,10 @@ import json
|
|||||||
from youtubeaio.models import YouTubeChannel, YouTubePlaylistItem, YouTubeSubscription
|
from youtubeaio.models import YouTubeChannel, YouTubePlaylistItem, YouTubeSubscription
|
||||||
from youtubeaio.types import AuthScope
|
from youtubeaio.types import AuthScope
|
||||||
|
|
||||||
from tests.common import load_fixture
|
from homeassistant.components.youtube import DOMAIN
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from tests.common import async_load_fixture
|
||||||
|
|
||||||
|
|
||||||
class MockYouTube:
|
class MockYouTube:
|
||||||
@ -16,11 +19,13 @@ class MockYouTube:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
channel_fixture: str = "youtube/get_channel.json",
|
hass: HomeAssistant,
|
||||||
playlist_items_fixture: str = "youtube/get_playlist_items.json",
|
channel_fixture: str = "get_channel.json",
|
||||||
subscriptions_fixture: str = "youtube/get_subscriptions.json",
|
playlist_items_fixture: str = "get_playlist_items.json",
|
||||||
|
subscriptions_fixture: str = "get_subscriptions.json",
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize mock service."""
|
"""Initialize mock service."""
|
||||||
|
self.hass = hass
|
||||||
self._channel_fixture = channel_fixture
|
self._channel_fixture = channel_fixture
|
||||||
self._playlist_items_fixture = playlist_items_fixture
|
self._playlist_items_fixture = playlist_items_fixture
|
||||||
self._subscriptions_fixture = subscriptions_fixture
|
self._subscriptions_fixture = subscriptions_fixture
|
||||||
@ -32,7 +37,9 @@ class MockYouTube:
|
|||||||
|
|
||||||
async def get_user_channels(self) -> AsyncGenerator[YouTubeChannel]:
|
async def get_user_channels(self) -> AsyncGenerator[YouTubeChannel]:
|
||||||
"""Get channels for authenticated user."""
|
"""Get channels for authenticated user."""
|
||||||
channels = json.loads(load_fixture(self._channel_fixture))
|
channels = json.loads(
|
||||||
|
await async_load_fixture(self.hass, self._channel_fixture, DOMAIN)
|
||||||
|
)
|
||||||
for item in channels["items"]:
|
for item in channels["items"]:
|
||||||
yield YouTubeChannel(**item)
|
yield YouTubeChannel(**item)
|
||||||
|
|
||||||
@ -42,7 +49,9 @@ class MockYouTube:
|
|||||||
"""Get channels."""
|
"""Get channels."""
|
||||||
if self._thrown_error is not None:
|
if self._thrown_error is not None:
|
||||||
raise self._thrown_error
|
raise self._thrown_error
|
||||||
channels = json.loads(load_fixture(self._channel_fixture))
|
channels = json.loads(
|
||||||
|
await async_load_fixture(self.hass, self._channel_fixture, DOMAIN)
|
||||||
|
)
|
||||||
for item in channels["items"]:
|
for item in channels["items"]:
|
||||||
yield YouTubeChannel(**item)
|
yield YouTubeChannel(**item)
|
||||||
|
|
||||||
@ -50,13 +59,17 @@ class MockYouTube:
|
|||||||
self, playlist_id: str, amount: int
|
self, playlist_id: str, amount: int
|
||||||
) -> AsyncGenerator[YouTubePlaylistItem]:
|
) -> AsyncGenerator[YouTubePlaylistItem]:
|
||||||
"""Get channels."""
|
"""Get channels."""
|
||||||
channels = json.loads(load_fixture(self._playlist_items_fixture))
|
channels = json.loads(
|
||||||
|
await async_load_fixture(self.hass, self._playlist_items_fixture, DOMAIN)
|
||||||
|
)
|
||||||
for item in channels["items"]:
|
for item in channels["items"]:
|
||||||
yield YouTubePlaylistItem(**item)
|
yield YouTubePlaylistItem(**item)
|
||||||
|
|
||||||
async def get_user_subscriptions(self) -> AsyncGenerator[YouTubeSubscription]:
|
async def get_user_subscriptions(self) -> AsyncGenerator[YouTubeSubscription]:
|
||||||
"""Get channels for authenticated user."""
|
"""Get channels for authenticated user."""
|
||||||
channels = json.loads(load_fixture(self._subscriptions_fixture))
|
channels = json.loads(
|
||||||
|
await async_load_fixture(self.hass, self._subscriptions_fixture, DOMAIN)
|
||||||
|
)
|
||||||
for item in channels["items"]:
|
for item in channels["items"]:
|
||||||
yield YouTubeSubscription(**item)
|
yield YouTubeSubscription(**item)
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ async def mock_setup_integration(
|
|||||||
)
|
)
|
||||||
|
|
||||||
async def func() -> MockYouTube:
|
async def func() -> MockYouTube:
|
||||||
mock = MockYouTube()
|
mock = MockYouTube(hass)
|
||||||
with patch("homeassistant.components.youtube.api.YouTube", return_value=mock):
|
with patch("homeassistant.components.youtube.api.YouTube", return_value=mock):
|
||||||
assert await async_setup_component(hass, DOMAIN, {})
|
assert await async_setup_component(hass, DOMAIN, {})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
@ -61,7 +61,7 @@ async def test_full_flow(
|
|||||||
) as mock_setup,
|
) as mock_setup,
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.youtube.config_flow.YouTube",
|
"homeassistant.components.youtube.config_flow.YouTube",
|
||||||
return_value=MockYouTube(),
|
return_value=MockYouTube(hass),
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||||
@ -114,7 +114,7 @@ async def test_flow_abort_without_channel(
|
|||||||
assert resp.status == 200
|
assert resp.status == 200
|
||||||
assert resp.headers["content-type"] == "text/html; charset=utf-8"
|
assert resp.headers["content-type"] == "text/html; charset=utf-8"
|
||||||
|
|
||||||
service = MockYouTube(channel_fixture="youtube/get_no_channel.json")
|
service = MockYouTube(hass, channel_fixture="get_no_channel.json")
|
||||||
with (
|
with (
|
||||||
patch("homeassistant.components.youtube.async_setup_entry", return_value=True),
|
patch("homeassistant.components.youtube.async_setup_entry", return_value=True),
|
||||||
patch(
|
patch(
|
||||||
@ -156,8 +156,9 @@ async def test_flow_abort_without_subscriptions(
|
|||||||
assert resp.headers["content-type"] == "text/html; charset=utf-8"
|
assert resp.headers["content-type"] == "text/html; charset=utf-8"
|
||||||
|
|
||||||
service = MockYouTube(
|
service = MockYouTube(
|
||||||
channel_fixture="youtube/get_no_channel.json",
|
hass,
|
||||||
subscriptions_fixture="youtube/get_no_subscriptions.json",
|
channel_fixture="get_no_channel.json",
|
||||||
|
subscriptions_fixture="get_no_subscriptions.json",
|
||||||
)
|
)
|
||||||
with (
|
with (
|
||||||
patch("homeassistant.components.youtube.async_setup_entry", return_value=True),
|
patch("homeassistant.components.youtube.async_setup_entry", return_value=True),
|
||||||
@ -199,7 +200,7 @@ async def test_flow_without_subscriptions(
|
|||||||
assert resp.status == 200
|
assert resp.status == 200
|
||||||
assert resp.headers["content-type"] == "text/html; charset=utf-8"
|
assert resp.headers["content-type"] == "text/html; charset=utf-8"
|
||||||
|
|
||||||
service = MockYouTube(subscriptions_fixture="youtube/get_no_subscriptions.json")
|
service = MockYouTube(hass, subscriptions_fixture="get_no_subscriptions.json")
|
||||||
with (
|
with (
|
||||||
patch("homeassistant.components.youtube.async_setup_entry", return_value=True),
|
patch("homeassistant.components.youtube.async_setup_entry", return_value=True),
|
||||||
patch(
|
patch(
|
||||||
@ -352,7 +353,7 @@ async def test_reauth(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
youtube = MockYouTube(channel_fixture=f"youtube/{fixture}.json")
|
youtube = MockYouTube(hass, channel_fixture=f"{fixture}.json")
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.youtube.async_setup_entry", return_value=True
|
"homeassistant.components.youtube.async_setup_entry", return_value=True
|
||||||
@ -422,7 +423,7 @@ async def test_options_flow(
|
|||||||
await setup_integration()
|
await setup_integration()
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.youtube.config_flow.YouTube",
|
"homeassistant.components.youtube.config_flow.YouTube",
|
||||||
return_value=MockYouTube(),
|
return_value=MockYouTube(hass),
|
||||||
):
|
):
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||||
@ -476,7 +477,7 @@ async def test_own_channel_included(
|
|||||||
) as mock_setup,
|
) as mock_setup,
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.youtube.config_flow.YouTube",
|
"homeassistant.components.youtube.config_flow.YouTube",
|
||||||
return_value=MockYouTube(),
|
return_value=MockYouTube(hass),
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||||
@ -522,7 +523,7 @@ async def test_options_flow_own_channel(
|
|||||||
await setup_integration()
|
await setup_integration()
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.youtube.config_flow.YouTube",
|
"homeassistant.components.youtube.config_flow.YouTube",
|
||||||
return_value=MockYouTube(),
|
return_value=MockYouTube(hass),
|
||||||
):
|
):
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""Sensor tests for the YouTube integration."""
|
"""Sensor tests for the YouTube integration."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
@ -42,12 +43,13 @@ async def test_sensor_without_uploaded_video(
|
|||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.youtube.api.AsyncConfigEntryAuth.get_resource",
|
"homeassistant.components.youtube.api.AsyncConfigEntryAuth.get_resource",
|
||||||
return_value=MockYouTube(
|
return_value=MockYouTube(
|
||||||
playlist_items_fixture="youtube/get_no_playlist_items.json"
|
hass, playlist_items_fixture="get_no_playlist_items.json"
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
future = dt_util.utcnow() + timedelta(minutes=15)
|
future = dt_util.utcnow() + timedelta(minutes=15)
|
||||||
async_fire_time_changed(hass, future)
|
async_fire_time_changed(hass, future)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
state = hass.states.get("sensor.google_for_developers_latest_upload")
|
state = hass.states.get("sensor.google_for_developers_latest_upload")
|
||||||
assert state == snapshot
|
assert state == snapshot
|
||||||
@ -72,12 +74,13 @@ async def test_sensor_updating(
|
|||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.youtube.api.AsyncConfigEntryAuth.get_resource",
|
"homeassistant.components.youtube.api.AsyncConfigEntryAuth.get_resource",
|
||||||
return_value=MockYouTube(
|
return_value=MockYouTube(
|
||||||
playlist_items_fixture="youtube/get_playlist_items_2.json"
|
hass, playlist_items_fixture="get_playlist_items_2.json"
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
future = dt_util.utcnow() + timedelta(minutes=15)
|
future = dt_util.utcnow() + timedelta(minutes=15)
|
||||||
async_fire_time_changed(hass, future)
|
async_fire_time_changed(hass, future)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
state = hass.states.get("sensor.google_for_developers_latest_upload")
|
state = hass.states.get("sensor.google_for_developers_latest_upload")
|
||||||
assert state
|
assert state
|
||||||
assert state.name == "Google for Developers Latest upload"
|
assert state.name == "Google for Developers Latest upload"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user