File: test_calendar.py

package info (click to toggle)
python-ical 9.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,448 kB
  • sloc: python: 13,877; sh: 9; makefile: 5
file content (460 lines) | stat: -rw-r--r-- 14,364 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
"""Tests for timeline related calendar eents."""
from __future__ import annotations

from collections.abc import Generator
import datetime
import re
import uuid
import zoneinfo
from unittest.mock import patch

import pytest
from freezegun import freeze_time

from ical.calendar import Calendar
from ical.calendar_stream import IcsCalendarStream
from ical.event import Event
from ical.types.recur import Recur


@pytest.fixture(name="calendar")
def mock_calendar() -> Calendar:
    """Fixture calendar with all day events to use in tests."""
    cal = Calendar()
    cal.events.extend(
        [
            Event(
                summary="second",
                start=datetime.date(2000, 2, 1),
                end=datetime.date(2000, 2, 2),
            ),
            Event(
                summary="fourth",
                start=datetime.date(2000, 4, 1),
                end=datetime.date(2000, 4, 2),
            ),
            Event(
                summary="third",
                start=datetime.date(2000, 3, 1),
                end=datetime.date(2000, 3, 2),
            ),
            Event(
                summary="first",
                start=datetime.date(2000, 1, 1),
                end=datetime.date(2000, 1, 2),
            ),
        ]
    )
    return cal


@pytest.fixture(name="calendar_times")
def mock_calendar_times() -> Calendar:
    """Fixture calendar with datetime based events to use in tests."""
    cal = Calendar()
    cal.events.extend(
        [
            Event(
                summary="first",
                start=datetime.datetime(2000, 1, 1, 11, 0),
                end=datetime.datetime(2000, 1, 1, 11, 30),
            ),
            Event(
                summary="second",
                start=datetime.datetime(2000, 1, 1, 12, 0),
                end=datetime.datetime(2000, 1, 1, 13, 0),
            ),
            Event(
                summary="third",
                start=datetime.datetime(2000, 1, 2, 12, 0),
                end=datetime.datetime(2000, 1, 2, 13, 0),
            ),
        ]
    )
    return cal


@pytest.fixture(name="calendar_mixed")
def mock_calendar_mixed() -> Calendar:
    """Fixture with a mix of all day and datetime based events."""
    cal = Calendar()
    cal.events.extend(
        [
            Event(
                summary="All Day",
                start=datetime.date(2000, 2, 1),
                end=datetime.date(2000, 2, 2),
            ),
            Event(
                summary="Event @ 7 UTC",
                start=datetime.datetime(
                    2000, 2, 1, 7, 00, 0, tzinfo=datetime.timezone.utc
                ),
                end=datetime.datetime(
                    2000, 2, 2, 7, 00, 0, tzinfo=datetime.timezone.utc
                ),
            ),
            Event(
                summary="Event @ 5UTC",
                start=datetime.datetime(
                    2000, 2, 1, 5, 00, 0, tzinfo=datetime.timezone.utc
                ),
                end=datetime.datetime(
                    2000, 2, 2, 5, 00, 0, tzinfo=datetime.timezone.utc
                ),
            ),
        ]
    )
    return cal


def test_iteration(calendar: Calendar) -> None:
    """Test chronological iteration of a timeline."""
    assert [e.summary for e in calendar.timeline] == [
        "first",
        "second",
        "third",
        "fourth",
    ]


@pytest.mark.parametrize(
    "when,expected_events",
    [
        (datetime.date(2000, 1, 1), ["first"]),
        (datetime.date(2000, 2, 1), ["second"]),
        (datetime.date(2000, 3, 1), ["third"]),
    ],
)
def test_on_date(
    calendar: Calendar, when: datetime.date, expected_events: list[str]
) -> None:
    """Test returning events on a particular day."""
    assert [e.summary for e in calendar.timeline.on_date(when)] == expected_events


def test_start_after(calendar: Calendar) -> None:
    """Test chronological iteration starting at a specific time."""
    assert [
        e.summary for e in calendar.timeline.start_after(datetime.date(2000, 1, 1))
    ] == ["second", "third", "fourth"]
    assert [
        e.summary
        for e in calendar.timeline.start_after(datetime.datetime(2000, 1, 1, 6, 0, 0))
    ] == ["second", "third", "fourth"]
    assert [
        e.summary
        for e in calendar.timeline.start_after(datetime.datetime(2000, 1, 15, 0, 0, 0))
    ] == ["second", "third", "fourth"]


def test_start_after_times(calendar_times: Calendar) -> None:
    """Test chronological iteration starting at a specific time."""
    assert [
        e.summary
        for e in calendar_times.timeline.start_after(datetime.date(2000, 1, 1))
    ] == ["first", "second", "third"]
    assert [
        e.summary
        for e in calendar_times.timeline.start_after(
            datetime.datetime(2000, 1, 1, 6, 0, 0)
        )
    ] == ["first", "second", "third"]


