Browse Source

added tests for _export_event

Fabian Peter Hammerle 4 years ago
parent
commit
4b0418fa4c
1 changed files with 55 additions and 9 deletions
  1. 55 9
      tests/vdir_test.py

+ 55 - 9
tests/vdir_test.py

@@ -1,14 +1,18 @@
+import copy
+import pathlib
+
 import icalendar.cal
 import pytest
 
 import ics2vdir
 
 
-@pytest.mark.parametrize(
-    ("event_ical", "expected_filename"),
-    [
-        (
-            b"""BEGIN:VEVENT
+def _normalize_ical(ical: bytes) -> bytes:
+    return ical.replace(b"\n", b"\r\n")
+
+
+_SINGLE_EVENT_ICAL = _normalize_ical(
+    b"""BEGIN:VEVENT
 SUMMARY:party
 DTSTART:20201024T100000Z
 DTEND:20201026T120000Z
@@ -22,9 +26,16 @@ LOCATION:
 STATUS:CONFIRMED
 TRANSP:OPAQUE
 END:VEVENT
-""",
-            "1qa2ws3ed4rf5tg@google.com.ics",
-        ),
+"""
+)
+
+# pylint: disable=protected-access
+
+
+@pytest.mark.parametrize(
+    ("event_ical", "expected_filename"),
+    [
+        (_SINGLE_EVENT_ICAL, "1qa2ws3ed4rf5tg@google.com.ics",),
         (
             b"""BEGIN:VEVENT
 SUMMARY:work
@@ -48,5 +59,40 @@ END:VEVENT
 )
 def test__event_vdir_filename(event_ical, expected_filename):
     event = icalendar.cal.Event.from_ical(event_ical)
-    # pylint: disable=protected-access
     assert ics2vdir._event_vdir_filename(event) == expected_filename
+
+
+@pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
+def test__export_event_create(tmpdir, event_ical):
+    temp_path = pathlib.Path(tmpdir)
+    event = icalendar.cal.Event.from_ical(event_ical)
+    ics2vdir._export_event(event, temp_path)
+    (ics_path,) = temp_path.iterdir()
+    assert ics_path.name == "1qa2ws3ed4rf5tg@google.com.ics"
+    assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL
+
+
+@pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
+def test__export_event_update(tmpdir, event_ical):
+    temp_path = pathlib.Path(tmpdir)
+    event = icalendar.cal.Event.from_ical(event_ical)
+    ics2vdir._export_event(event, temp_path)
+    event["SUMMARY"] += " suffix"
+    ics2vdir._export_event(event, temp_path)
+    (ics_path,) = temp_path.iterdir()
+    assert ics_path.name == event["UID"] + ".ics"
+    assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL.replace(
+        b"party", b"party suffix"
+    )
+
+
+@pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
+def test__export_event_unchanged(tmpdir, event_ical):
+    temp_path = pathlib.Path(tmpdir)
+    event = icalendar.cal.Event.from_ical(event_ical)
+    ics2vdir._export_event(event, temp_path)
+    (ics_path,) = temp_path.iterdir()
+    old_stat = copy.deepcopy(ics_path.stat())
+    ics2vdir._export_event(event, temp_path)
+    assert ics_path.stat() == old_stat
+    assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL