Debian Codegraph @

recurring_ical_events/test/test_occurrence.py

raw symbols SCIP
1 """The all() function can be exposed now that we have the after() function.
2
3 all() becomes a Generator.
4 We do not wish to retain a high memory footprint on the occurrences.
5 """
6
7 from __future__ import annotations
8
9 from datetime import date, datetime
10 from typing import TYPE_CHECKING, NamedTuple
11
12 import pytest
13
14 try:
15 from zoneinfo import ZoneInfo
16 except ImportError:
17 from backports.zoneinfo import ZoneInfo
18
19 from recurring_ical_events.occurrence import Occurrence, OccurrenceID
20
21 if TYPE_CHECKING:
22 from recurring_ical_events.types import RecurrenceIDs, Time
23
24
25 class Adapter(NamedTuple):
26 uid: str
27 name: str
28 recurrence_ids: RecurrenceIDs
29 start: Time
30 end: Time = date(2020, 10, 10)
31
32 def component_name(self):
33 return self.name
34
35
36 def occurrence_id(adapter: Adapter):
37 """Check the id of the occurrence."""
38 return Occurrence(adapter).id
39
40
41 def occurrence_id_string(adapter: Adapter):
42 """Check the string of the occurrence id."""
43 return Occurrence(adapter).id.to_string()
44
45
46 def occurrence_id_string_parsed(adapter: Adapter):
47 """Check the string of the occurrence id."""
48 return OccurrenceID.from_string(Occurrence(adapter).id.to_string())
49
50
51 @pytest.mark.parametrize(
52 ("adapter1", "adapter2", "equality", "message"),
53 [
54 (
55 Adapter("asd", "asd", (date(2020, 10, 2),), date(2020, 10, 2)),
56 Adapter("asd", "asd", (date(2020, 10, 2),), date(2020, 10, 2)),
57 True,
58 "same copy",
59 ),
60 (
61 Adapter("asd1", "asd", (date(2020, 10, 2),), date(2020, 10, 2)),
62 Adapter("asd", "asd", (date(2020, 10, 2),), date(2020, 10, 2)),
63 False,
64 "different name",
65 ),
66 (
67 Adapter("asd", "asd", (date(2020, 10, 2),), date(2020, 10, 2)),
68 Adapter("asd", "asd1", (date(2020, 10, 2),), date(2020, 10, 2)),
69 False,
70 "different uid",
71 ),
72 (
73 Adapter("asd", "asd", (date(2020, 10, 1),), date(2020, 10, 2)),
74 Adapter("asd", "asd", (date(2020, 10, 2),), date(2020, 10, 2)),
75 False,
76 "different recurrence id",
77 ),
78 ## I cannot think of a possible example where the recurrence id is the same but the
79 ## start differs - especially after the occurrences are calculated.
80 # (
81 # Adapter("asd", "asd", (date(2020, 10, 2),), date(2020, 10, 1)),
82 # Adapter("asd", "asd", (date(2020, 10, 2),), date(2020, 10, 2)),
83 # True,
84 # "different start but recurrence id is the same",
85 # ),
86 (
87 Adapter("asd", "asd", (), date(2020, 10, 2)),
88 Adapter("asd", "asd", (), date(2020, 10, 2)),
89 True,
90 "no recurrence id but copy",
91 ),
92 (
93 Adapter("asd", "asd", (), date(2020, 10, 2)),
94 Adapter("asd", "asd", (), date(2020, 10, 1)),
95 False,
96 "no recurrence id but start differs",
97 ),
98 (
99 Adapter("VEVENT", "uid", (), datetime(2020, 10, 2, 10)),
100 Adapter("VEVENT", "uid", (), datetime(2020, 10, 2, 10)),
101 True,
102 "datetime",
103 ),
104 (
105 Adapter(
106 "VEVENT", "uid", (), datetime(2020, 10, 2, 10, tzinfo=ZoneInfo("UTC"))
107 ),
108 Adapter(
109 "VEVENT", "uid", (), datetime(2020, 10, 2, 10, tzinfo=ZoneInfo("UTC"))
110 ),
111 True,
112 "UTC date",
113 ),
114 (
115 Adapter(
116 "VEVENT",
117 "uid",
118 (),
119 datetime(2020, 10, 2, 10, tzinfo=ZoneInfo("Europe/Berlin")),
120 ),
121 Adapter(
122 "VEVENT",
123 "uid",
124 (),
125 datetime(2020, 10, 2, 10, tzinfo=ZoneInfo("Europe/Berlin")),
126 ),
127 True,
128 "Europe/Berlin date",
129 ),
130 (
131 Adapter(
132 "VEVENT", "uid", (), datetime(2020, 10, 2, 10, tzinfo=ZoneInfo("UTC"))
133 ),
134 Adapter("VEVENT", "uid", (), datetime(2020, 10, 2, 10)),
135 False,
136 "timezone differs - UTC",
137 ),
138 (
139 Adapter(
140 "VEVENT",
141 "uid",
142 (),
143 datetime(2020, 10, 2, 10, tzinfo=ZoneInfo("Europe/Berlin")),
144 ),
145 Adapter("VEVENT", "uid", (), datetime(2020, 10, 2, 10)),
146 False,
147 "timezone differs - Europe/Berlin",
148 ),
149 (
150 Adapter(
151 "VEVENT",
152 "uid",
153 (),
154 datetime(2020, 10, 2, 10, tzinfo=ZoneInfo("Europe/Berlin")),
155 ),
156 Adapter(
157 "VEVENT", "uid", (), datetime(2020, 10, 2, 10, tzinfo=ZoneInfo("UTC"))
158 ),
159 False,
160 "timezone differs - Europe/Berlin + UTC",
161 ),
162 ],
163 )
164 @pytest.mark.parametrize(
165 "create_occurrence",
166 [Occurrence, occurrence_id, occurrence_id_string, occurrence_id_string_parsed],
167 )
168 def test_equality(adapter1, adapter2, equality, message, create_occurrence):
169 """Check the equality of Occurrences."""
170 occurrence1 = create_occurrence(adapter1)
171 occurrence2 = create_occurrence(adapter2)
172 assert (occurrence1 == occurrence2) == equality, "1 == 2 - " + message
173 assert (occurrence1 != occurrence2) != equality, "1 != 2 - " + message
174 assert (occurrence2 == occurrence1) == equality, "2 == 1 - " + message
175 assert (occurrence2 != occurrence1) != equality, "2 != 1 - " + message
176 assert (hash(occurrence1) == hash(occurrence2)) == equality, (
177 "hash1 == hash2 - " + message
178 )
179 assert (hash(occurrence1) != hash(occurrence2)) != equality, (
180 "hash1 != hash2 - " + message
181 )
182
183
184 ALL_CHARS = "".join(map(chr, range(256)))
185
186
187 @pytest.mark.parametrize(
188 ("original_component_id", "message"),
189 [
190 (
191 OccurrenceID("VEVENT", "awdd", date(1970, 2, 12), date(1971, 2, 12)),
192 "date and all values given",
193 ),
194 (
195 OccurrenceID(
196 "VTODO", "+-2123_", datetime(1970, 2, 12), datetime(1971, 2, 12)
197 ),
198 "datetime and all values given",
199 ),
200 (
201 OccurrenceID("VTODO", "+-2123_", None, datetime(1971, 2, 12, 10, 21)),
202 "datetime no recurrence id",
203 ),
204 (
205 OccurrenceID("asd", "+-2123_", None, date(1971, 2, 12)),
206 "date no recurrence id",
207 ),
208 (
209 OccurrenceID("VALARM", ALL_CHARS, None, date(1971, 2, 12)),
210 "no recurrence id and a wild UID",
211 ),
212 (
213 OccurrenceID(
214 "asd",
215 ALL_CHARS,
216 datetime(2025, 2, 11, 14, 14),
217 datetime(1971, 2, 12, 10, 23),
218 ),
219 "recurrence id and a wild UID",
220 ),
221 (
222 OccurrenceID(
223 "VEVENT",
224 "",
225 None,
226 datetime(1971, 2, 12, 10, 23, tzinfo=ZoneInfo("UTC")),
227 ),
228 "start in UTC",
229 ),
230 (
231 OccurrenceID(
232 "VEVENT",
233 "",
234 None,
235 datetime(1971, 2, 12, 10, 23, tzinfo=ZoneInfo("Europe/Berlin")),
236 ),
237 "start in Europe/Berlin",
238 ),
239 ],
240 )
241 def test_equality_after_parsed(original_component_id: OccurrenceID, message: str):
242 """Check the equality after parsing."""
243 parsed_component_id = OccurrenceID.from_string(original_component_id.to_string())
244 assert original_component_id == parsed_component_id, (
245 "The parsed component id should be equal to its source. " + message
246 )
247 assert original_component_id.to_string() == parsed_component_id.to_string(), (
248 "The string representation cannot change. " + message
249 )