vdir_test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # ical2vdir - convert .ics file to vdir directory
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import copy
  18. import os
  19. import pathlib
  20. import unittest.mock
  21. import icalendar.cal
  22. import pytest
  23. import ical2vdir
  24. def _normalize_ical(ical: bytes) -> bytes:
  25. return ical.replace(b"\n", b"\r\n")
  26. _SINGLE_EVENT_ICAL = _normalize_ical(
  27. b"""BEGIN:VEVENT
  28. SUMMARY:party
  29. DTSTART:20201024T100000Z
  30. DTEND:20201026T120000Z
  31. DTSTAMP:20200205T160640Z
  32. UID:1qa2ws3ed4rf5tg@google.com
  33. SEQUENCE:0
  34. CREATED:20191231T103841Z
  35. DESCRIPTION:
  36. LAST-MODIFIED:20191231T103841Z
  37. LOCATION:
  38. STATUS:CONFIRMED
  39. TRANSP:OPAQUE
  40. END:VEVENT
  41. """
  42. )
  43. # pylint: disable=protected-access
  44. def test__write_event_cleanup(tmpdir):
  45. event = icalendar.cal.Event.from_ical(_SINGLE_EVENT_ICAL)
  46. with unittest.mock.patch("os.unlink") as unlink_mock:
  47. with pytest.raises(IsADirectoryError):
  48. ical2vdir._write_event(event, pathlib.Path(tmpdir))
  49. unlink_mock.assert_called_once()
  50. unlink_args, _ = unlink_mock.call_args
  51. os.unlink(unlink_args[0])
  52. @pytest.mark.parametrize(
  53. ("event_ical", "expected_filename"),
  54. [
  55. (_SINGLE_EVENT_ICAL, "1qa2ws3ed4rf5tg@google.com.ics",),
  56. (
  57. b"""BEGIN:VEVENT
  58. SUMMARY:work
  59. DTSTART;TZID=Europe/Vienna:20150924T090000
  60. DTEND;TZID=Europe/Vienna:20150924T123000
  61. DTSTAMP:20200205T160640Z
  62. UID:1qa2ws3ed4rf5tg@google.com
  63. RECURRENCE-ID;TZID=Europe/Vienna:20150924T090000
  64. SEQUENCE:5
  65. CREATED:20140228T212925Z
  66. DESCRIPTION:
  67. LAST-MODIFIED:20150908T181423Z
  68. LOCATION:
  69. STATUS:CONFIRMED
  70. TRANSP:TRANSPARENT
  71. END:VEVENT
  72. """,
  73. "1qa2ws3ed4rf5tg@google.com.20150924T090000+0200.ics",
  74. ),
  75. ],
  76. )
  77. def test__event_vdir_filename(event_ical, expected_filename):
  78. event = icalendar.cal.Event.from_ical(event_ical)
  79. assert ical2vdir._event_vdir_filename(event) == expected_filename
  80. @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
  81. def test__sync_event_create(tmpdir, event_ical):
  82. temp_path = pathlib.Path(tmpdir)
  83. event = icalendar.cal.Event.from_ical(event_ical)
  84. ical2vdir._sync_event(event, temp_path)
  85. (ics_path,) = temp_path.iterdir()
  86. assert ics_path.name == "1qa2ws3ed4rf5tg@google.com.ics"
  87. assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL
  88. @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
  89. def test__sync_event_update(tmpdir, event_ical):
  90. temp_path = pathlib.Path(tmpdir)
  91. event = icalendar.cal.Event.from_ical(event_ical)
  92. ical2vdir._sync_event(event, temp_path)
  93. event["SUMMARY"] += " suffix"
  94. ical2vdir._sync_event(event, temp_path)
  95. (ics_path,) = temp_path.iterdir()
  96. assert ics_path.name == event["UID"] + ".ics"
  97. assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL.replace(
  98. b"party", b"party suffix"
  99. )
  100. @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
  101. def test__sync_event_unchanged(tmpdir, event_ical):
  102. temp_path = pathlib.Path(tmpdir)
  103. event = icalendar.cal.Event.from_ical(event_ical)
  104. ical2vdir._sync_event(event, temp_path)
  105. (ics_path,) = temp_path.iterdir()
  106. old_stat = copy.deepcopy(ics_path.stat())
  107. ical2vdir._sync_event(event, temp_path)
  108. assert ics_path.stat() == old_stat
  109. assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL