mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 17:27:52 +00:00
Add test coverage for google calendar event response format (#68767)
Add test coverage for the calendar event response format as I am about to do some refactoring and simplification. Notably, each calendar implementation uses a slightly different API response, and this is codifying that.
This commit is contained in:
parent
79080f5e2f
commit
609c6ef5d4
@ -6,6 +6,7 @@ import datetime
|
||||
from http import HTTPStatus
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
import urllib
|
||||
|
||||
import httplib2
|
||||
import pytest
|
||||
@ -74,12 +75,21 @@ def upcoming() -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def upcoming_date() -> dict[str, Any]:
|
||||
"""Create a test event with an arbitrary start/end date fetched from the api url."""
|
||||
now = dt_util.now()
|
||||
return {
|
||||
"start": {"date": now.date().isoformat()},
|
||||
"end": {"date": now.date().isoformat()},
|
||||
}
|
||||
|
||||
|
||||
def upcoming_event_url() -> str:
|
||||
"""Return a calendar API to return events created by upcoming()."""
|
||||
now = dt_util.now()
|
||||
start = (now - datetime.timedelta(minutes=60)).isoformat()
|
||||
end = (now + datetime.timedelta(minutes=60)).isoformat()
|
||||
return f"/api/calendars/{TEST_ENTITY}?start={start}&end={end}"
|
||||
return f"/api/calendars/{TEST_ENTITY}?start={urllib.parse.quote(start)}&end={urllib.parse.quote(end)}"
|
||||
|
||||
|
||||
async def test_all_day_event(
|
||||
@ -365,10 +375,12 @@ async def test_http_event_api_failure(
|
||||
assert events == []
|
||||
|
||||
|
||||
@pytest.mark.freeze_time("2022-03-27 12:05:00+00:00")
|
||||
async def test_http_api_event(
|
||||
hass, hass_client, mock_events_list_items, component_setup
|
||||
):
|
||||
"""Test querying the API and fetching events from the server."""
|
||||
hass.config.set_time_zone("Asia/Baghdad")
|
||||
event = {
|
||||
**TEST_EVENT,
|
||||
**upcoming(),
|
||||
@ -381,8 +393,35 @@ async def test_http_api_event(
|
||||
assert response.status == HTTPStatus.OK
|
||||
events = await response.json()
|
||||
assert len(events) == 1
|
||||
assert "summary" in events[0]
|
||||
assert events[0]["summary"] == event["summary"]
|
||||
assert {k: events[0].get(k) for k in ["summary", "start", "end"]} == {
|
||||
"summary": TEST_EVENT["summary"],
|
||||
"start": {"dateTime": "2022-03-27T15:05:00+03:00"},
|
||||
"end": {"dateTime": "2022-03-27T15:10:00+03:00"},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.freeze_time("2022-03-27 12:05:00+00:00")
|
||||
async def test_http_api_all_day_event(
|
||||
hass, hass_client, mock_events_list_items, component_setup
|
||||
):
|
||||
"""Test querying the API and fetching events from the server."""
|
||||
event = {
|
||||
**TEST_EVENT,
|
||||
**upcoming_date(),
|
||||
}
|
||||
mock_events_list_items([event])
|
||||
assert await component_setup()
|
||||
|
||||
client = await hass_client()
|
||||
response = await client.get(upcoming_event_url())
|
||||
assert response.status == HTTPStatus.OK
|
||||
events = await response.json()
|
||||
assert len(events) == 1
|
||||
assert {k: events[0].get(k) for k in ["summary", "start", "end"]} == {
|
||||
"summary": TEST_EVENT["summary"],
|
||||
"start": {"date": "2022-03-27"},
|
||||
"end": {"date": "2022-03-27"},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
Loading…
x
Reference in New Issue
Block a user