test_ical_manager.py (2778B)
1 import unittest 2 import os 3 import tempfile 4 from datetime import datetime, timezone 5 from icalendar import Event 6 from ungana.ical.ical_manager import ICalManager 7 8 class TestICalManager(unittest.TestCase): 9 10 def setUp(self): 11 self.manager = ICalManager() 12 self.compulsory_event_fields = { 13 "start": datetime(2025, 9, 6, 10, 0, tzinfo=timezone.utc), 14 "summary": "Gophers Meetup", 15 "location": "Gopher Confrence Hall", 16 "description": "Gophers yearly meetup", 17 "organizer": "mailto:events@gophers.com", 18 "uid": "test-uid-123" 19 } 20 21 def test_create_event_basic(self): 22 event = self.manager.create_event(self.compulsory_event_fields) 23 self.assertIsInstance(event, Event) 24 self.assertEqual(str(event["summary"]), "Gophers Meetup") 25 self.assertEqual(str(event["organizer"]), "mailto:events@gophers.com") 26 self.assertEqual(str(event["uid"]), "test-uid-123") 27 28 def test_create_event_with_duration(self): 29 data = self.compulsory_event_fields.copy() 30 data["duration"] = "PT1H" # 1 hour ISO duration 31 event = self.manager.create_event(data) 32 self.assertIn("duration", event) 33 34 def test_create_event_with_contact(self): 35 data = self.compulsory_event_fields.copy() 36 data["contact"] = "Foo bar" 37 event = self.manager.create_event(data) 38 self.assertIn("contact", event) 39 self.assertEqual(str(event["contact"]), "Foo bar") 40 41 def test_create_event_with_attachments(self): 42 data = self.compulsory_event_fields.copy() 43 data["attachments"] = [ 44 "http://foo.com/foo.pdf", 45 ("ATTACH", "http://foo.com/foo.pdf", {"FMTTYPE": "application/pdf"}) 46 ] 47 event = self.manager.create_event(data) 48 self.assertIn("ATTACH", event) 49 50 def test_load_nonexistent_file_returns_new_calendar(self): 51 cal = self.manager.load_ical_file("does_not_exist.ics") 52 self.assertEqual(cal["VERSION"], "2.0") 53 self.assertEqual(cal["PRODID"], "-//Ungana//mxm.dk//") 54 55 def test_save_and_load_event(self): 56 event = self.manager.create_event(self.compulsory_event_fields) 57 with tempfile.TemporaryDirectory() as tmpdir: 58 filepath = os.path.join(tmpdir, "xyz.ics") 59 self.manager.save_ical_file(event, filepath) 60 61 # Verify file exists 62 self.assertTrue(os.path.exists(filepath)) 63 64 # Reload calendar 65 cal = self.manager.load_ical_file(filepath) 66 events = [c for c in cal.walk() if c.name == "VEVENT"] 67 self.assertEqual(len(events), 1) 68 self.assertEqual(str(events[0]["summary"]), "Gophers Meetup") 69 70 71 if __name__ == "__main__": 72 unittest.main()