diff --git a/.gitignore b/.gitignore index fd8db29..9169049 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ example.py coverage.xml .coverage .DS_Store -.tox \ No newline at end of file +.tox +.vscode diff --git a/gcsa/event.py b/gcsa/event.py index b6c547b..36f0d05 100644 --- a/gcsa/event.py +++ b/gcsa/event.py @@ -1,5 +1,6 @@ from functools import total_ordering -from typing import List, Union +import logging +from typing import List, Optional, Union from beautiful_date import BeautifulDate from tzlocal import get_localzone_name @@ -13,6 +14,8 @@ from .reminders import PopupReminder, EmailReminder, Reminder from .util.date_time_util import ensure_localisation +log = logging.getLogger(__name__) + class Visibility: """Possible values of the event visibility. @@ -44,7 +47,7 @@ class Transparency: class Event(Resource): def __init__( self, - summary: str, + summary: Optional[str], start: Union[date, datetime, BeautifulDate], end: Union[date, datetime, BeautifulDate] = None, *, @@ -159,6 +162,13 @@ def ensure_list(obj): if isinstance(self.start, datetime) and isinstance(self.end, datetime): self.start = ensure_localisation(self.start, timezone) self.end = ensure_localisation(self.end, timezone) + + if self.start.microsecond != 0 or self.end.microsecond != 0: + log.warning( + "Microseconds are used in start/end, " + + "but are not supported in the Google Calendar API " + + "and will be dropped on submission." + ) elif isinstance(self.start, datetime) or isinstance(self.end, datetime): raise TypeError('Start and end must either both be date or both be datetime.') @@ -186,6 +196,12 @@ def ensure_date(d): self.event_id = event_id self.summary = summary + if self.summary == "": + log.warning( + f"Summary is empty in {self}. Note that if the event is loaded " + + "from Google Calendar, its summary will be `None`" + ) + self.description = description self.location = location self.recurrence = ensure_list(recurrence) diff --git a/tests/test_event.py b/tests/test_event.py index 786e464..3444b54 100644 --- a/tests/test_event.py +++ b/tests/test_event.py @@ -1,4 +1,5 @@ from datetime import time +import datetime from unittest import TestCase from beautiful_date import Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Dec, hours, days, Nov @@ -67,6 +68,41 @@ def test_init_no_end(self): event = Event('Lunch', start, timezone=TEST_TIMEZONE) self.assertEqual(event.end, start + 1 * hours) + def test_init_with_microseconds(self): + with self.assertLogs("gcsa.event", level="WARNING") as cm: + Event( + "Test", + start=datetime.datetime( + year=1979, month=1, day=1, hour=1, minute=1, second=1, microsecond=1 + ), + ) + self.assertEqual( + cm.output, + [ + "WARNING:gcsa.event:Microseconds are used in start/end, " + + "but are not supported in the Google Calendar API " + + "and will be dropped on submission." + ], + ) + + def test_init_without_title(self): + with self.assertLogs("gcsa.event", level="WARNING") as cm: + Event( + "", + start=datetime.datetime( + year=1979, month=1, day=1, hour=1, minute=1, second=1, microsecond=0 + ), + timezone=TEST_TIMEZONE, + ) + self.assertEqual( + cm.output, + [ + "WARNING:gcsa.event:Summary is empty in 1979-01-01 01:01:01+12:00 - . " + + "Note that if the event is loaded from Google Calendar, " + + "its summary will be `None`" + ], + ) + def test_init_no_start_or_end(self): event = Event('Good day', start=None, timezone=TEST_TIMEZONE) self.assertIsNone(event.start)