mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Improve calendar trigger test quality (#79451)
Improve calendar test quality
This commit is contained in:
parent
4abe5aec6c
commit
d1d218444b
@ -62,14 +62,15 @@ class FakeSchedule:
|
|||||||
self,
|
self,
|
||||||
start: datetime.timedelta,
|
start: datetime.timedelta,
|
||||||
end: datetime.timedelta,
|
end: datetime.timedelta,
|
||||||
description: str = None,
|
summary: str | None = None,
|
||||||
location: str = None,
|
description: str | None = None,
|
||||||
|
location: str | None = None,
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Create a new fake event, used by tests."""
|
"""Create a new fake event, used by tests."""
|
||||||
event = calendar.CalendarEvent(
|
event = calendar.CalendarEvent(
|
||||||
start=start,
|
start=start,
|
||||||
end=end,
|
end=end,
|
||||||
summary=f"Event {secrets.token_hex(16)}", # Arbitrary unique data
|
summary=summary if summary else f"Event {secrets.token_hex(16)}",
|
||||||
description=description,
|
description=description,
|
||||||
location=location,
|
location=location,
|
||||||
)
|
)
|
||||||
@ -85,8 +86,13 @@ class FakeSchedule:
|
|||||||
"""Get all events in a specific time frame, used by the demo calendar."""
|
"""Get all events in a specific time frame, used by the demo calendar."""
|
||||||
assert start_date < end_date
|
assert start_date < end_date
|
||||||
values = []
|
values = []
|
||||||
|
local_start_date = dt_util.as_local(start_date)
|
||||||
|
local_end_date = dt_util.as_local(end_date)
|
||||||
for event in self.events:
|
for event in self.events:
|
||||||
if start_date < event.start < end_date or start_date < event.end < end_date:
|
if (
|
||||||
|
event.start_datetime_local < local_end_date
|
||||||
|
and local_start_date < event.end_datetime_local
|
||||||
|
):
|
||||||
values.append(event)
|
values.append(event)
|
||||||
return values
|
return values
|
||||||
|
|
||||||
@ -104,6 +110,14 @@ class FakeSchedule:
|
|||||||
await self.fire_time(dt_util.utcnow())
|
await self.fire_time(dt_util.utcnow())
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def set_time_zone(hass):
|
||||||
|
"""Set the time zone for the tests."""
|
||||||
|
# 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")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def fake_schedule(hass, freezer):
|
def fake_schedule(hass, freezer):
|
||||||
"""Fixture that tests can use to make fake events."""
|
"""Fixture that tests can use to make fake events."""
|
||||||
@ -534,25 +548,72 @@ async def test_update_missed(hass, calls, fake_schedule):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
async def test_event_payload(hass, calls, fake_schedule):
|
@pytest.mark.parametrize(
|
||||||
"""Test the a calendar trigger based on start time."""
|
"create_data,fire_time,payload_data",
|
||||||
event_data = fake_schedule.create_event(
|
[
|
||||||
start=datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
(
|
||||||
end=datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00"),
|
{
|
||||||
description="Description",
|
"start": datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
||||||
location="Location",
|
"end": datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00"),
|
||||||
)
|
"summary": "Summary",
|
||||||
|
},
|
||||||
|
datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
|
||||||
|
{
|
||||||
|
"summary": "Summary",
|
||||||
|
"start": "2022-04-19T11:00:00+00:00",
|
||||||
|
"end": "2022-04-19T11:30:00+00:00",
|
||||||
|
"all_day": False,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
{
|
||||||
|
"start": datetime.datetime.fromisoformat("2022-04-19 11:00:00+00:00"),
|
||||||
|
"end": datetime.datetime.fromisoformat("2022-04-19 11:30:00+00:00"),
|
||||||
|
"summary": "Summary",
|
||||||
|
"description": "Description",
|
||||||
|
"location": "Location",
|
||||||
|
},
|
||||||
|
datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00"),
|
||||||
|
{
|
||||||
|
"summary": "Summary",
|
||||||
|
"start": "2022-04-19T11:00:00+00:00",
|
||||||
|
"end": "2022-04-19T11:30:00+00:00",
|
||||||
|
"all_day": False,
|
||||||
|
"description": "Description",
|
||||||
|
"location": "Location",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
{
|
||||||
|
"summary": "Summary",
|
||||||
|
"start": datetime.date.fromisoformat("2022-04-20"),
|
||||||
|
"end": datetime.date.fromisoformat("2022-04-21"),
|
||||||
|
},
|
||||||
|
datetime.datetime.fromisoformat("2022-04-20 00:00:01-06:00"),
|
||||||
|
{
|
||||||
|
"summary": "Summary",
|
||||||
|
"start": "2022-04-20",
|
||||||
|
"end": "2022-04-21",
|
||||||
|
"all_day": True,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
ids=["basic", "more-fields", "all-day"],
|
||||||
|
)
|
||||||
|
async def test_event_payload(
|
||||||
|
hass, calls, fake_schedule, set_time_zone, create_data, fire_time, payload_data
|
||||||
|
):
|
||||||
|
"""Test the fields in the calendar event payload are set."""
|
||||||
|
fake_schedule.create_event(**create_data)
|
||||||
await create_automation(hass, EVENT_START)
|
await create_automation(hass, EVENT_START)
|
||||||
assert len(calls()) == 0
|
assert len(calls()) == 0
|
||||||
|
|
||||||
await fake_schedule.fire_until(
|
await fake_schedule.fire_until(fire_time)
|
||||||
datetime.datetime.fromisoformat("2022-04-19 11:15:00+00:00")
|
|
||||||
)
|
|
||||||
assert calls() == [
|
assert calls() == [
|
||||||
{
|
{
|
||||||
"platform": "calendar",
|
"platform": "calendar",
|
||||||
"event": EVENT_START,
|
"event": EVENT_START,
|
||||||
"calendar_event": event_data,
|
"calendar_event": payload_data,
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user