Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update calendar tests to use mock entities instead of demo platform #105317

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 178 additions & 5 deletions tests/components/calendar/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
"""Test fixtures for calendar sensor platforms."""
from collections.abc import Generator
import datetime
import secrets
from typing import Any
from unittest.mock import AsyncMock

import pytest

from homeassistant.components.calendar import DOMAIN, CalendarEntity, CalendarEvent
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt as dt_util

from tests.common import (
MockConfigEntry,
MockModule,
MockPlatform,
mock_config_flow,
mock_integration,
mock_platform,
)

@pytest.fixture(autouse=True)
async def setup_homeassistant(hass: HomeAssistant):
"""Set up the homeassistant integration."""
await async_setup_component(hass, "homeassistant", {})
TEST_DOMAIN = "test"


@pytest.fixture
Expand All @@ -17,3 +32,161 @@ def set_time_zone(hass: HomeAssistant) -> None:
# Set our timezone to CST/Regina so we can check calculations
# This keeps UTC-6 all year round
hass.config.set_time_zone("America/Regina")


class MockFlow(ConfigFlow):
"""Test flow."""


class MockCalendarEntity(CalendarEntity):
"""Test Calendar entity."""

_attr_has_entity_name = True

def __init__(self, name: str, events: list[CalendarEvent] | None = None) -> None:
"""Initialize entity."""
self._attr_name = name.capitalize()
self._events = events or []

@property
def event(self) -> CalendarEvent | None:
"""Return the next upcoming event."""
return self._events[0] if self._events else None

def create_event(
self,
start: datetime.datetime,
end: datetime.datetime,
summary: str | None = None,
description: str | None = None,
location: str | None = None,
) -> dict[str, Any]:
"""Create a new fake event, used by tests."""
event = CalendarEvent(
start=start,
end=end,
summary=summary if summary else f"Event {secrets.token_hex(16)}",
description=description,
location=location,
)
self._events.append(event)
return event.as_dict()

async def async_get_events(
self,
hass: HomeAssistant,
start_date: datetime.datetime,
end_date: datetime.datetime,
) -> list[CalendarEvent]:
"""Return calendar events within a datetime range."""
assert start_date < end_date
events = []
for event in self._events:
if event.start_datetime_local >= end_date:
continue
if event.end_datetime_local < start_date:
continue
events.append(event)
return events


@pytest.fixture
def config_flow_fixture(hass: HomeAssistant) -> Generator[None, None, None]:
"""Mock config flow."""
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")

with mock_config_flow(TEST_DOMAIN, MockFlow):
yield


@pytest.fixture
def mock_setup_integration(hass: HomeAssistant, config_flow_fixture: None) -> None:
"""Fixture to set up a mock integration."""

async def async_setup_entry_init(
hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
"""Set up test config entry."""
await hass.config_entries.async_forward_entry_setup(config_entry, DOMAIN)
return True

async def async_unload_entry_init(
hass: HomeAssistant,
config_entry: ConfigEntry,
) -> bool:
await hass.config_entries.async_unload_platforms(
config_entry, [Platform.CALENDAR]
)
return True

mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
mock_integration(
hass,
MockModule(
TEST_DOMAIN,
async_setup_entry=async_setup_entry_init,
async_unload_entry=async_unload_entry_init,
),
)


async def create_mock_platform(
hass: HomeAssistant,
entities: list[CalendarEntity],
) -> MockConfigEntry:
"""Create a calendar platform with the specified entities."""

async def async_setup_entry_platform(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up test event platform via config entry."""
async_add_entities(entities)

mock_platform(
hass,
f"{TEST_DOMAIN}.{DOMAIN}",
MockPlatform(async_setup_entry=async_setup_entry_platform),
)

config_entry = MockConfigEntry(domain=TEST_DOMAIN)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

return config_entry


@pytest.fixture(name="test_entities")
def mock_test_entities() -> list[MockCalendarEntity]:
"""Fixture to create fake entities used in the test."""
half_hour_from_now = dt_util.now() + datetime.timedelta(minutes=30)
entity1 = MockCalendarEntity(
"Calendar 1",
[
CalendarEvent(
start=half_hour_from_now,
end=half_hour_from_now + datetime.timedelta(minutes=60),
summary="Future Event",
description="Future Description",
location="Future Location",
)
],
)
entity1.async_get_events = AsyncMock(wraps=entity1.async_get_events)

middle_of_event = dt_util.now() - datetime.timedelta(minutes=30)
entity2 = MockCalendarEntity(
"Calendar 2",
[
CalendarEvent(
start=middle_of_event,
end=middle_of_event + datetime.timedelta(minutes=60),
summary="Current Event",
)
],
)
entity2.async_get_events = AsyncMock(wraps=entity2.async_get_events)

return [entity1, entity2]
28 changes: 14 additions & 14 deletions tests/components/calendar/snapshots/test_init.ambr
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
# serializer version: 1
# name: test_list_events_service_duration[calendar.calendar_1-00:15:00-get_events]
# name: test_list_events_service_duration[frozen_time-calendar.calendar_1-00:15:00-get_events]
dict({
'calendar.calendar_1': dict({
'events': list([
]),
}),
})
# ---
# name: test_list_events_service_duration[calendar.calendar_1-00:15:00-list_events]
# name: test_list_events_service_duration[frozen_time-calendar.calendar_1-00:15:00-list_events]
dict({
'events': list([
]),
})
# ---
# name: test_list_events_service_duration[calendar.calendar_1-01:00:00-get_events]
# name: test_list_events_service_duration[frozen_time-calendar.calendar_1-01:00:00-get_events]
dict({
'calendar.calendar_1': dict({
'events': list([
dict({
'description': 'Future Description',
'end': '2023-10-19T08:20:05-07:00',
'end': '2023-10-19T09:20:05-06:00',
'location': 'Future Location',
'start': '2023-10-19T07:20:05-07:00',
'start': '2023-10-19T08:20:05-06:00',
'summary': 'Future Event',
}),
]),
}),
})
# ---
# name: test_list_events_service_duration[calendar.calendar_1-01:00:00-list_events]
# name: test_list_events_service_duration[frozen_time-calendar.calendar_1-01:00:00-list_events]
dict({
'events': list([
dict({
'description': 'Future Description',
'end': '2023-10-19T08:20:05-07:00',
'end': '2023-10-19T09:20:05-06:00',
'location': 'Future Location',
'start': '2023-10-19T07:20:05-07:00',
'start': '2023-10-19T08:20:05-06:00',
'summary': 'Future Event',
}),
]),
})
# ---
# name: test_list_events_service_duration[calendar.calendar_2-00:15:00-get_events]
# name: test_list_events_service_duration[frozen_time-calendar.calendar_2-00:15:00-get_events]
dict({
'calendar.calendar_2': dict({
'events': list([
dict({
'end': '2023-10-19T07:20:05-07:00',
'start': '2023-10-19T06:20:05-07:00',
'end': '2023-10-19T08:20:05-06:00',
'start': '2023-10-19T07:20:05-06:00',
'summary': 'Current Event',
}),
]),
}),
})
# ---
# name: test_list_events_service_duration[calendar.calendar_2-00:15:00-list_events]
# name: test_list_events_service_duration[frozen_time-calendar.calendar_2-00:15:00-list_events]
dict({
'events': list([
dict({
'end': '2023-10-19T07:20:05-07:00',
'start': '2023-10-19T06:20:05-07:00',
'end': '2023-10-19T08:20:05-06:00',
'start': '2023-10-19T07:20:05-06:00',
'summary': 'Current Event',
}),
]),
Expand Down
Loading
Loading