From 9881538c279f9aa3031d395f1ce38b2e7d0ae3f3 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Mon, 28 Mar 2022 13:22:49 -0700 Subject: [PATCH] Improve caldav tests to use APIs rather than entity methods (#68745) * Improve caldav tests to use APIs rather than entity methods Update caldav tests to use the best practice of exercising the public APIs rather than directly calling the entity methods directly. This is motivated by additional calendar API cleanup and possibly future breaking changes. * Remove unnecessary start/end arguments which are ignored --- tests/components/caldav/test_calendar.py | 46 +++++++++++++++++------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/tests/components/caldav/test_calendar.py b/tests/components/caldav/test_calendar.py index 7bfb8b620f6..cb23381d1c6 100644 --- a/tests/components/caldav/test_calendar.py +++ b/tests/components/caldav/test_calendar.py @@ -1,5 +1,6 @@ """The tests for the webdav calendar component.""" import datetime +from http import HTTPStatus from unittest.mock import MagicMock, Mock, patch from caldav.objects import Event @@ -274,6 +275,23 @@ def mock_private_cal(): yield _calendar +@pytest.fixture +def get_api_events(hass_client): + """Fixture to return events for a specific calendar using the API.""" + + async def api_call(entity_id): + client = await hass_client() + response = await client.get( + # The start/end times are arbitrary since they are ignored by `_mock_calendar` + # which just returns all events for the calendar. + f"/api/calendars/{entity_id}?start=2022-01-01&end=2022-01-01" + ) + assert response.status == HTTPStatus.OK + return await response.json() + + return api_call + + def _local_datetime(hours, minutes): """Build a datetime object for testing in the correct timezone.""" return dt.as_local(datetime.datetime(2017, 11, 27, hours, minutes, 0)) @@ -893,18 +911,17 @@ async def test_event_rrule_hourly_ended(mock_now, hass, calendar): assert state.state == STATE_OFF -async def test_get_events(hass, calendar): +async def test_get_events(hass, calendar, get_api_events): """Test that all events are returned on API.""" assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG}) await hass.async_block_till_done() - entity = hass.data["calendar"].get_entity("calendar.private") - events = await entity.async_get_events( - hass, datetime.date(2015, 11, 27), datetime.date(2015, 11, 28) - ) + + events = await get_api_events("calendar.private") assert len(events) == 14 + assert calendar.call -async def test_get_events_custom_calendars(hass, calendar): +async def test_get_events_custom_calendars(hass, calendar, get_api_events): """Test that only searched events are returned on API.""" config = dict(CALDAV_CONFIG) config["custom_calendars"] = [ @@ -914,9 +931,14 @@ async def test_get_events_custom_calendars(hass, calendar): assert await async_setup_component(hass, "calendar", {"calendar": config}) await hass.async_block_till_done() - entity = hass.data["calendar"].get_entity("calendar.private_private") - events = await entity.async_get_events( - hass, datetime.date(2015, 11, 27), datetime.date(2015, 11, 28) - ) - assert len(events) == 1 - assert events[0]["summary"] == "This is a normal event" + events = await get_api_events("calendar.private_private") + assert events == [ + { + "description": "Surprisingly rainy", + "end": "2017-11-27T10:00:00-08:00", + "location": "Hamburg", + "start": "2017-11-27T09:00:00-08:00", + "summary": "This is a normal event", + "uid": "1", + } + ]