mirror of
https://github.com/b4tman/sync_ics2gcal
synced 2025-02-01 12:28:29 +00:00
test_sync rewrite using pytest
This commit is contained in:
parent
508ddedb2c
commit
c38a3afe00
@ -1,18 +1,16 @@
|
||||
import datetime
|
||||
import hashlib
|
||||
import operator
|
||||
import unittest
|
||||
from copy import deepcopy
|
||||
from random import shuffle
|
||||
|
||||
import dateutil.parser
|
||||
import pytest
|
||||
from pytz import timezone, utc
|
||||
|
||||
from gcal_sync import CalendarSync
|
||||
|
||||
|
||||
class TestCalendarSync(unittest.TestCase):
|
||||
@staticmethod
|
||||
def sha1(string):
|
||||
if isinstance(string, str):
|
||||
string = string.encode('utf8')
|
||||
@ -20,7 +18,6 @@ class TestCalendarSync(unittest.TestCase):
|
||||
h.update(string)
|
||||
return h.hexdigest()
|
||||
|
||||
@staticmethod
|
||||
def gen_events(start, stop, start_time, no_time=False):
|
||||
if no_time:
|
||||
start_time = datetime.date(
|
||||
@ -50,7 +47,7 @@ class TestCalendarSync(unittest.TestCase):
|
||||
'summary': 'test event __ {}'.format(i),
|
||||
'location': 'la la la {}'.format(i),
|
||||
'description': 'test TEST -- test event {}'.format(i),
|
||||
"iCalUID": "{}@test.com".format(TestCalendarSync.sha1("test - event {}".format(i))),
|
||||
"iCalUID": "{}@test.com".format(sha1("test - event {}".format(i))),
|
||||
"updated": updated.isoformat() + 'Z',
|
||||
"created": updated.isoformat() + 'Z'
|
||||
}
|
||||
@ -59,14 +56,12 @@ class TestCalendarSync(unittest.TestCase):
|
||||
result.append(event)
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def gen_list_to_compare(start, stop):
|
||||
result = []
|
||||
for i in range(start, stop):
|
||||
result.append({'iCalUID': 'test{:06d}'.format(i)})
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def get_start_date(event):
|
||||
event_start = event['start']
|
||||
start_date = None
|
||||
@ -83,12 +78,12 @@ class TestCalendarSync(unittest.TestCase):
|
||||
|
||||
return result
|
||||
|
||||
def test_compare(self):
|
||||
def test_compare():
|
||||
part_len = 20
|
||||
# [1..2n]
|
||||
lst_src = TestCalendarSync.gen_list_to_compare(1, 1 + part_len * 2)
|
||||
lst_src = gen_list_to_compare(1, 1 + part_len * 2)
|
||||
# [n..3n]
|
||||
lst_dst = TestCalendarSync.gen_list_to_compare(
|
||||
lst_dst = gen_list_to_compare(
|
||||
1 + part_len, 1 + part_len * 3)
|
||||
|
||||
lst_src_rnd = deepcopy(lst_src)
|
||||
@ -100,21 +95,19 @@ class TestCalendarSync(unittest.TestCase):
|
||||
to_ins, to_upd, to_del = CalendarSync._events_list_compare(
|
||||
lst_src_rnd, lst_dst_rnd)
|
||||
|
||||
self.assertEqual(len(to_ins), part_len)
|
||||
self.assertEqual(len(to_upd), part_len)
|
||||
self.assertEqual(len(to_del), part_len)
|
||||
assert len(to_ins) == part_len
|
||||
assert len(to_upd) == part_len
|
||||
assert len(to_del) == part_len
|
||||
|
||||
self.assertEqual(
|
||||
sorted(to_ins, key=lambda x: x['iCalUID']), lst_src[:part_len])
|
||||
self.assertEqual(
|
||||
sorted(to_del, key=lambda x: x['iCalUID']), lst_dst[part_len:])
|
||||
assert sorted(to_ins, key=lambda x: x['iCalUID']) == lst_src[:part_len]
|
||||
assert sorted(to_del, key=lambda x: x['iCalUID']) == lst_dst[part_len:]
|
||||
|
||||
to_upd_ok = list(zip(lst_src[part_len:], lst_dst[:part_len]))
|
||||
self.assertEqual(len(to_upd), len(to_upd_ok))
|
||||
assert len(to_upd) == len(to_upd_ok)
|
||||
for item in to_upd_ok:
|
||||
self.assertIn(item, to_upd)
|
||||
assert item in to_upd
|
||||
|
||||
def test_filter_events_by_date(self, no_time=False):
|
||||
def test_filter_events_by_date(no_time=False):
|
||||
msk = timezone('Europe/Moscow')
|
||||
now = utc.localize(datetime.datetime.utcnow())
|
||||
msk_now = msk.normalize(now.astimezone(msk))
|
||||
@ -134,7 +127,7 @@ class TestCalendarSync(unittest.TestCase):
|
||||
date_cmp = datetime.date(
|
||||
date_cmp.year, date_cmp.month, date_cmp.day)
|
||||
|
||||
events = TestCalendarSync.gen_events(
|
||||
events = gen_events(
|
||||
1, 1 + (part_len * 2), msk_now, no_time)
|
||||
shuffle(events)
|
||||
|
||||
@ -143,20 +136,19 @@ class TestCalendarSync(unittest.TestCase):
|
||||
events_past = CalendarSync._filter_events_by_date(
|
||||
events, date_cmp, operator.lt)
|
||||
|
||||
self.assertEqual(len(events_pending), 1 + part_len)
|
||||
self.assertEqual(len(events_past), part_len - 1)
|
||||
assert len(events_pending) == 1 + part_len
|
||||
assert len(events_past) == part_len - 1
|
||||
|
||||
for event in events_pending:
|
||||
self.assertGreaterEqual(
|
||||
TestCalendarSync.get_start_date(event), date_cmp)
|
||||
assert get_start_date(event) >= date_cmp
|
||||
|
||||
for event in events_past:
|
||||
self.assertLess(TestCalendarSync.get_start_date(event), date_cmp)
|
||||
assert get_start_date(event) < date_cmp
|
||||
|
||||
def test_filter_events_by_date_no_time(self):
|
||||
self.test_filter_events_by_date(no_time=True)
|
||||
def test_filter_events_by_date_no_time():
|
||||
test_filter_events_by_date(no_time=True)
|
||||
|
||||
def test_filter_events_to_update(self):
|
||||
def test_filter_events_to_update():
|
||||
msk = timezone('Europe/Moscow')
|
||||
now = utc.localize(datetime.datetime.utcnow())
|
||||
msk_now = msk.normalize(now.astimezone(msk))
|
||||
@ -166,8 +158,8 @@ class TestCalendarSync(unittest.TestCase):
|
||||
date_upd = msk_now + (one_hour * 5)
|
||||
|
||||
count = 10
|
||||
events_old = TestCalendarSync.gen_events(1, 1 + count, msk_now)
|
||||
events_new = TestCalendarSync.gen_events(1, 1 + count, date_upd)
|
||||
events_old = gen_events(1, 1 + count, msk_now)
|
||||
events_new = gen_events(1, 1 + count, date_upd)
|
||||
|
||||
sync1 = CalendarSync(None, None)
|
||||
sync1.to_update = list(zip(events_new, events_old))
|
||||
@ -177,9 +169,5 @@ class TestCalendarSync(unittest.TestCase):
|
||||
sync2.to_update = list(zip(events_old, events_new))
|
||||
sync2._filter_events_to_update()
|
||||
|
||||
self.assertEqual(len(sync1.to_update), count)
|
||||
self.assertEqual(len(sync2.to_update), 0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
assert len(sync1.to_update) == count
|
||||
assert sync2.to_update == []
|
||||
|
Loading…
x
Reference in New Issue
Block a user