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:
Allen Porter 2022-03-28 09:05:02 -07:00 committed by GitHub
parent 79080f5e2f
commit 609c6ef5d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@ import datetime
from http import HTTPStatus from http import HTTPStatus
from typing import Any from typing import Any
from unittest.mock import patch from unittest.mock import patch
import urllib
import httplib2 import httplib2
import pytest 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: def upcoming_event_url() -> str:
"""Return a calendar API to return events created by upcoming().""" """Return a calendar API to return events created by upcoming()."""
now = dt_util.now() now = dt_util.now()
start = (now - datetime.timedelta(minutes=60)).isoformat() start = (now - datetime.timedelta(minutes=60)).isoformat()
end = (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( async def test_all_day_event(
@ -365,10 +375,12 @@ async def test_http_event_api_failure(
assert events == [] assert events == []
@pytest.mark.freeze_time("2022-03-27 12:05:00+00:00")
async def test_http_api_event( async def test_http_api_event(
hass, hass_client, mock_events_list_items, component_setup hass, hass_client, mock_events_list_items, component_setup
): ):
"""Test querying the API and fetching events from the server.""" """Test querying the API and fetching events from the server."""
hass.config.set_time_zone("Asia/Baghdad")
event = { event = {
**TEST_EVENT, **TEST_EVENT,
**upcoming(), **upcoming(),
@ -381,8 +393,35 @@ async def test_http_api_event(
assert response.status == HTTPStatus.OK assert response.status == HTTPStatus.OK
events = await response.json() events = await response.json()
assert len(events) == 1 assert len(events) == 1
assert "summary" in events[0] assert {k: events[0].get(k) for k in ["summary", "start", "end"]} == {
assert events[0]["summary"] == event["summary"] "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( @pytest.mark.parametrize(