Use service_calls fixture in media_extractor tests (#120935)

This commit is contained in:
epenet 2024-07-01 17:32:17 +02:00 committed by GitHub
parent 8a23e37837
commit c4903dd982
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 23 deletions

View file

@ -7,14 +7,12 @@ from unittest.mock import AsyncMock, patch
import pytest import pytest
from homeassistant.components.media_extractor import DOMAIN from homeassistant.components.media_extractor import DOMAIN
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from . import MockYoutubeDL from . import MockYoutubeDL
from .const import AUDIO_QUERY from .const import AUDIO_QUERY
from tests.common import async_mock_service
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
async def setup_homeassistant(hass: HomeAssistant): async def setup_homeassistant(hass: HomeAssistant):
@ -31,12 +29,6 @@ async def setup_media_player(hass: HomeAssistant) -> None:
await hass.async_block_till_done() await hass.async_block_till_done()
@pytest.fixture
def calls(hass: HomeAssistant) -> list[ServiceCall]:
"""Track calls to a mock service."""
return async_mock_service(hass, "media_player", "play_media")
@pytest.fixture(name="mock_youtube_dl") @pytest.fixture(name="mock_youtube_dl")
async def setup_mock_yt_dlp(hass: HomeAssistant) -> MockYoutubeDL: async def setup_mock_yt_dlp(hass: HomeAssistant) -> MockYoutubeDL:
"""Mock YoutubeDL.""" """Mock YoutubeDL."""

View file

@ -100,7 +100,7 @@ async def test_extracting_playlist_no_entries(
async def test_play_media_service( async def test_play_media_service(
hass: HomeAssistant, hass: HomeAssistant,
mock_youtube_dl: MockYoutubeDL, mock_youtube_dl: MockYoutubeDL,
calls: list[ServiceCall], service_calls: list[ServiceCall],
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
request: pytest.FixtureRequest, request: pytest.FixtureRequest,
config_fixture: str, config_fixture: str,
@ -123,13 +123,14 @@ async def test_play_media_service(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert calls[0].data == snapshot assert len(service_calls) == 2
assert service_calls[1].data == snapshot
async def test_download_error( async def test_download_error(
hass: HomeAssistant, hass: HomeAssistant,
empty_media_extractor_config: dict[str, Any], empty_media_extractor_config: dict[str, Any],
calls: list[ServiceCall], service_calls: list[ServiceCall],
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test handling DownloadError.""" """Test handling DownloadError."""
@ -152,7 +153,7 @@ async def test_download_error(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 1
assert f"Could not retrieve data for the URL: {YOUTUBE_VIDEO}" in caplog.text assert f"Could not retrieve data for the URL: {YOUTUBE_VIDEO}" in caplog.text
@ -160,7 +161,7 @@ async def test_no_target_entity(
hass: HomeAssistant, hass: HomeAssistant,
mock_youtube_dl: MockYoutubeDL, mock_youtube_dl: MockYoutubeDL,
empty_media_extractor_config: dict[str, Any], empty_media_extractor_config: dict[str, Any],
calls: list[ServiceCall], service_calls: list[ServiceCall],
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test having no target entity.""" """Test having no target entity."""
@ -179,14 +180,15 @@ async def test_no_target_entity(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert calls[0].data == snapshot assert len(service_calls) == 2
assert service_calls[1].data == snapshot
async def test_playlist( async def test_playlist(
hass: HomeAssistant, hass: HomeAssistant,
mock_youtube_dl: MockYoutubeDL, mock_youtube_dl: MockYoutubeDL,
empty_media_extractor_config: dict[str, Any], empty_media_extractor_config: dict[str, Any],
calls: list[ServiceCall], service_calls: list[ServiceCall],
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test extracting a playlist.""" """Test extracting a playlist."""
@ -205,14 +207,15 @@ async def test_playlist(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert calls[0].data == snapshot assert len(service_calls) == 2
assert service_calls[1].data == snapshot
async def test_playlist_no_entries( async def test_playlist_no_entries(
hass: HomeAssistant, hass: HomeAssistant,
mock_youtube_dl: MockYoutubeDL, mock_youtube_dl: MockYoutubeDL,
empty_media_extractor_config: dict[str, Any], empty_media_extractor_config: dict[str, Any],
calls: list[ServiceCall], service_calls: list[ServiceCall],
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test extracting a playlist without entries.""" """Test extracting a playlist without entries."""
@ -231,7 +234,7 @@ async def test_playlist_no_entries(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 1
assert ( assert (
f"Could not retrieve data for the URL: {YOUTUBE_EMPTY_PLAYLIST}" in caplog.text f"Could not retrieve data for the URL: {YOUTUBE_EMPTY_PLAYLIST}" in caplog.text
) )
@ -240,7 +243,7 @@ async def test_playlist_no_entries(
async def test_query_error( async def test_query_error(
hass: HomeAssistant, hass: HomeAssistant,
empty_media_extractor_config: dict[str, Any], empty_media_extractor_config: dict[str, Any],
calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test handling error with query.""" """Test handling error with query."""
@ -270,15 +273,13 @@ async def test_query_error(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 0 assert len(service_calls) == 1
async def test_cookiefile_detection( async def test_cookiefile_detection(
hass: HomeAssistant, hass: HomeAssistant,
mock_youtube_dl: MockYoutubeDL, mock_youtube_dl: MockYoutubeDL,
empty_media_extractor_config: dict[str, Any], empty_media_extractor_config: dict[str, Any],
calls: list[ServiceCall],
snapshot: SnapshotAssertion,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test cookie file detection.""" """Test cookie file detection."""