def test_active_after(calendar: Calendar) -> None:
    """Test chronological iteration starting at a specific time."""
    assert [
        e.summary
        for e in calendar.timeline.start_after(datetime.datetime(2000, 1, 1, 12, 0, 0))
    ] == ["second", "third", "fourth"]
    assert [
        e.summary for e in calendar.timeline.start_after(datetime.date(2000, 1, 1))
    ] == ["second", "third", "fourth"]


def test_active_after_times(calendar_times: Calendar) -> None:
    """Test chronological iteration starting at a specific time."""
    assert [
        e.summary
        for e in calendar_times.timeline.start_after(
            datetime.datetime(2000, 1, 1, 12, 0, 0)
        )
    ] == ["third"]
    assert [
        e.summary
        for e in calendar_times.timeline.start_after(datetime.date(2000, 1, 1))
    ] == ["first", "second", "third"]


@pytest.mark.parametrize(
    "at_datetime,expected_events",
    [
        (datetime.datetime(2000, 1, 1, 11, 15), ["first"]),
        (datetime.datetime(2000, 1, 1, 11, 59), []),
        (datetime.datetime(2000, 1, 1, 12, 0), ["second"]),
        (datetime.datetime(2000, 1, 1, 12, 30), ["second"]),
        (datetime.datetime(2000, 1, 1, 12, 59), ["second"]),
        (datetime.datetime(2000, 1, 1, 13, 0), []),
    ],
)
def test_at_instant(
    calendar_times: Calendar, at_datetime: datetime.datetime, expected_events: list[str]
) -> None:
    """Test returning events at a specific time."""
    assert [
        e.summary for e in calendar_times.timeline.at_instant(at_datetime)
    ] == expected_events


@freeze_time("2000-01-01 12:30:00")
def test_now(calendar_times: Calendar) -> None:
    """Test events happening at the current time."""
    assert [e.summary for e in calendar_times.timeline.now()] == ["second"]


@freeze_time("2000-01-01 13:00:00")
def test_now_no_match(calendar_times: Calendar) -> None:
    """Test no events happening at the current time."""
    assert [e.summary for e in calendar_times.timeline.now()] == []


@freeze_time("2000-01-01 12:30:00")
def test_today(calendar_times: Calendar) -> None:
    """Test events active today."""
    assert [e.summary for e in calendar_times.timeline.today()] == ["first", "second"]


@pytest.mark.parametrize(
    "start,end,expected_events",
    [
        (
            datetime.datetime(2000, 1, 1, 10, 00),
            datetime.datetime(2000, 1, 2, 14, 00),
            ["first", "second", "third"],
        ),
        (
            datetime.datetime(2000, 1, 1, 10, 00),
            datetime.datetime(2000, 1, 1, 14, 00),
            ["first", "second"],
        ),
        (
            datetime.datetime(2000, 1, 1, 12, 00),
            datetime.datetime(2000, 1, 2, 14, 00),
            ["second", "third"],
        ),
        (
            datetime.datetime(2000, 1, 1, 12, 00),
            datetime.datetime(2000, 1, 1, 14, 00),
            ["second"],
        ),
    ],
)
def test_included(
    calendar_times: Calendar,
    start: datetime.datetime,
    end: datetime.datetime,
    expected_events: list[str],
) -> None:
    """Test calendar timeline inclusions."""
    assert [
        e.summary for e in calendar_times.timeline.included(start, end)
    ] == expected_events


def test_multiple_calendars(calendar: Calendar, calendar_times: Calendar) -> None:
    """Test multiple calendars have independent event lists."""
    assert len(calendar.events) == 4
    assert len(calendar_times.events) == 3
    assert len(Calendar().events) == 0


def test_multiple_iteration(calendar: Calendar) -> None:
    """Test iterating over a timeline multiple times."""
    line = calendar.timeline
    assert [e.summary for e in line] == [
        "first",
        "second",
        "third",
        "fourth",
    ]
    assert [e.summary for e in line] == [
        "first",
        "second",
        "third",
        "fourth",
    ]


TEST_ICS = """BEGIN:VCALENDAR
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
VERSION:2.0
BEGIN:VEVENT
DTSTAMP:19970610T172345Z
UID:19970610T172345Z-AF23B2@example.com
DTSTART:19970714T170000Z
DTEND:19970715T040000Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR"""


def test_calendar_serialization() -> None:
    """Test single calendar serialization."""
    calendar = IcsCalendarStream.calendar_from_ics(TEST_ICS)
    assert len(calendar.events) == 1
    assert calendar.events[0].summary == "Bastille Day Party"
    output_ics = IcsCalendarStream.calendar_to_ics(calendar)
    assert output_ics == TEST_ICS


def test_empty_calendar() -> None:
    """Test reading an empty calendar file."""
    calendar = IcsCalendarStream.calendar_from_ics("")
    assert len(calendar.events) == 0


@pytest.fixture(name="_uid")
def mock_uid() -> Generator[str, None, None]:
    """Patch out uuid creation with a fixed value."""
    value = str(uuid.uuid3(uuid.NAMESPACE_DNS, "fixed-name"))
    with patch(
        "ical.event.uid_factory",
        return_value=value,
    ):
        yield value


