From 89877bc6d8277a8853fa1c01dd3313138494c32a Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 21 Feb 2022 22:39:40 +0300 Subject: [PATCH 1/3] add test_filter_events_to_update_no_updated #80 --- tests/test_sync.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_sync.py b/tests/test_sync.py index f480260..aed1ff0 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -178,3 +178,29 @@ def test_filter_events_to_update(): assert len(sync1.to_update) == count assert sync2.to_update == [] + + +def test_filter_events_to_update_no_updated(): + now = utc.localize(datetime.datetime.utcnow()) + + one_hour = datetime.datetime( + 1, 1, 1, 2) - datetime.datetime(1, 1, 1, 1) + date_upd = now + (one_hour * 5) + + count = 10 + events_old = gen_events(1, 1 + count, now) + events_new = gen_events(1, 1 + count, date_upd) + + for event in events_new: + del event['updated'] + + sync1 = CalendarSync(None, None) + sync1.to_update = list(zip(events_new, events_old)) + sync1._filter_events_to_update() + + sync2 = CalendarSync(None, None) + sync2.to_update = list(zip(events_old, events_new)) + sync2._filter_events_to_update() + + assert len(sync1.to_update) == count + assert sync2.to_update == [] From b09136747fa5d974364fcc7d7f9d59bb814a4fea Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 22 Feb 2022 09:52:28 +0300 Subject: [PATCH 2/3] rewrite test_filter_events_no_updated --- tests/test_sync.py | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/tests/test_sync.py b/tests/test_sync.py index aed1ff0..0a4e312 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -180,27 +180,28 @@ def test_filter_events_to_update(): assert sync2.to_update == [] -def test_filter_events_to_update_no_updated(): - now = utc.localize(datetime.datetime.utcnow()) - - one_hour = datetime.datetime( - 1, 1, 1, 2) - datetime.datetime(1, 1, 1, 1) - date_upd = now + (one_hour * 5) +def test_filter_events_no_updated(): + """ + test filtering events that not have 'updated' field + such events should always pass the filter + """ + now = datetime.datetime.utcnow() + yesterday = now - datetime.timedelta(days=-1) count = 10 events_old = gen_events(1, 1 + count, now) - events_new = gen_events(1, 1 + count, date_upd) + events_new = gen_events(1, 1 + count, now) + # 1/2 updated=yesterday, 1/2 no updated field + i = 0 for event in events_new: - del event['updated'] + if 0 == i % 2: + event['updated'] = yesterday.isoformat() + 'Z' + else: + del event['updated'] + i += 1 - sync1 = CalendarSync(None, None) - sync1.to_update = list(zip(events_new, events_old)) - sync1._filter_events_to_update() - - sync2 = CalendarSync(None, None) - sync2.to_update = list(zip(events_old, events_new)) - sync2._filter_events_to_update() - - assert len(sync1.to_update) == count - assert sync2.to_update == [] + sync = CalendarSync(None, None) + sync.to_update = list(zip(events_old, events_new)) + sync._filter_events_to_update() + assert len(sync.to_update) == count // 2 From 693c6c43598d2af4c4032cc4d2c89d4eb7c1fe8f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 22 Feb 2022 10:15:05 +0300 Subject: [PATCH 3/3] overwrite events without `updated` field --- sync_ics2gcal/sync.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sync_ics2gcal/sync.py b/sync_ics2gcal/sync.py index d37964e..1c56f6d 100644 --- a/sync_ics2gcal/sync.py +++ b/sync_ics2gcal/sync.py @@ -72,6 +72,8 @@ class CalendarSync: def filter_updated(event_tuple: EventTuple) -> bool: new, old = event_tuple + if 'updated' not in new or 'updated' not in old: + return True new_date = dateutil.parser.parse(new['updated']) old_date = dateutil.parser.parse(old['updated']) return new_date > old_date