mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Improve google calendar test coverage to 97% (#65223)
* Improve google calendar test coverage to 97% * Remove commented out code. * Remove unnecessary (flaky) checks for token file persistence * Remove mock code assertions * Add debug logging to google calendar integration * Increase alarm time to polling code to reduce flakes * Setup every test in their own configuration directory * Mock out filesystem calls to avoid disk dependencies Update scope checking code to use Storage object rather than text file matching * Update tests to check entity states when integration is loaded * Mock out google service in multiple locations * Update homeassistant/components/google/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/google/__init__.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
@@ -1,10 +1,20 @@
|
||||
"""Test configuration and mocks for the google integration."""
|
||||
from unittest.mock import patch
|
||||
from collections.abc import Callable
|
||||
from typing import Any, Generator, TypeVar
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.google import GoogleCalendarService
|
||||
|
||||
ApiResult = Callable[[dict[str, Any]], None]
|
||||
T = TypeVar("T")
|
||||
YieldFixture = Generator[T, None, None]
|
||||
|
||||
|
||||
CALENDAR_ID = "qwertyuiopasdfghjklzxcvbnm@import.calendar.google.com"
|
||||
TEST_CALENDAR = {
|
||||
"id": "qwertyuiopasdfghjklzxcvbnm@import.calendar.google.com",
|
||||
"id": CALENDAR_ID,
|
||||
"etag": '"3584134138943410"',
|
||||
"timeZone": "UTC",
|
||||
"accessRole": "reader",
|
||||
@@ -34,3 +44,45 @@ def mock_next_event():
|
||||
)
|
||||
with patch_google_cal as google_cal_data:
|
||||
yield google_cal_data
|
||||
|
||||
|
||||
@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
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_calendars_list(
|
||||
google_service: GoogleCalendarService,
|
||||
) -> ApiResult:
|
||||
"""Fixture to construct a fake calendar list API response."""
|
||||
|
||||
def _put_result(response: dict[str, Any]) -> None:
|
||||
google_service.return_value.get.return_value.calendarList.return_value.list.return_value.execute.return_value = (
|
||||
response
|
||||
)
|
||||
return
|
||||
|
||||
return _put_result
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_insert_event(
|
||||
google_service: GoogleCalendarService,
|
||||
) -> Mock:
|
||||
"""Fixture to create a mock to capture new events added to the API."""
|
||||
insert_mock = Mock()
|
||||
google_service.return_value.get.return_value.events.return_value.insert = (
|
||||
insert_mock
|
||||
)
|
||||
return insert_mock
|
||||
|
||||
Reference in New Issue
Block a user