@freeze_time("2000-01-01 12:30:00")
def test_create_and_serialize_calendar(
    _uid: str,
    mock_prodid: Generator[None, None, None],
) -> None:
    """Test creating a calendar manually then serializing."""
    cal = Calendar()
    cal.events.append(
        Event(
            summary="Event",
            start=datetime.date(2000, 2, 1),
            end=datetime.date(2000, 2, 2),
        )
    )
    ics = IcsCalendarStream.calendar_to_ics(cal)
    assert re.split("\r?\n", ics) == [
        "BEGIN:VCALENDAR",
        "PRODID:-//example//1.2.3",
        "VERSION:2.0",
        "BEGIN:VEVENT",
        "DTSTAMP:20000101T123000Z",
        "UID:68e9e07c-7557-36e2-91c1-8febe7527841",
        "DTSTART:20000201",
        "DTEND:20000202",
        "SUMMARY:Event",
        "END:VEVENT",
        "END:VCALENDAR",
    ]


def test_mixed_iteration_order(calendar_mixed: Calendar) -> None:
    """Test iteration order of all day events based on the attendee timezone."""

    # UTC order
    assert [e.summary for e in calendar_mixed.timeline_tz(datetime.timezone.utc)] == [
        "All Day",
        "Event @ 5UTC",
        "Event @ 7 UTC",
    ]

    # Attendee is in -6
    assert [
        e.summary
        for e in calendar_mixed.timeline_tz(zoneinfo.ZoneInfo("America/Regina"))
    ] == ["Event @ 5UTC", "All Day", "Event @ 7 UTC"]

    # Attendee is in -8
    assert [
        e.summary
        for e in calendar_mixed.timeline_tz(zoneinfo.ZoneInfo("America/Los_Angeles"))
    ] == ["Event @ 5UTC", "Event @ 7 UTC", "All Day"]


@pytest.mark.parametrize(
    "tzname,dt_before,dt_after",
    [
        (
            "America/Los_Angeles",  # UTC-8 in Feb
            datetime.datetime(2000, 2, 1, 7, 59, 59, tzinfo=datetime.timezone.utc),
            datetime.datetime(2000, 2, 1, 8, 0, 0, tzinfo=datetime.timezone.utc),
        ),
        (
            "America/Regina",  # UTC-6 all year round
            datetime.datetime(2000, 2, 1, 5, 59, 59, tzinfo=datetime.timezone.utc),
            datetime.datetime(2000, 2, 1, 6, 0, 0, tzinfo=datetime.timezone.utc),
        ),
        (
            "CET",  # UTC-1 in Feb
            datetime.datetime(2000, 1, 31, 22, 59, 59, tzinfo=datetime.timezone.utc),
            datetime.datetime(2000, 1, 31, 23, 0, 0, tzinfo=datetime.timezone.utc),
        ),
    ],
)
def test_all_day_with_local_timezone(
    tzname: str, dt_before: datetime.datetime, dt_after: datetime.datetime
) -> None:
    """Test iteration of all day events using local timezone override."""
    cal = Calendar()
    cal.events.extend(
        [
            Event(
                summary="event",
                start=datetime.date(2000, 2, 1),
                end=datetime.date(2000, 2, 2),
            ),
        ]
    )

    local_tz = zoneinfo.ZoneInfo(tzname)

    def start_after(dtstart: datetime.datetime) -> list[str]:
        nonlocal cal, local_tz
        return [e.summary for e in cal.timeline_tz(local_tz).start_after(dtstart)]

    local_before = dt_before.astimezone(local_tz)
    assert start_after(local_before) == ["event"]

    local_after = dt_after.astimezone(local_tz)
    assert not start_after(local_after)


@freeze_time(datetime.datetime(2023, 10, 25, 12, 0, 0, tzinfo=datetime.timezone.utc))
def test_floating_time_with_timezone_propagation() -> None:
    """Test iteration of floating time events ensuring timezones are respected."""
    cal = Calendar()
    cal.events.extend(
        [
            Event(
                summary="Event 1",
                start=datetime.datetime(2023, 10, 25, 6, 40),
                end=datetime.datetime(2023, 10, 25, 6, 50),
                rrule=Recur.from_rrule("FREQ=WEEKLY;BYDAY=WE,MO,TU,TH,FR"),
            ),
            Event(
                summary="Event 2",
                start=datetime.datetime(2023, 10, 25, 8, 30),
                end=datetime.datetime(2023, 10, 25, 8, 40),
                rrule=Recur.from_rrule("FREQ=DAILY"),
            ),
            Event(
                summary="Event 3",
                start=datetime.datetime(2023, 10, 25, 18, 30),
                end=datetime.datetime(2023, 10, 25, 18, 40),
                rrule=Recur.from_rrule("FREQ=DAILY"),
            ),
        ]
    )
    # Ensure there are no calls to fetch the local timezone, only using the provided
    # timezone information.
    with patch("ical.util.local_timezone", side_effect=ValueError("do not invoke")):
        it = iter(cal.timeline_tz(zoneinfo.ZoneInfo("Europe/Brussels")))
        for i in range(0, 30):
            next(it)