File: test_recurrence.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 (272 lines) | stat: -rw-r--r-- 8,110 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
"""Tests for timeline related calendar eents."""

from __future__ import annotations

import datetime
import zoneinfo

import pytest
from ical.exceptions import CalendarParseError

from ical.calendar import Calendar
from ical.component import ComponentModel
from ical.exceptions import RecurrenceError
from ical.event import Event
from ical.parsing.property import ParsedProperty, ParsedPropertyParameter
from ical.parsing.component import parse_content
from ical.timeline import Timeline
from ical.todo import Todo
from ical.types.recur import Frequency, Recur, RecurrenceId, Weekday, WeekdayValue
from ical.recurrence import Recurrences


def test_from_contentlines() -> None:
    """Test parsing a recurrence rule from a string."""
    recurrences = Recurrences.from_basic_contentlines(
        [
            "DTSTART;TZID=America/New_York:20220802T060000",
            "RRULE:FREQ=DAILY;COUNT=3",
        ]
    )
    assert recurrences.rrule == [
        Recur(
            freq=Frequency.DAILY,
            count=3,
        )
    ]
    assert recurrences.dtstart == datetime.datetime(
        2022, 8, 2, 6, 0, 0, tzinfo=zoneinfo.ZoneInfo("America/New_York")
    )


def test_from_contentlines_rdate() -> None:
    """Test parsing a recurrence rule with RDATE from a string."""

    lines = [
        "RRULE:FREQ=DAILY;COUNT=3",
        "RDATE;VALUE=DATE:20220803,20220804",
        "IGNORED:20250806",
    ]

    # parse using full ical parser
    content = [
        "BEGIN:RECURRENCE",
        *lines,
        "END:RECURRENCE",
    ]
    component = parse_content("\n".join(content))
    assert component
    orig_recurrences = Recurrences.parse_obj(component[0].as_dict())
    recurrences = Recurrences.from_basic_contentlines(lines)
    assert recurrences.rrule == [
        Recur(
            freq=Frequency.DAILY,
            count=3,
        )
    ]
    assert recurrences.rdate == [
        datetime.date(2022, 8, 3),
        datetime.date(2022, 8, 4),
    ]


@pytest.mark.parametrize("property", ["RDATE", "EXDATE"])
@pytest.mark.parametrize(
    ("date_value", "expected"),
    [
        ("{property}:20220803T060000", [datetime.datetime(2022, 8, 3, 6, 0, 0)]),
        (
            "{property}:20220803T060000,20220804T060000",
            [
                datetime.datetime(2022, 8, 3, 6, 0, 0),
                datetime.datetime(2022, 8, 4, 6, 0, 0),
            ],
        ),
        ("{property}:20220803", [datetime.date(2022, 8, 3)]),
        (
            "{property}:20220803,20220804",
            [datetime.date(2022, 8, 3), datetime.date(2022, 8, 4)],
        ),
        (
            "{property};VALUE=DATE:20220803,20220804",
            [datetime.date(2022, 8, 3), datetime.date(2022, 8, 4)],
        ),
        (
            "{property};VALUE=DATE-TIME:20220803T060000,20220804T060000",
            [
                datetime.datetime(2022, 8, 3, 6, 0, 0),
                datetime.datetime(2022, 8, 4, 6, 0, 0),
            ],
        ),
        (
            "{property}:20220803T060000Z,20220804T060000Z",
            [
                datetime.datetime(2022, 8, 3, 6, 0, 0, tzinfo=datetime.UTC),
                datetime.datetime(2022, 8, 4, 6, 0, 0, tzinfo=datetime.UTC),
            ],
        ),
        (
            "{property};TZID=America/New_York:19980119T020000",
            [
                datetime.datetime(
                    1998, 1, 19, 2, 0, 0, tzinfo=zoneinfo.ZoneInfo("America/New_York")
                )
            ],
        ),
    ],
)
def test_from_contentlines_date_values(
    property: str, date_value: str, expected: list[datetime.datetime | datetime.date]
) -> None:
    """Test parsing a recurrence rule with RDATE from a string."""
    lines = [
        "RRULE:FREQ=DAILY;COUNT=3",
        date_value.format(property=property),
    ]

    # Parse using full ical parser with a fake component
    content = [
        "BEGIN:RECURRENCE",
        *lines,
        "END:RECURRENCE",
    ]
    # assert content == 'a'
    component = parse_content("\n".join(content))
    assert component
    orig_recurrences = Recurrences.parse_obj(component[0].as_dict())

    # Parse using optimized parser
    recurrences = Recurrences.from_basic_contentlines(lines)

    # Compare both approaches
    assert orig_recurrences == recurrences

    # Additionally assert expected values from test parameters
    assert recurrences.rrule == [
        Recur(
            freq=Frequency.DAILY,
            count=3,
        )
    ]
    attr = property.lower()
    assert getattr(recurrences, attr) == expected


