Cleanup CalDAV test fixtures (#103893)

This commit is contained in:
Allen Porter 2023-11-13 06:57:47 -08:00 committed by GitHub
parent 0e4186ff8a
commit ba3269540f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 36 deletions

View File

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

View File

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

View File

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

View File

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