2019-05-05 16:38:55 +00:00
|
|
|
"""The tests for the google calendar platform."""
|
Increase test coverage for google calendar (#62648)
* Increase test coverage for google calendar
Update tests to exercise the API responses, getting test coverage
to 97% for calendar.py
----------- coverage: platform linux, python 3.9.6-final-0 -----------
Name Stmts Miss Cover Missing
---------------------------------------------------------------------------
homeassistant/components/google/__init__.py 193 84 56% 92, 163-228, 238, 244-247, 254-262, 274, 298-299, 305-347, 387-392, 416-430, 435-437
homeassistant/components/google/calendar.py 122 4 97% 41, 45, 51, 135
---------------------------------------------------------------------------
TOTAL 315 88 72%
* Revert conftest changes
* Update typing errors found on CI
* Update python3.8 typing imports
* Remove commented out code
2021-12-23 06:31:56 +00:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-05-05 16:38:55 +00:00
|
|
|
import copy
|
Increase test coverage for google calendar (#62648)
* Increase test coverage for google calendar
Update tests to exercise the API responses, getting test coverage
to 97% for calendar.py
----------- coverage: platform linux, python 3.9.6-final-0 -----------
Name Stmts Miss Cover Missing
---------------------------------------------------------------------------
homeassistant/components/google/__init__.py 193 84 56% 92, 163-228, 238, 244-247, 254-262, 274, 298-299, 305-347, 387-392, 416-430, 435-437
homeassistant/components/google/calendar.py 122 4 97% 41, 45, 51, 135
---------------------------------------------------------------------------
TOTAL 315 88 72%
* Revert conftest changes
* Update typing errors found on CI
* Update python3.8 typing imports
* Remove commented out code
2021-12-23 06:31:56 +00:00
|
|
|
from http import HTTPStatus
|
|
|
|
from typing import Any, Callable
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import Mock, patch
|
2018-02-09 07:11:47 +00:00
|
|
|
|
2019-05-05 16:38:55 +00:00
|
|
|
import httplib2
|
2018-02-09 07:11:47 +00:00
|
|
|
import pytest
|
|
|
|
|
2019-05-05 16:38:55 +00:00
|
|
|
from homeassistant.components.google import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CAL_ID,
|
|
|
|
CONF_CLIENT_ID,
|
|
|
|
CONF_CLIENT_SECRET,
|
|
|
|
CONF_DEVICE_ID,
|
|
|
|
CONF_ENTITIES,
|
Increase test coverage for google calendar (#62648)
* Increase test coverage for google calendar
Update tests to exercise the API responses, getting test coverage
to 97% for calendar.py
----------- coverage: platform linux, python 3.9.6-final-0 -----------
Name Stmts Miss Cover Missing
---------------------------------------------------------------------------
homeassistant/components/google/__init__.py 193 84 56% 92, 163-228, 238, 244-247, 254-262, 274, 298-299, 305-347, 387-392, 416-430, 435-437
homeassistant/components/google/calendar.py 122 4 97% 41, 45, 51, 135
---------------------------------------------------------------------------
TOTAL 315 88 72%
* Revert conftest changes
* Update typing errors found on CI
* Update python3.8 typing imports
* Remove commented out code
2021-12-23 06:31:56 +00:00
|
|
|
CONF_IGNORE_AVAILABILITY,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_TRACK,
|
|
|
|
DEVICE_SCHEMA,
|
|
|
|
SERVICE_SCAN_CALENDARS,
|
Increase test coverage for google calendar (#62648)
* Increase test coverage for google calendar
Update tests to exercise the API responses, getting test coverage
to 97% for calendar.py
----------- coverage: platform linux, python 3.9.6-final-0 -----------
Name Stmts Miss Cover Missing
---------------------------------------------------------------------------
homeassistant/components/google/__init__.py 193 84 56% 92, 163-228, 238, 244-247, 254-262, 274, 298-299, 305-347, 387-392, 416-430, 435-437
homeassistant/components/google/calendar.py 122 4 97% 41, 45, 51, 135
---------------------------------------------------------------------------
TOTAL 315 88 72%
* Revert conftest changes
* Update typing errors found on CI
* Update python3.8 typing imports
* Remove commented out code
2021-12-23 06:31:56 +00:00
|
|
|
GoogleCalendarService,
|
2019-07-31 19:25:30 +00:00
|
|
|
do_setup,
|
|
|
|
)
|
2019-05-05 16:38:55 +00:00
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON
|
2018-02-09 07:11:47 +00:00
|
|
|
from homeassistant.helpers.template import DATE_STR_FORMAT
|
2019-05-05 16:38:55 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from homeassistant.util import slugify
|
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
Increase test coverage for google calendar (#62648)
* Increase test coverage for google calendar
Update tests to exercise the API responses, getting test coverage
to 97% for calendar.py
----------- coverage: platform linux, python 3.9.6-final-0 -----------
Name Stmts Miss Cover Missing
---------------------------------------------------------------------------
homeassistant/components/google/__init__.py 193 84 56% 92, 163-228, 238, 244-247, 254-262, 274, 298-299, 305-347, 387-392, 416-430, 435-437
homeassistant/components/google/calendar.py 122 4 97% 41, 45, 51, 135
---------------------------------------------------------------------------
TOTAL 315 88 72%
* Revert conftest changes
* Update typing errors found on CI
* Update python3.8 typing imports
* Remove commented out code
2021-12-23 06:31:56 +00:00
|
|
|
from .conftest import TEST_CALENDAR
|
|
|
|
|
2019-05-05 16:38:55 +00:00
|
|
|
from tests.common import async_mock_service
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
GOOGLE_CONFIG = {CONF_CLIENT_ID: "client_id", CONF_CLIENT_SECRET: "client_secret"}
|
|
|
|
TEST_ENTITY = "calendar.we_are_we_are_a_test_calendar"
|
|
|
|
TEST_ENTITY_NAME = "We are, we are, a... Test Calendar"
|
2019-05-05 16:38:55 +00:00
|
|
|
|
|
|
|
TEST_EVENT = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"summary": "Test All Day Event",
|
|
|
|
"start": {},
|
|
|
|
"end": {},
|
|
|
|
"location": "Test Cases",
|
|
|
|
"description": "test event",
|
|
|
|
"kind": "calendar#event",
|
|
|
|
"created": "2016-06-23T16:37:57.000Z",
|
|
|
|
"transparency": "transparent",
|
|
|
|
"updated": "2016-06-24T01:57:21.045Z",
|
|
|
|
"reminders": {"useDefault": True},
|
|
|
|
"organizer": {
|
|
|
|
"email": "uvrttabwegnui4gtia3vyqb@import.calendar.google.com",
|
|
|
|
"displayName": "Organizer Name",
|
|
|
|
"self": True,
|
2019-05-05 16:38:55 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"sequence": 0,
|
|
|
|
"creator": {
|
|
|
|
"email": "uvrttabwegnui4gtia3vyqb@import.calendar.google.com",
|
|
|
|
"displayName": "Organizer Name",
|
|
|
|
"self": True,
|
2019-05-05 16:38:55 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"id": "_c8rinwq863h45qnucyoi43ny8",
|
|
|
|
"etag": '"2933466882090000"',
|
|
|
|
"htmlLink": "https://www.google.com/calendar/event?eid=*******",
|
|
|
|
"iCalUID": "cydrevtfuybguinhomj@google.com",
|
|
|
|
"status": "confirmed",
|
2019-05-05 16:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_calendar_info(calendar):
|
|
|
|
"""Convert data from Google into DEVICE_SCHEMA."""
|
2019-07-31 19:25:30 +00:00
|
|
|
calendar_info = DEVICE_SCHEMA(
|
|
|
|
{
|
|
|
|
CONF_CAL_ID: calendar["id"],
|
|
|
|
CONF_ENTITIES: [
|
|
|
|
{
|
|
|
|
CONF_TRACK: calendar["track"],
|
|
|
|
CONF_NAME: calendar["summary"],
|
|
|
|
CONF_DEVICE_ID: slugify(calendar["summary"]),
|
Increase test coverage for google calendar (#62648)
* Increase test coverage for google calendar
Update tests to exercise the API responses, getting test coverage
to 97% for calendar.py
----------- coverage: platform linux, python 3.9.6-final-0 -----------
Name Stmts Miss Cover Missing
---------------------------------------------------------------------------
homeassistant/components/google/__init__.py 193 84 56% 92, 163-228, 238, 244-247, 254-262, 274, 298-299, 305-347, 387-392, 416-430, 435-437
homeassistant/components/google/calendar.py 122 4 97% 41, 45, 51, 135
---------------------------------------------------------------------------
TOTAL 315 88 72%
* Revert conftest changes
* Update typing errors found on CI
* Update python3.8 typing imports
* Remove commented out code
2021-12-23 06:31:56 +00:00
|
|
|
CONF_IGNORE_AVAILABILITY: calendar.get("ignore_availability", True),
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
2019-05-05 16:38:55 +00:00
|
|
|
return calendar_info
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_google_setup(hass, test_calendar):
|
|
|
|
"""Mock the google set up functions."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.loop.run_until_complete(async_setup_component(hass, "group", {"group": {}}))
|
2019-05-05 16:38:55 +00:00
|
|
|
calendar = get_calendar_info(test_calendar)
|
|
|
|
calendars = {calendar[CONF_CAL_ID]: calendar}
|
|
|
|
patch_google_auth = patch(
|
2019-07-31 19:25:30 +00:00
|
|
|
"homeassistant.components.google.do_authentication", side_effect=do_setup
|
|
|
|
)
|
2019-05-05 16:38:55 +00:00
|
|
|
patch_google_load = patch(
|
2019-07-31 19:25:30 +00:00
|
|
|
"homeassistant.components.google.load_config", return_value=calendars
|
|
|
|
)
|
|
|
|
patch_google_services = patch("homeassistant.components.google.setup_services")
|
|
|
|
async_mock_service(hass, "google", SERVICE_SCAN_CALENDARS)
|
2019-05-05 16:38:55 +00:00
|
|
|
|
|
|
|
with patch_google_auth, patch_google_load, patch_google_services:
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def set_time_zone():
|
|
|
|
"""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
|
2019-07-31 19:25:30 +00:00
|
|
|
dt_util.set_default_time_zone(dt_util.get_time_zone("America/Regina"))
|
2019-05-05 16:38:55 +00:00
|
|
|
yield
|
2019-07-31 19:25:30 +00:00
|
|
|
dt_util.set_default_time_zone(dt_util.get_time_zone("UTC"))
|
2019-05-05 16:38:55 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@pytest.fixture(name="google_service")
|
2019-05-05 16:38:55 +00:00
|
|
|
def mock_google_service():
|
|
|
|
"""Mock google service."""
|
|
|
|
patch_google_service = patch(
|
2019-07-31 19:25:30 +00:00
|
|
|
"homeassistant.components.google.calendar.GoogleCalendarService"
|
|
|
|
)
|
2019-05-05 16:38:55 +00:00
|
|
|
with patch_google_service as mock_service:
|
|
|
|
yield mock_service
|
|
|
|
|
|
|
|
|
|
|
|
async def test_all_day_event(hass, mock_next_event):
|
|
|
|
"""Test that we can create an event trigger on device."""
|
2019-07-31 19:25:30 +00:00
|
|
|
week_from_today = dt_util.dt.date.today() + dt_util.dt.timedelta(days=7)
|
2019-05-05 16:38:55 +00:00
|
|
|
end_event = week_from_today + dt_util.dt.timedelta(days=1)
|
|
|
|
event = copy.deepcopy(TEST_EVENT)
|
|
|
|
start = week_from_today.isoformat()
|
|
|
|
end = end_event.isoformat()
|
2019-07-31 19:25:30 +00:00
|
|
|
event["start"]["date"] = start
|
|
|
|
event["end"]["date"] = end
|
2019-05-05 16:38:55 +00:00
|
|
|
mock_next_event.return_value.event = event
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
2019-05-05 16:38:55 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(TEST_ENTITY)
|
|
|
|
assert state.name == TEST_ENTITY_NAME
|
|
|
|
assert state.state == STATE_OFF
|
|
|
|
assert dict(state.attributes) == {
|
2019-07-31 19:25:30 +00:00
|
|
|
"friendly_name": TEST_ENTITY_NAME,
|
|
|
|
"message": event["summary"],
|
|
|
|
"all_day": True,
|
|
|
|
"offset_reached": False,
|
|
|
|
"start_time": week_from_today.strftime(DATE_STR_FORMAT),
|
|
|
|
"end_time": end_event.strftime(DATE_STR_FORMAT),
|
|
|
|
"location": event["location"],
|
|
|
|
"description": event["description"],
|
2019-05-05 16:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_future_event(hass, mock_next_event):
|
|
|
|
"""Test that we can create an event trigger on device."""
|
|
|
|
one_hour_from_now = dt_util.now() + dt_util.dt.timedelta(minutes=30)
|
|
|
|
end_event = one_hour_from_now + dt_util.dt.timedelta(minutes=60)
|
|
|
|
start = one_hour_from_now.isoformat()
|
|
|
|
end = end_event.isoformat()
|
|
|
|
event = copy.deepcopy(TEST_EVENT)
|
2019-07-31 19:25:30 +00:00
|
|
|
event["start"]["dateTime"] = start
|
|
|
|
event["end"]["dateTime"] = end
|
2019-05-05 16:38:55 +00:00
|
|
|
mock_next_event.return_value.event = event
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
2019-05-05 16:38:55 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(TEST_ENTITY)
|
|
|
|
assert state.name == TEST_ENTITY_NAME
|
|
|
|
assert state.state == STATE_OFF
|
|
|
|
assert dict(state.attributes) == {
|
2019-07-31 19:25:30 +00:00
|
|
|
"friendly_name": TEST_ENTITY_NAME,
|
|
|
|
"message": event["summary"],
|
|
|
|
"all_day": False,
|
|
|
|
"offset_reached": False,
|
|
|
|
"start_time": one_hour_from_now.strftime(DATE_STR_FORMAT),
|
|
|
|
"end_time": end_event.strftime(DATE_STR_FORMAT),
|
|
|
|
"location": event["location"],
|
|
|
|
"description": event["description"],
|
2019-05-05 16:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_in_progress_event(hass, mock_next_event):
|
|
|
|
"""Test that we can create an event trigger on device."""
|
|
|
|
middle_of_event = dt_util.now() - dt_util.dt.timedelta(minutes=30)
|
|
|
|
end_event = middle_of_event + dt_util.dt.timedelta(minutes=60)
|
|
|
|
start = middle_of_event.isoformat()
|
|
|
|
end = end_event.isoformat()
|
|
|
|
event = copy.deepcopy(TEST_EVENT)
|
2019-07-31 19:25:30 +00:00
|
|
|
event["start"]["dateTime"] = start
|
|
|
|
event["end"]["dateTime"] = end
|
2019-05-05 16:38:55 +00:00
|
|
|
mock_next_event.return_value.event = event
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
2019-05-05 16:38:55 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(TEST_ENTITY)
|
|
|
|
assert state.name == TEST_ENTITY_NAME
|
|
|
|
assert state.state == STATE_ON
|
|
|
|
assert dict(state.attributes) == {
|
2019-07-31 19:25:30 +00:00
|
|
|
"friendly_name": TEST_ENTITY_NAME,
|
|
|
|
"message": event["summary"],
|
|
|
|
"all_day": False,
|
|
|
|
"offset_reached": False,
|
|
|
|
"start_time": middle_of_event.strftime(DATE_STR_FORMAT),
|
|
|
|
"end_time": end_event.strftime(DATE_STR_FORMAT),
|
|
|
|
"location": event["location"],
|
|
|
|
"description": event["description"],
|
2019-05-05 16:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_offset_in_progress_event(hass, mock_next_event):
|
|
|
|
"""Test that we can create an event trigger on device."""
|
|
|
|
middle_of_event = dt_util.now() + dt_util.dt.timedelta(minutes=14)
|
|
|
|
end_event = middle_of_event + dt_util.dt.timedelta(minutes=60)
|
|
|
|
start = middle_of_event.isoformat()
|
|
|
|
end = end_event.isoformat()
|
2019-07-31 19:25:30 +00:00
|
|
|
event_summary = "Test Event in Progress"
|
2019-05-05 16:38:55 +00:00
|
|
|
event = copy.deepcopy(TEST_EVENT)
|
2019-07-31 19:25:30 +00:00
|
|
|
event["start"]["dateTime"] = start
|
|
|
|
event["end"]["dateTime"] = end
|
2020-02-25 01:54:20 +00:00
|
|
|
event["summary"] = f"{event_summary} !!-15"
|
2019-05-05 16:38:55 +00:00
|
|
|
mock_next_event.return_value.event = event
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
2019-05-05 16:38:55 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(TEST_ENTITY)
|
|
|
|
assert state.name == TEST_ENTITY_NAME
|
|
|
|
assert state.state == STATE_OFF
|
|
|
|
assert dict(state.attributes) == {
|
2019-07-31 19:25:30 +00:00
|
|
|
"friendly_name": TEST_ENTITY_NAME,
|
|
|
|
"message": event_summary,
|
|
|
|
"all_day": False,
|
|
|
|
"offset_reached": True,
|
|
|
|
"start_time": middle_of_event.strftime(DATE_STR_FORMAT),
|
|
|
|
"end_time": end_event.strftime(DATE_STR_FORMAT),
|
|
|
|
"location": event["location"],
|
|
|
|
"description": event["description"],
|
2019-05-05 16:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip
|
|
|
|
async def test_all_day_offset_in_progress_event(hass, mock_next_event):
|
|
|
|
"""Test that we can create an event trigger on device."""
|
|
|
|
tomorrow = dt_util.dt.date.today() + dt_util.dt.timedelta(days=1)
|
|
|
|
end_event = tomorrow + dt_util.dt.timedelta(days=1)
|
|
|
|
start = tomorrow.isoformat()
|
|
|
|
end = end_event.isoformat()
|
2019-07-31 19:25:30 +00:00
|
|
|
event_summary = "Test All Day Event Offset In Progress"
|
2019-05-05 16:38:55 +00:00
|
|
|
event = copy.deepcopy(TEST_EVENT)
|
2019-07-31 19:25:30 +00:00
|
|
|
event["start"]["date"] = start
|
|
|
|
event["end"]["date"] = end
|
2020-02-25 01:54:20 +00:00
|
|
|
event["summary"] = f"{event_summary} !!-25:0"
|
2019-05-05 16:38:55 +00:00
|
|
|
mock_next_event.return_value.event = event
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
2019-05-05 16:38:55 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(TEST_ENTITY)
|
|
|
|
assert state.name == TEST_ENTITY_NAME
|
|
|
|
assert state.state == STATE_OFF
|
|
|
|
assert dict(state.attributes) == {
|
2019-07-31 19:25:30 +00:00
|
|
|
"friendly_name": TEST_ENTITY_NAME,
|
|
|
|
"message": event_summary,
|
|
|
|
"all_day": True,
|
|
|
|
"offset_reached": True,
|
|
|
|
"start_time": tomorrow.strftime(DATE_STR_FORMAT),
|
|
|
|
"end_time": end_event.strftime(DATE_STR_FORMAT),
|
|
|
|
"location": event["location"],
|
|
|
|
"description": event["description"],
|
2019-05-05 16:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_all_day_offset_event(hass, mock_next_event):
|
|
|
|
"""Test that we can create an event trigger on device."""
|
|
|
|
tomorrow = dt_util.dt.date.today() + dt_util.dt.timedelta(days=2)
|
|
|
|
end_event = tomorrow + dt_util.dt.timedelta(days=1)
|
|
|
|
start = tomorrow.isoformat()
|
|
|
|
end = end_event.isoformat()
|
2019-07-31 19:25:30 +00:00
|
|
|
offset_hours = 1 + dt_util.now().hour
|
|
|
|
event_summary = "Test All Day Event Offset"
|
2019-05-05 16:38:55 +00:00
|
|
|
event = copy.deepcopy(TEST_EVENT)
|
2019-07-31 19:25:30 +00:00
|
|
|
event["start"]["date"] = start
|
|
|
|
event["end"]["date"] = end
|
2020-02-25 01:54:20 +00:00
|
|
|
event["summary"] = f"{event_summary} !!-{offset_hours}:0"
|
2019-05-05 16:38:55 +00:00
|
|
|
mock_next_event.return_value.event = event
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
2019-05-05 16:38:55 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(TEST_ENTITY)
|
|
|
|
assert state.name == TEST_ENTITY_NAME
|
|
|
|
assert state.state == STATE_OFF
|
|
|
|
assert dict(state.attributes) == {
|
2019-07-31 19:25:30 +00:00
|
|
|
"friendly_name": TEST_ENTITY_NAME,
|
|
|
|
"message": event_summary,
|
|
|
|
"all_day": True,
|
|
|
|
"offset_reached": False,
|
|
|
|
"start_time": tomorrow.strftime(DATE_STR_FORMAT),
|
|
|
|
"end_time": end_event.strftime(DATE_STR_FORMAT),
|
|
|
|
"location": event["location"],
|
|
|
|
"description": event["description"],
|
2019-05-05 16:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-11 03:59:37 +00:00
|
|
|
async def test_update_error(hass, google_service):
|
2019-05-05 16:38:55 +00:00
|
|
|
"""Test that the calendar handles a server error."""
|
|
|
|
google_service.return_value.get = Mock(
|
2019-07-31 19:25:30 +00:00
|
|
|
side_effect=httplib2.ServerNotFoundError("unit test")
|
|
|
|
)
|
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
2019-05-05 16:38:55 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(TEST_ENTITY)
|
|
|
|
assert state.name == TEST_ENTITY_NAME
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "off"
|
Increase test coverage for google calendar (#62648)
* Increase test coverage for google calendar
Update tests to exercise the API responses, getting test coverage
to 97% for calendar.py
----------- coverage: platform linux, python 3.9.6-final-0 -----------
Name Stmts Miss Cover Missing
---------------------------------------------------------------------------
homeassistant/components/google/__init__.py 193 84 56% 92, 163-228, 238, 244-247, 254-262, 274, 298-299, 305-347, 387-392, 416-430, 435-437
homeassistant/components/google/calendar.py 122 4 97% 41, 45, 51, 135
---------------------------------------------------------------------------
TOTAL 315 88 72%
* Revert conftest changes
* Update typing errors found on CI
* Update python3.8 typing imports
* Remove commented out code
2021-12-23 06:31:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_calendars_api(hass, hass_client, google_service):
|
|
|
|
"""Test the Rest API returns the calendar."""
|
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
client = await hass_client()
|
|
|
|
response = await client.get("/api/calendars")
|
|
|
|
assert response.status == HTTPStatus.OK
|
|
|
|
data = await response.json()
|
|
|
|
assert data == [
|
|
|
|
{
|
|
|
|
"entity_id": TEST_ENTITY,
|
|
|
|
"name": TEST_ENTITY_NAME,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
async def test_http_event_api_failure(hass, hass_client, google_service):
|
|
|
|
"""Test the Rest API response during a calendar failure."""
|
|
|
|
google_service.return_value.get = Mock(
|
|
|
|
side_effect=httplib2.ServerNotFoundError("unit test")
|
|
|
|
)
|
|
|
|
|
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
start = dt_util.now().isoformat()
|
|
|
|
end = (dt_util.now() + dt_util.dt.timedelta(minutes=60)).isoformat()
|
|
|
|
|
|
|
|
client = await hass_client()
|
|
|
|
response = await client.get(f"/api/calendars/{TEST_ENTITY}?start={start}&end={end}")
|
|
|
|
assert response.status == HTTPStatus.OK
|
|
|
|
# A failure to talk to the server results in an empty list of events
|
|
|
|
events = await response.json()
|
|
|
|
assert events == []
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_events_list(
|
|
|
|
google_service: GoogleCalendarService,
|
|
|
|
) -> Callable[[dict[str, Any]], None]:
|
|
|
|
"""Fixture to construct a fake event list API response."""
|
|
|
|
|
|
|
|
def _put_result(response: dict[str, Any]) -> None:
|
|
|
|
google_service.return_value.get.return_value.events.return_value.list.return_value.execute.return_value = (
|
|
|
|
response
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
return _put_result
|
|
|
|
|
|
|
|
|
|
|
|
async def test_http_api_event(hass, hass_client, google_service, mock_events_list):
|
|
|
|
"""Test querying the API and fetching events from the server."""
|
|
|
|
now = dt_util.now()
|
|
|
|
|
|
|
|
mock_events_list(
|
|
|
|
{
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"summary": "Event title",
|
|
|
|
"start": {"dateTime": now.isoformat()},
|
|
|
|
"end": {
|
|
|
|
"dateTime": (now + dt_util.dt.timedelta(minutes=5)).isoformat()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
start = (now - dt_util.dt.timedelta(minutes=60)).isoformat()
|
|
|
|
end = (now + dt_util.dt.timedelta(minutes=60)).isoformat()
|
|
|
|
|
|
|
|
client = await hass_client()
|
|
|
|
response = await client.get(f"/api/calendars/{TEST_ENTITY}?start={start}&end={end}")
|
|
|
|
assert response.status == HTTPStatus.OK
|
|
|
|
events = await response.json()
|
|
|
|
assert len(events) == 1
|
|
|
|
assert "summary" in events[0]
|
|
|
|
assert events[0]["summary"] == "Event title"
|
|
|
|
|
|
|
|
|
|
|
|
def create_ignore_avail_calendar() -> dict[str, Any]:
|
|
|
|
"""Create a calendar with ignore_availability set."""
|
|
|
|
calendar = TEST_CALENDAR.copy()
|
|
|
|
calendar["ignore_availability"] = False
|
|
|
|
return calendar
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("test_calendar", [create_ignore_avail_calendar()])
|
|
|
|
async def test_opaque_event(hass, hass_client, google_service, mock_events_list):
|
|
|
|
"""Test querying the API and fetching events from the server."""
|
|
|
|
now = dt_util.now()
|
|
|
|
|
|
|
|
mock_events_list(
|
|
|
|
{
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"summary": "Event title",
|
|
|
|
"transparency": "opaque",
|
|
|
|
"start": {"dateTime": now.isoformat()},
|
|
|
|
"end": {
|
|
|
|
"dateTime": (now + dt_util.dt.timedelta(minutes=5)).isoformat()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
start = (now - dt_util.dt.timedelta(minutes=60)).isoformat()
|
|
|
|
end = (now + dt_util.dt.timedelta(minutes=60)).isoformat()
|
|
|
|
|
|
|
|
client = await hass_client()
|
|
|
|
response = await client.get(f"/api/calendars/{TEST_ENTITY}?start={start}&end={end}")
|
|
|
|
assert response.status == HTTPStatus.OK
|
|
|
|
events = await response.json()
|
|
|
|
assert len(events) == 1
|
|
|
|
assert "summary" in events[0]
|
|
|
|
assert events[0]["summary"] == "Event title"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("test_calendar", [create_ignore_avail_calendar()])
|
|
|
|
async def test_transparent_event(hass, hass_client, google_service, mock_events_list):
|
|
|
|
"""Test querying the API and fetching events from the server."""
|
|
|
|
now = dt_util.now()
|
|
|
|
|
|
|
|
mock_events_list(
|
|
|
|
{
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"summary": "Event title",
|
|
|
|
"transparency": "transparent",
|
|
|
|
"start": {"dateTime": now.isoformat()},
|
|
|
|
"end": {
|
|
|
|
"dateTime": (now + dt_util.dt.timedelta(minutes=5)).isoformat()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert await async_setup_component(hass, "google", {"google": GOOGLE_CONFIG})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
start = (now - dt_util.dt.timedelta(minutes=60)).isoformat()
|
|
|
|
end = (now + dt_util.dt.timedelta(minutes=60)).isoformat()
|
|
|
|
|
|
|
|
client = await hass_client()
|
|
|
|
response = await client.get(f"/api/calendars/{TEST_ENTITY}?start={start}&end={end}")
|
|
|
|
assert response.status == HTTPStatus.OK
|
|
|
|
events = await response.json()
|
|
|
|
assert events == []
|