icalevents/test/test_icalparser.py

93 wiersze
3.7 KiB
Python
Czysty Zwykły widok Historia

2017-07-19 08:05:03 +00:00
import unittest
import icalevents.icalparser
from datetime import datetime, date
from dateutil.tz import UTC, gettz
2017-07-19 08:05:03 +00:00
class ICalParserTests(unittest.TestCase):
def setUp(self):
self.eventA = icalevents.icalparser.Event()
self.eventA.uid = 1234
2021-10-11 17:58:18 +00:00
self.eventA.start = datetime(
year=2017, month=2, day=3, hour=12, minute=5, tzinfo=UTC
)
self.eventA.end = datetime(
year=2017, month=2, day=3, hour=15, minute=5, tzinfo=UTC
)
2017-07-19 08:05:03 +00:00
self.eventA.all_day = False
self.eventA.summary = "Event A"
2019-02-07 01:53:06 +00:00
self.eventA.attendee = "name@example.com"
self.eventA.organizer = "name@example.com"
2017-07-19 08:05:03 +00:00
self.eventB = icalevents.icalparser.Event()
self.eventB.uid = 1234
2021-10-11 17:58:18 +00:00
self.eventB.start = datetime(
year=2017, month=2, day=1, hour=15, minute=5, tzinfo=UTC
)
self.eventB.end = datetime(
year=2017, month=2, day=1, hour=16, minute=5, tzinfo=UTC
)
2017-07-19 08:05:03 +00:00
self.eventB.all_day = False
self.eventB.summary = "Event B"
2019-02-07 01:53:06 +00:00
self.eventB.attendee = ["name@example.com", "another@example.com"]
2019-02-07 07:27:05 +00:00
self.eventB.organizer = "name@example.com"
2019-02-07 01:53:06 +00:00
self.dtA = datetime(2018, 6, 21, 12)
2021-10-11 17:58:18 +00:00
self.dtB = datetime(2018, 6, 21, 12, tzinfo=gettz("Europe/Berlin"))
2017-07-19 08:05:03 +00:00
def test_now(self):
n = icalevents.icalparser.now()
self.assertTrue(type(n) == datetime, "result of now has type datetime")
self.assertTrue(n.tzinfo, "result of now has a timezone info")
2018-09-16 16:42:32 +00:00
def test_time_left(self):
dt = datetime(year=2017, month=2, day=2, hour=11, minute=2, tzinfo=UTC)
2018-09-16 16:42:32 +00:00
time_left = self.eventA.time_left(time=dt)
self.assertEqual(time_left.days, 1)
self.assertEqual(time_left.seconds, 3780)
2017-07-19 08:05:03 +00:00
def test_event_copy_to(self):
new_start = datetime(year=2017, month=2, day=5, hour=12, minute=5, tzinfo=UTC)
2017-07-19 08:05:03 +00:00
eventC = self.eventA.copy_to(new_start)
new_uid = 1234567890
2017-07-19 08:05:03 +00:00
self.assertNotEqual(eventC.uid, self.eventA.uid, "new event has new UID")
self.assertEqual(eventC.start, new_start, "new event has new start")
2021-10-11 17:58:18 +00:00
self.assertEqual(
eventC.end - eventC.start,
self.eventA.end - self.eventA.start,
"new event has same duration",
)
2018-09-16 16:42:32 +00:00
self.assertEqual(eventC.all_day, False, "new event is no all day event")
2017-07-19 08:05:03 +00:00
self.assertEqual(eventC.summary, self.eventA.summary, "copy to: summary")
2021-10-11 17:58:18 +00:00
self.assertEqual(
eventC.description, self.eventA.description, "copy to: description"
)
2017-07-19 08:05:03 +00:00
eventD = eventC.copy_to(uid=new_uid)
self.assertEqual(eventD.uid, new_uid, "new event has specified UID")
2018-09-16 16:42:32 +00:00
self.assertEqual(eventD.start, eventC.start, "new event has same start")
self.assertEqual(eventD.end, eventC.end, "new event has same end")
2021-10-11 17:58:18 +00:00
self.assertEqual(
eventD.all_day, eventC.all_day, "new event is no all day event"
)
2018-09-16 16:42:32 +00:00
self.assertEqual(eventD.summary, eventC.summary, "copy to: summary")
self.assertEqual(eventD.description, eventC.description, "copy to: description")
2018-09-16 16:42:32 +00:00
2017-07-19 08:05:03 +00:00
def test_event_order(self):
self.assertTrue(self.eventA > self.eventB, "order of events")
2019-02-07 01:53:06 +00:00
def test_attendee(self):
self.assertIsInstance(self.eventA.attendee, str)
self.assertIsInstance(self.eventB.attendee, list)
2020-11-10 17:11:09 +00:00
2019-02-07 01:53:06 +00:00
def test_organizer(self):
self.assertIsInstance(self.eventA.organizer, str)
self.assertIsInstance(self.eventB.organizer, str)
def test_str(self):
self.eventA.start = datetime(year=2017, month=2, day=3, hour=12, minute=5)
self.eventA.end = datetime(year=2017, month=2, day=3, hour=15, minute=5)
self.assertEqual("2017-02-03 12:05:00: Event A (3:00:00)", str(self.eventA))