@pytest.mark.parametrize(
    "contentlines",
    [
        ["RRULE;COUNT=3"],
        ["RRULE:COUNT=3;FREQ=invalid"],
        ["EXDATE"],
        ["RDATE"],
        ["RRULE;COUNT=3", "EXDATE"],
        ["EXDATE", "RDATE"],
        ["EXDATE:20220803T060000", "RDATE:"],
    ],
)
def test_from_invalid_contentlines(contentlines: list[str]) -> None:
    """Test parsing content lines that are not valid."""
    with pytest.raises(CalendarParseError):
        Recurrences.from_basic_contentlines(contentlines)


def test_as_rrule() -> None:
    """Test parsing a recurrence rule from a string."""
    recurrences = Recurrences.from_basic_contentlines(
        [
            "DTSTART:20220802T060000Z",
            "RRULE:FREQ=DAILY;COUNT=3",
            "EXDATE:20220803T060000Z",
        ]
    )
    assert list(recurrences.as_rrule()) == [
        datetime.datetime(2022, 8, 2, 6, 0, 0, tzinfo=datetime.UTC),
        datetime.datetime(2022, 8, 4, 6, 0, 0, tzinfo=datetime.UTC),
    ]


def test_as_rrule_with_rdate() -> None:
    """Test parsing a recurrence rule from a string."""
    recurrences = Recurrences.from_basic_contentlines(
        [
            "DTSTART:20220801",
            "RDATE:20220803",
            "RDATE:20220804",
            "RDATE:20220805",
        ]
    )
    assert list(recurrences.as_rrule()) == [
        datetime.date(2022, 8, 3),
        datetime.date(2022, 8, 4),
        datetime.date(2022, 8, 5),
    ]


def test_as_rrule_with_date() -> None:
    """Test parsing a recurrence rule from a string."""
    recurrences = Recurrences.from_basic_contentlines(
        [
            "RRULE:FREQ=DAILY;COUNT=3",
            "EXDATE:20220803T060000Z",
        ]
    )
    assert list(recurrences.as_rrule(datetime.datetime(2022, 8, 2, 6, 0, 0, tzinfo=datetime.UTC))) == [
        datetime.datetime(2022, 8, 2, 6, 0, 0, tzinfo=datetime.UTC),
        datetime.datetime(2022, 8, 4, 6, 0, 0, tzinfo=datetime.UTC),
    ]


def test_as_rrule_without_date() -> None:
    """Test parsing a recurrence rule from a string."""
    recurrences = Recurrences.from_basic_contentlines(
        [
            "RRULE:FREQ=DAILY;COUNT=3",
            "EXDATE:20220803T060000Z",
        ]
    )
    with pytest.raises(ValueError, match="dtstart is required"):
        list(recurrences.as_rrule())


def test_rrule_failure() -> None:
    """Test parsing a recurrence rule from a string."""
    recurrences = Recurrences.from_basic_contentlines(
        [
            "DTSTART:20220802T060000Z",
            "RRULE:FREQ=DAILY;COUNT=3",
            "EXDATE:20220803T060000",
        ]
    )
    with pytest.raises(RecurrenceError, match="can't compare offset-naive"):
        list(recurrences.as_rrule())


def test_ics() -> None:
    """Test parsing a recurrence rule from a string."""
    recurrences = Recurrences.from_basic_contentlines(
        [
            "DTSTART:20220802T060000Z",
            "RRULE:FREQ=DAILY;COUNT=3",
            "EXDATE:20220803T060000Z",
        ]
    )
    assert recurrences.ics() == [
        "DTSTART:20220802T060000Z",
        "RRULE:FREQ=DAILY;COUNT=3",
        "EXDATE:20220803T060000Z",
    ]



def test_mismatch_date_and_datetime_types() -> None:
    """Test parsing a recurrence rule from a string."""
    recurrences = Recurrences.from_basic_contentlines(
        [
            "DTSTART:20220801T060000Z",
            "RDATE:20220803",
            "RDATE:20220804T060000Z",
            "RDATE:20220805",
        ]
    )
    with pytest.raises(RecurrenceError):
        list(recurrences.as_rrule())