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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage for google calendar event response format #68767

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 42 additions & 3 deletions tests/components/google/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from http import HTTPStatus
from typing import Any
from unittest.mock import patch
import urllib

import httplib2
import pytest
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(),
Expand All @@ -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(
Expand Down