vdir_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 tempfile
  21. import unittest.mock
  22. import icalendar.cal
  23. import pytest
  24. import ical2vdir
  25. def _normalize_ical(ical: bytes) -> bytes:
  26. return ical.replace(b"\n", b"\r\n")
  27. _SINGLE_EVENT_ICAL = _normalize_ical(
  28. b"""BEGIN:VEVENT
  29. SUMMARY:party
  30. DTSTART:20201024T100000Z
  31. DTEND:20201026T120000Z
  32. DTSTAMP:20200205T160640Z
  33. UID:1qa2ws3ed4rf5tg@google.com
  34. SEQUENCE:0
  35. CREATED:20191231T103841Z
  36. DESCRIPTION:
  37. LAST-MODIFIED:20191231T103841Z
  38. LOCATION:
  39. STATUS:CONFIRMED
  40. TRANSP:OPAQUE
  41. END:VEVENT
  42. """
  43. )
  44. # pylint: disable=protected-access
  45. # tmp_path fixture: https://github.com/pytest-dev/pytest/blob/5.4.3/src/_pytest/tmpdir.py#L191
  46. def test__write_event_cleanup(tmp_path: pathlib.Path) -> None:
  47. event = icalendar.cal.Event.from_ical(_SINGLE_EVENT_ICAL)
  48. with pytest.raises(IsADirectoryError):
  49. ical2vdir._write_event(event, tmp_path)
  50. assert tmp_path.is_dir() # did not overwrite
  51. def test__write_event_move_failed(tmp_path: pathlib.Path) -> None:
  52. event = icalendar.cal.Event.from_ical(_SINGLE_EVENT_ICAL)
  53. output_path = tmp_path.joinpath("test.ics")
  54. with unittest.mock.patch("os.unlink") as unlink_mock, unittest.mock.patch(
  55. "shutil.move", side_effect=Exception("test")
  56. ), pytest.raises(Exception, match=r"^test$"):
  57. ical2vdir._write_event(event, output_path)
  58. assert not output_path.exists()
  59. unlink_mock.assert_called_once() # cleanup temporary file
  60. unlink_args, _ = unlink_mock.call_args
  61. (temp_path,) = unlink_args
  62. assert os.path.dirname(temp_path) == os.path.dirname(tempfile.mkdtemp())
  63. @pytest.mark.parametrize(
  64. ("event_ical", "expected_filename"),
  65. [
  66. (
  67. _SINGLE_EVENT_ICAL,
  68. "1qa2ws3ed4rf5tg@google.com.ics",
  69. ),
  70. (
  71. b"""BEGIN:VEVENT
  72. SUMMARY:work
  73. DTSTART;TZID=Europe/Vienna:20150924T090000
  74. DTEND;TZID=Europe/Vienna:20150924T123000
  75. DTSTAMP:20200205T160640Z
  76. UID:1qa2ws3ed4rf5tg@google.com
  77. RECURRENCE-ID;TZID=Europe/Vienna:20150924T090000
  78. SEQUENCE:5
  79. CREATED:20140228T212925Z
  80. DESCRIPTION:
  81. LAST-MODIFIED:20150908T181423Z
  82. LOCATION:
  83. STATUS:CONFIRMED
  84. TRANSP:TRANSPARENT
  85. END:VEVENT
  86. """,
  87. "1qa2ws3ed4rf5tg@google.com.20150924T090000+0200.ics",
  88. ),
  89. ],
  90. )
  91. def test__event_vdir_filename(event_ical: bytes, expected_filename: str) -> None:
  92. event = icalendar.cal.Event.from_ical(event_ical)
  93. assert ical2vdir._event_vdir_filename(event) == expected_filename
  94. @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
  95. def test__sync_event_create(tmp_path: pathlib.Path, event_ical: bytes) -> None:
  96. event = icalendar.cal.Event.from_ical(event_ical)
  97. ical2vdir._sync_event(event, tmp_path)
  98. (ics_path,) = tmp_path.iterdir()
  99. assert ics_path.name == "1qa2ws3ed4rf5tg@google.com.ics"
  100. assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL
  101. @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
  102. def test__sync_event_update(tmp_path: pathlib.Path, event_ical: bytes) -> None:
  103. event = icalendar.cal.Event.from_ical(event_ical)
  104. ical2vdir._sync_event(event, tmp_path)
  105. event["SUMMARY"] += " suffix"
  106. ical2vdir._sync_event(event, tmp_path)
  107. (ics_path,) = tmp_path.iterdir()
  108. assert ics_path.name == event["UID"] + ".ics"
  109. assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL.replace(
  110. b"party", b"party suffix"
  111. )
  112. @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
  113. def test__sync_event_unchanged(tmp_path: pathlib.Path, event_ical: bytes) -> None:
  114. event = icalendar.cal.Event.from_ical(event_ical)
  115. ical2vdir._sync_event(event, tmp_path)
  116. (ics_path,) = tmp_path.iterdir()
  117. old_stat = copy.deepcopy(ics_path.stat())
  118. ical2vdir._sync_event(event, tmp_path)
  119. assert ics_path.stat() == old_stat
  120. assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL