Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warn on microsecond granularity and empty string summary #191

Merged
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ example.py
coverage.xml
.coverage
.DS_Store
.tox
.tox
.vscode
20 changes: 18 additions & 2 deletions gcsa/event.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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,
*,
Expand Down Expand Up @@ -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.')

Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_event.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down
Loading