2022-06-03 11:06:36 +00:00
|
|
|
import datetime
|
2022-06-04 15:51:43 +00:00
|
|
|
from typing import Tuple, Any
|
2021-05-01 10:47:02 +00:00
|
|
|
|
2018-04-17 13:01:21 +00:00
|
|
|
import pytest
|
2022-06-03 11:06:36 +00:00
|
|
|
from pytz import timezone, utc
|
2018-04-17 13:01:21 +00:00
|
|
|
|
2020-02-19 20:26:28 +00:00
|
|
|
from sync_ics2gcal import CalendarConverter
|
2022-06-03 11:06:36 +00:00
|
|
|
from sync_ics2gcal.ical import format_datetime_utc
|
2018-04-05 18:39:22 +00:00
|
|
|
|
2018-04-17 13:20:24 +00:00
|
|
|
uid = "UID:uisgtr8tre93wewe0yr8wqy@test.com"
|
2022-03-08 15:46:21 +00:00
|
|
|
only_start_date = (
|
|
|
|
uid
|
|
|
|
+ """
|
2018-04-07 13:04:04 +00:00
|
|
|
DTSTART;VALUE=DATE:20180215
|
|
|
|
"""
|
2022-03-08 15:46:21 +00:00
|
|
|
)
|
|
|
|
date_val = (
|
|
|
|
only_start_date
|
|
|
|
+ """
|
2018-04-07 13:04:04 +00:00
|
|
|
DTEND;VALUE=DATE:20180217
|
|
|
|
"""
|
2022-03-08 15:46:21 +00:00
|
|
|
)
|
|
|
|
date_duration = (
|
|
|
|
only_start_date
|
|
|
|
+ """
|
2018-04-17 13:01:21 +00:00
|
|
|
DURATION:P2D
|
2018-04-07 13:04:04 +00:00
|
|
|
"""
|
2022-03-08 15:46:21 +00:00
|
|
|
)
|
|
|
|
datetime_utc_val = (
|
|
|
|
uid
|
|
|
|
+ """
|
2018-04-17 13:01:21 +00:00
|
|
|
DTSTART;VALUE=DATE-TIME:20180319T092001Z
|
|
|
|
DTEND:20180321T102501Z
|
2018-04-07 13:04:04 +00:00
|
|
|
"""
|
2022-03-08 15:46:21 +00:00
|
|
|
)
|
|
|
|
datetime_utc_duration = (
|
|
|
|
uid
|
|
|
|
+ """
|
2018-04-07 13:04:04 +00:00
|
|
|
DTSTART;VALUE=DATE-TIME:20180319T092001Z
|
|
|
|
DURATION:P2DT1H5M
|
|
|
|
"""
|
2022-03-08 15:46:21 +00:00
|
|
|
)
|
|
|
|
created_updated = (
|
|
|
|
date_val
|
|
|
|
+ """
|
2018-04-07 13:04:04 +00:00
|
|
|
CREATED:20180320T071155Z
|
|
|
|
LAST-MODIFIED:20180326T120235Z
|
|
|
|
"""
|
2022-03-08 15:46:21 +00:00
|
|
|
)
|
2018-04-07 13:04:04 +00:00
|
|
|
|
2018-04-15 20:41:50 +00:00
|
|
|
|
2021-05-01 10:47:02 +00:00
|
|
|
def ics_test_cal(content: str) -> str:
|
2018-04-17 13:20:24 +00:00
|
|
|
return "BEGIN:VCALENDAR\r\n{}END:VCALENDAR\r\n".format(content)
|
|
|
|
|
|
|
|
|
2021-05-01 10:47:02 +00:00
|
|
|
def ics_test_event(content: str) -> str:
|
2018-04-17 13:20:24 +00:00
|
|
|
return ics_test_cal("BEGIN:VEVENT\r\n{}END:VEVENT\r\n".format(content))
|
|
|
|
|
|
|
|
|
2022-06-04 15:51:43 +00:00
|
|
|
def test_empty_calendar() -> None:
|
2018-04-17 13:01:21 +00:00
|
|
|
converter = CalendarConverter()
|
2018-04-17 13:20:24 +00:00
|
|
|
converter.loads(ics_test_cal(""))
|
2018-04-17 13:01:21 +00:00
|
|
|
evnts = converter.events_to_gcal()
|
|
|
|
assert evnts == []
|
|
|
|
|
|
|
|
|
2022-06-04 15:51:43 +00:00
|
|
|
def test_empty_event() -> None:
|
2018-04-17 13:01:21 +00:00
|
|
|
converter = CalendarConverter()
|
2018-04-17 13:20:24 +00:00
|
|
|
converter.loads(ics_test_event(""))
|
2018-04-17 13:01:21 +00:00
|
|
|
with pytest.raises(KeyError):
|
|
|
|
converter.events_to_gcal()
|
|
|
|
|
|
|
|
|
2022-06-04 15:51:43 +00:00
|
|
|
def test_event_no_end() -> None:
|
2018-04-17 13:01:21 +00:00
|
|
|
converter = CalendarConverter()
|
2018-04-17 13:20:24 +00:00
|
|
|
converter.loads(ics_test_event(only_start_date))
|
2018-04-17 13:01:21 +00:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
converter.events_to_gcal()
|
|
|
|
|
2018-04-17 13:20:24 +00:00
|
|
|
|
2022-03-08 15:46:21 +00:00
|
|
|
@pytest.fixture(
|
|
|
|
params=[
|
|
|
|
("date", ics_test_event(date_val), "2018-02-15", "2018-02-17"),
|
|
|
|
("date", ics_test_event(date_duration), "2018-02-15", "2018-02-17"),
|
|
|
|
(
|
|
|
|
"dateTime",
|
|
|
|
ics_test_event(datetime_utc_val),
|
|
|
|
"2018-03-19T09:20:01.000001Z",
|
|
|
|
"2018-03-21T10:25:01.000001Z",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"dateTime",
|
|
|
|
ics_test_event(datetime_utc_duration),
|
|
|
|
"2018-03-19T09:20:01.000001Z",
|
|
|
|
"2018-03-21T10:25:01.000001Z",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"date values",
|
|
|
|
"date duration",
|
|
|
|
"datetime utc values",
|
|
|
|
"datetime utc duration",
|
|
|
|
],
|
2018-04-17 13:01:21 +00:00
|
|
|
)
|
2022-06-04 15:51:43 +00:00
|
|
|
def param_events_start_end(request: Any) -> Any:
|
2018-04-17 13:01:21 +00:00
|
|
|
return request.param
|
|
|
|
|
2018-04-17 13:20:24 +00:00
|
|
|
|
2022-06-04 15:51:43 +00:00
|
|
|
def test_event_start_end(param_events_start_end: Tuple[str, str, str, str]) -> None:
|
2018-04-17 13:01:21 +00:00
|
|
|
(date_type, ics_str, start, end) = param_events_start_end
|
|
|
|
converter = CalendarConverter()
|
|
|
|
converter.loads(ics_str)
|
|
|
|
events = converter.events_to_gcal()
|
|
|
|
assert len(events) == 1
|
|
|
|
event = events[0]
|
2022-03-08 15:46:21 +00:00
|
|
|
assert event["start"] == {date_type: start}
|
|
|
|
assert event["end"] == {date_type: end}
|
2018-04-17 13:01:21 +00:00
|
|
|
|
2018-04-17 13:20:24 +00:00
|
|
|
|
2022-06-04 15:51:43 +00:00
|
|
|
def test_event_created_updated() -> None:
|
2018-04-17 13:01:21 +00:00
|
|
|
converter = CalendarConverter()
|
2018-04-17 13:20:24 +00:00
|
|
|
converter.loads(ics_test_event(created_updated))
|
2018-04-17 13:01:21 +00:00
|
|
|
events = converter.events_to_gcal()
|
|
|
|
assert len(events) == 1
|
|
|
|
event = events[0]
|
2022-03-08 15:46:21 +00:00
|
|
|
assert event["created"] == "2018-03-20T07:11:55.000001Z"
|
|
|
|
assert event["updated"] == "2018-03-26T12:02:35.000001Z"
|
2022-06-03 11:06:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"value,expected_str",
|
|
|
|
[
|
|
|
|
(
|
|
|
|
datetime.datetime(2022, 6, 3, 13, 52, 15, 1, utc),
|
|
|
|
"2022-06-03T13:52:15.000001Z",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
datetime.datetime(2022, 6, 3, 13, 52, 15, 1, timezone("Europe/Moscow")),
|
|
|
|
"2022-06-03T11:22:15.000001Z",
|
|
|
|
),
|
|
|
|
(datetime.date(2022, 6, 3), "2022-06-03T00:00:00.000001Z"),
|
|
|
|
],
|
|
|
|
ids=["utc", "with timezone", "date"],
|
|
|
|
)
|
2022-06-04 15:51:43 +00:00
|
|
|
def test_format_datetime_utc(value: datetime.datetime, expected_str: str) -> None:
|
2022-06-03 11:06:36 +00:00
|
|
|
assert format_datetime_utc(value) == expected_str
|