From c4903dd982d7ba8e6851a154a41b16bd1375d664 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 1 Jul 2024 17:32:17 +0200 Subject: [PATCH] Use service_calls fixture in media_extractor tests (#120935) --- tests/components/media_extractor/conftest.py | 10 +------ tests/components/media_extractor/test_init.py | 29 ++++++++++--------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/tests/components/media_extractor/conftest.py b/tests/components/media_extractor/conftest.py index 45b3bb698e0..58d51f1cb2e 100644 --- a/tests/components/media_extractor/conftest.py +++ b/tests/components/media_extractor/conftest.py @@ -7,14 +7,12 @@ from unittest.mock import AsyncMock, patch import pytest 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 . import MockYoutubeDL from .const import AUDIO_QUERY -from tests.common import async_mock_service - @pytest.fixture(autouse=True) async def setup_homeassistant(hass: HomeAssistant): @@ -31,12 +29,6 @@ async def setup_media_player(hass: HomeAssistant) -> None: 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") async def setup_mock_yt_dlp(hass: HomeAssistant) -> MockYoutubeDL: """Mock YoutubeDL.""" diff --git a/tests/components/media_extractor/test_init.py b/tests/components/media_extractor/test_init.py index 8c8a1407ccc..9708e1c2ad6 100644 --- a/tests/components/media_extractor/test_init.py +++ b/tests/components/media_extractor/test_init.py @@ -100,7 +100,7 @@ async def test_extracting_playlist_no_entries( async def test_play_media_service( hass: HomeAssistant, mock_youtube_dl: MockYoutubeDL, - calls: list[ServiceCall], + service_calls: list[ServiceCall], snapshot: SnapshotAssertion, request: pytest.FixtureRequest, config_fixture: str, @@ -123,13 +123,14 @@ async def test_play_media_service( ) 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( hass: HomeAssistant, empty_media_extractor_config: dict[str, Any], - calls: list[ServiceCall], + service_calls: list[ServiceCall], caplog: pytest.LogCaptureFixture, ) -> None: """Test handling DownloadError.""" @@ -152,7 +153,7 @@ async def test_download_error( ) 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 @@ -160,7 +161,7 @@ async def test_no_target_entity( hass: HomeAssistant, mock_youtube_dl: MockYoutubeDL, empty_media_extractor_config: dict[str, Any], - calls: list[ServiceCall], + service_calls: list[ServiceCall], snapshot: SnapshotAssertion, ) -> None: """Test having no target entity.""" @@ -179,14 +180,15 @@ async def test_no_target_entity( ) 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( hass: HomeAssistant, mock_youtube_dl: MockYoutubeDL, empty_media_extractor_config: dict[str, Any], - calls: list[ServiceCall], + service_calls: list[ServiceCall], snapshot: SnapshotAssertion, ) -> None: """Test extracting a playlist.""" @@ -205,14 +207,15 @@ async def test_playlist( ) 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( hass: HomeAssistant, mock_youtube_dl: MockYoutubeDL, empty_media_extractor_config: dict[str, Any], - calls: list[ServiceCall], + service_calls: list[ServiceCall], caplog: pytest.LogCaptureFixture, ) -> None: """Test extracting a playlist without entries.""" @@ -231,7 +234,7 @@ async def test_playlist_no_entries( ) 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_EMPTY_PLAYLIST}" in caplog.text ) @@ -240,7 +243,7 @@ async def test_playlist_no_entries( async def test_query_error( hass: HomeAssistant, empty_media_extractor_config: dict[str, Any], - calls: list[ServiceCall], + service_calls: list[ServiceCall], ) -> None: """Test handling error with query.""" @@ -270,15 +273,13 @@ async def test_query_error( ) await hass.async_block_till_done() - assert len(calls) == 0 + assert len(service_calls) == 1 async def test_cookiefile_detection( hass: HomeAssistant, mock_youtube_dl: MockYoutubeDL, empty_media_extractor_config: dict[str, Any], - calls: list[ServiceCall], - snapshot: SnapshotAssertion, caplog: pytest.LogCaptureFixture, ) -> None: """Test cookie file detection."""