From ba3269540fe38f7109fe793cc6e49be06e4e6e93 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Mon, 13 Nov 2023 06:57:47 -0800 Subject: [PATCH] Cleanup CalDAV test fixtures (#103893) --- tests/components/caldav/conftest.py | 27 ++++++------------------ tests/components/caldav/test_calendar.py | 11 ++++++---- tests/components/caldav/test_init.py | 17 +++++++++------ tests/components/caldav/test_todo.py | 20 +++++++++++++----- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/tests/components/caldav/conftest.py b/tests/components/caldav/conftest.py index 1c773d49166..504103afe13 100644 --- a/tests/components/caldav/conftest.py +++ b/tests/components/caldav/conftest.py @@ -1,5 +1,4 @@ """Test fixtures for caldav.""" -from collections.abc import Awaitable, Callable from unittest.mock import Mock, patch import pytest @@ -12,7 +11,6 @@ from homeassistant.const import ( CONF_VERIFY_SSL, Platform, ) -from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry @@ -27,6 +25,13 @@ def mock_platforms() -> list[Platform]: return [] +@pytest.fixture(autouse=True) +async def mock_patch_platforms(platforms: list[str]) -> None: + """Fixture to set up the integration.""" + with patch(f"homeassistant.components.{DOMAIN}.PLATFORMS", platforms): + yield + + @pytest.fixture(name="calendars") def mock_calendars() -> list[Mock]: """Fixture to provide calendars returned by CalDAV client.""" @@ -57,21 +62,3 @@ def mock_config_entry() -> MockConfigEntry: CONF_VERIFY_SSL: True, }, ) - - -@pytest.fixture(name="setup_integration") -async def mock_setup_integration( - hass: HomeAssistant, - config_entry: MockConfigEntry, - platforms: list[str], -) -> Callable[[], Awaitable[bool]]: - """Fixture to set up the integration.""" - config_entry.add_to_hass(hass) - - async def run() -> bool: - with patch(f"homeassistant.components.{DOMAIN}.PLATFORMS", platforms): - result = await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() - return result - - return run diff --git a/tests/components/caldav/test_calendar.py b/tests/components/caldav/test_calendar.py index 5a648949f0f..df5428121ee 100644 --- a/tests/components/caldav/test_calendar.py +++ b/tests/components/caldav/test_calendar.py @@ -15,6 +15,7 @@ from homeassistant.core import HomeAssistant from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util +from tests.common import MockConfigEntry from tests.typing import ClientSessionGenerator EVENTS = [ @@ -1085,10 +1086,11 @@ async def test_calendar_components(hass: HomeAssistant) -> None: @freeze_time(_local_datetime(17, 30)) async def test_setup_config_entry( hass: HomeAssistant, - setup_integration: Callable[[], Awaitable[bool]], + config_entry: MockConfigEntry, ) -> None: """Test a calendar entity from a config entry.""" - assert await setup_integration() + config_entry.add_to_hass(hass) + await config_entry.async_setup(hass) state = hass.states.get(TEST_ENTITY) assert state @@ -1118,10 +1120,11 @@ async def test_setup_config_entry( ) async def test_config_entry_supported_components( hass: HomeAssistant, - setup_integration: Callable[[], Awaitable[bool]], + config_entry: MockConfigEntry, ) -> None: """Test that calendars are only created for VEVENT types when using a config entry.""" - assert await setup_integration() + config_entry.add_to_hass(hass) + await config_entry.async_setup(hass) state = hass.states.get("calendar.calendar_1") assert state diff --git a/tests/components/caldav/test_init.py b/tests/components/caldav/test_init.py index a37815a007c..8e832e24d2d 100644 --- a/tests/components/caldav/test_init.py +++ b/tests/components/caldav/test_init.py @@ -1,6 +1,5 @@ """Unit tests for the CalDav integration.""" -from collections.abc import Awaitable, Callable from unittest.mock import patch from caldav.lib.error import AuthorizationError, DAVError @@ -13,17 +12,21 @@ from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry +@pytest.fixture(autouse=True) +async def mock_add_to_hass(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: + """Fixture to add the ConfigEntry.""" + config_entry.add_to_hass(hass) + + async def test_load_unload( hass: HomeAssistant, - setup_integration: Callable[[], Awaitable[bool]], config_entry: MockConfigEntry, ) -> None: """Test loading and unloading of the config entry.""" - assert config_entry.state == ConfigEntryState.NOT_LOADED with patch("homeassistant.components.caldav.config_flow.caldav.DAVClient"): - assert await setup_integration() + await config_entry.async_setup(hass) assert config_entry.state == ConfigEntryState.LOADED @@ -47,8 +50,7 @@ async def test_load_unload( ) async def test_client_failure( hass: HomeAssistant, - setup_integration: Callable[[], Awaitable[bool]], - config_entry: MockConfigEntry | None, + config_entry: MockConfigEntry, side_effect: Exception, expected_state: ConfigEntryState, expected_flows: list[str], @@ -61,7 +63,8 @@ async def test_client_failure( "homeassistant.components.caldav.config_flow.caldav.DAVClient" ) as mock_client: mock_client.return_value.principal.side_effect = side_effect - assert not await setup_integration() + await config_entry.async_setup(hass) + await hass.async_block_till_done() assert config_entry.state == expected_state diff --git a/tests/components/caldav/test_todo.py b/tests/components/caldav/test_todo.py index 16a95d418a8..352b60d5ed3 100644 --- a/tests/components/caldav/test_todo.py +++ b/tests/components/caldav/test_todo.py @@ -1,5 +1,4 @@ """The tests for the webdav todo component.""" -from collections.abc import Awaitable, Callable from unittest.mock import MagicMock, Mock from caldav.objects import Todo @@ -8,6 +7,8 @@ import pytest from homeassistant.const import Platform from homeassistant.core import HomeAssistant +from tests.common import MockConfigEntry + CALENDAR_NAME = "My Tasks" ENTITY_NAME = "My tasks" TEST_ENTITY = "todo.my_tasks" @@ -88,6 +89,15 @@ def mock_calendars(todos: list[str], supported_components: list[str]) -> list[Mo return [calendar] +@pytest.fixture(autouse=True) +async def mock_add_to_hass( + hass: HomeAssistant, + config_entry: MockConfigEntry, +) -> None: + """Fixture to add the ConfigEntry.""" + config_entry.add_to_hass(hass) + + @pytest.mark.parametrize( ("todos", "expected_state"), [ @@ -115,11 +125,11 @@ def mock_calendars(todos: list[str], supported_components: list[str]) -> list[Mo ) async def test_todo_list_state( hass: HomeAssistant, - setup_integration: Callable[[], Awaitable[bool]], + config_entry: MockConfigEntry, expected_state: str, ) -> None: """Test a calendar entity from a config entry.""" - assert await setup_integration() + await config_entry.async_setup(hass) state = hass.states.get(TEST_ENTITY) assert state @@ -136,11 +146,11 @@ async def test_todo_list_state( ) async def test_supported_components( hass: HomeAssistant, - setup_integration: Callable[[], Awaitable[bool]], + config_entry: MockConfigEntry, has_entity: bool, ) -> None: """Test a calendar supported components matches VTODO.""" - assert await setup_integration() + await config_entry.async_setup(hass) state = hass.states.get(TEST_ENTITY) assert (state is not None) == has_entity