vdir_test.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 pathlib
  19. import icalendar.cal
  20. import pytest
  21. import ical2vdir
  22. def _normalize_ical(ical: bytes) -> bytes:
  23. return ical.replace(b"\n", b"\r\n")
  24. _SINGLE_EVENT_ICAL = _normalize_ical(
  25. b"""BEGIN:VEVENT
  26. SUMMARY:party
  27. DTSTART:20201024T100000Z
  28. DTEND:20201026T120000Z
  29. DTSTAMP:20200205T160640Z
  30. UID:1qa2ws3ed4rf5tg@google.com
  31. SEQUENCE:0
  32. CREATED:20191231T103841Z
  33. DESCRIPTION:
  34. LAST-MODIFIED:20191231T103841Z
  35. LOCATION:
  36. STATUS:CONFIRMED
  37. TRANSP:OPAQUE
  38. END:VEVENT
  39. """
  40. )
  41. # pylint: disable=protected-access
  42. @pytest.mark.parametrize(
  43. ("event_ical", "expected_filename"),
  44. [
  45. (_SINGLE_EVENT_ICAL, "1qa2ws3ed4rf5tg@google.com.ics",),
  46. (
  47. b"""BEGIN:VEVENT
  48. SUMMARY:work
  49. DTSTART;TZID=Europe/Vienna:20150924T090000
  50. DTEND;TZID=Europe/Vienna:20150924T123000
  51. DTSTAMP:20200205T160640Z
  52. UID:1qa2ws3ed4rf5tg@google.com
  53. RECURRENCE-ID;TZID=Europe/Vienna:20150924T090000
  54. SEQUENCE:5
  55. CREATED:20140228T212925Z
  56. DESCRIPTION:
  57. LAST-MODIFIED:20150908T181423Z
  58. LOCATION:
  59. STATUS:CONFIRMED
  60. TRANSP:TRANSPARENT
  61. END:VEVENT
  62. """,
  63. "1qa2ws3ed4rf5tg@google.com.20150924T090000+0200.ics",
  64. ),
  65. ],
  66. )
  67. def test__event_vdir_filename(event_ical, expected_filename):
  68. event = icalendar.cal.Event.from_ical(event_ical)
  69. assert ical2vdir._event_vdir_filename(event) == expected_filename
  70. @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
  71. def test__export_event_create(tmpdir, event_ical):
  72. temp_path = pathlib.Path(tmpdir)
  73. event = icalendar.cal.Event.from_ical(event_ical)
  74. ical2vdir._export_event(event, temp_path)
  75. (ics_path,) = temp_path.iterdir()
  76. assert ics_path.name == "1qa2ws3ed4rf5tg@google.com.ics"
  77. assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL
  78. @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
  79. def test__export_event_update(tmpdir, event_ical):
  80. temp_path = pathlib.Path(tmpdir)
  81. event = icalendar.cal.Event.from_ical(event_ical)
  82. ical2vdir._export_event(event, temp_path)
  83. event["SUMMARY"] += " suffix"
  84. ical2vdir._export_event(event, temp_path)
  85. (ics_path,) = temp_path.iterdir()
  86. assert ics_path.name == event["UID"] + ".ics"
  87. assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL.replace(
  88. b"party", b"party suffix"
  89. )
  90. @pytest.mark.parametrize("event_ical", [_SINGLE_EVENT_ICAL])
  91. def test__export_event_unchanged(tmpdir, event_ical):
  92. temp_path = pathlib.Path(tmpdir)
  93. event = icalendar.cal.Event.from_ical(event_ical)
  94. ical2vdir._export_event(event, temp_path)
  95. (ics_path,) = temp_path.iterdir()
  96. old_stat = copy.deepcopy(ics_path.stat())
  97. ical2vdir._export_event(event, temp_path)
  98. assert ics_path.stat() == old_stat
  99. assert ics_path.read_bytes() == _SINGLE_EVENT_ICAL