core/tests/components/calendar/test_init.py

70 lines
2.6 KiB
Python
Raw Normal View History

"""The tests for the calendar component."""
2018-06-15 17:37:46 +00:00
from datetime import timedelta
from http import HTTPStatus
from unittest.mock import patch
2018-06-15 17:37:46 +00:00
from homeassistant.bootstrap import async_setup_component
from homeassistant.exceptions import HomeAssistantError
2018-06-15 17:37:46 +00:00
import homeassistant.util.dt as dt_util
async def test_events_http_api(hass, hass_client):
2018-06-15 17:37:46 +00:00
"""Test the calendar demo view."""
2019-07-31 19:25:30 +00:00
await async_setup_component(hass, "calendar", {"calendar": {"platform": "demo"}})
await hass.async_block_till_done()
client = await hass_client()
2018-06-15 17:37:46 +00:00
start = dt_util.now()
end = start + timedelta(days=1)
response = await client.get(
2019-07-31 19:25:30 +00:00
"/api/calendars/calendar.calendar_1?start={}&end={}".format(
start.isoformat(), end.isoformat()
)
)
assert response.status == HTTPStatus.OK
2018-06-15 17:37:46 +00:00
events = await response.json()
2019-07-31 19:25:30 +00:00
assert events[0]["summary"] == "Future Event"
2018-06-15 17:37:46 +00:00
async def test_events_http_api_missing_fields(hass, hass_client):
"""Test the calendar demo view."""
await async_setup_component(hass, "calendar", {"calendar": {"platform": "demo"}})
await hass.async_block_till_done()
client = await hass_client()
response = await client.get("/api/calendars/calendar.calendar_2")
assert response.status == HTTPStatus.BAD_REQUEST
async def test_events_http_api_error(hass, hass_client):
"""Test the calendar demo view."""
await async_setup_component(hass, "calendar", {"calendar": {"platform": "demo"}})
await hass.async_block_till_done()
client = await hass_client()
start = dt_util.now()
end = start + timedelta(days=1)
with patch(
"homeassistant.components.demo.calendar.DemoCalendar.async_get_events",
side_effect=HomeAssistantError("Failure"),
):
response = await client.get(
"/api/calendars/calendar.calendar_1?start={}&end={}".format(
start.isoformat(), end.isoformat()
)
)
assert response.status == HTTPStatus.INTERNAL_SERVER_ERROR
assert await response.json() == {"message": "Error reading events: Failure"}
async def test_calendars_http_api(hass, hass_client):
2018-06-15 17:37:46 +00:00
"""Test the calendar demo view."""
2019-07-31 19:25:30 +00:00
await async_setup_component(hass, "calendar", {"calendar": {"platform": "demo"}})
await hass.async_block_till_done()
client = await hass_client()
2019-07-31 19:25:30 +00:00
response = await client.get("/api/calendars")
assert response.status == HTTPStatus.OK
2018-06-15 17:37:46 +00:00
data = await response.json()
assert data == [
2019-07-31 19:25:30 +00:00
{"entity_id": "calendar.calendar_1", "name": "Calendar 1"},
{"entity_id": "calendar.calendar_2", "name": "Calendar 2"},
2018-06-15 17:37:46 +00:00
]