| 1 |
"""Test VALARM recurrence. |
| 2 |
|
| 3 |
VALARM is specified in RFC 5545 and RFC 9074. |
| 4 |
See also https://github.com/niccokunzmann/python-recurring-ical-events/issues/186 |
| 5 |
""" |
| 6 |
|
| 7 |
from datetime import datetime, timedelta, timezone |
| 8 |
|
| 9 |
import icalendar |
| 10 |
import pytest |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.parametrize( |
| 14 |
("when", "count"), |
| 15 |
[ |
| 16 |
("20241003", 1), |
| 17 |
((2024, 10, 3, 13), 1), |
| 18 |
((2024, 10, 3, 13, 0), 1), |
| 19 |
((2024, 10, 3, 12), 0), |
| 20 |
((2024, 10, 3, 14), 0), |
| 21 |
], |
| 22 |
) |
| 23 |
def test_can_find_absolute_alarm(alarms, when, count): |
| 24 |
"""Find the absolute alarm.""" |
| 25 |
a = alarms.alarm_absolute.at(when) |
| 26 |
assert len(a) == count |
| 27 |
if count == 1: |
| 28 |
e: icalendar.Event = a[0] |
| 29 |
assert len(e.alarms.times) == 1 |
| 30 |
t = e.alarms.times[0] |
| 31 |
assert t.trigger == datetime(2024, 10, 3, 13, 0, 0, tzinfo=timezone.utc) |
| 32 |
|
| 33 |
|
| 34 |
def test_edited_alarm_is_moved(alarms): |
| 35 |
"""When an absolute alarm is edited, the old one does not occur.""" |
| 36 |
assert len(alarms.alarm_absolute_edited.at("20241004")) == 1, "New alarm is found" |
| 37 |
assert len(alarms.alarm_absolute_edited.at("20241003")) == 0, "Old alarm is removed" |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.parametrize( |
| 41 |
("when", "deltas"), |
| 42 |
[ |
| 43 |
("20241003", {0, 45, 90}), |
| 44 |
((2024, 10, 3, 13), {0, 45}), |
| 45 |
((2024, 10, 3, 13, 0), {0}), |
| 46 |
((2024, 10, 3, 12), set()), |
| 47 |
((2024, 10, 3, 14), {90}), |
| 48 |
], |
| 49 |
) |
| 50 |
def test_can_find_absolute_alarm_with_repeat(alarms, when, deltas): |
| 51 |
"""This absolute alarm has 2 repetitions in 45 min later.""" |
| 52 |
a = alarms.alarm_absolute_repeat.at(when) |
| 53 |
deltas = {timedelta(minutes=m) for m in deltas} |
| 54 |
e_deltas = set() |
| 55 |
for e in a: |
| 56 |
assert len(e.alarms.times) == 1 |
| 57 |
t = e.alarms.times[0] |
| 58 |
e_deltas.add(t.trigger - datetime(2024, 10, 3, 13, 0, 0, tzinfo=timezone.utc)) |
| 59 |
assert e_deltas == deltas |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.parametrize("day", [17, 18, 19, 20]) |
| 63 |
def test_collect_alarms_from_todos_relative_to_start(alarms, day): |
| 64 |
"""We also collect alarms from todos.""" |
| 65 |
todos = alarms.alarm_removed_and_moved.at((2023, 12, day)) |
| 66 |
assert len(todos) == 1 |
| 67 |
todo = todos[0] |
| 68 |
assert len(todo.alarms.times) == 1 |
| 69 |
alarm = todo.alarms.times[0] |
| 70 |
assert alarm.trigger.replace(tzinfo=None) == datetime(2023, 12, day, 8, 0) |
| 71 |
|
| 72 |
|
| 73 |
@pytest.mark.parametrize("day", [17, 18, 19, 20]) |
| 74 |
def test_collect_todos_with_alarms(calendars, day): |
| 75 |
"""We also collect alarms from todos.""" |
| 76 |
calendars.components = ["VTODO"] |
| 77 |
todos = calendars.alarm_removed_and_moved.at((2023, 12, day)) |
| 78 |
assert len(todos) == 1 |
| 79 |
todo = todos[0] |
| 80 |
assert todo.start.replace(tzinfo=None) == datetime(2023, 12, day, 9, 0) |
| 81 |
|
| 82 |
|
| 83 |
def test_collect_alarms_from_todos_relative_to_end(alarms): |
| 84 |
"""We also collect alarms from todos.""" |
| 85 |
todos = alarms.alarm_removed_and_moved.at((2023, 12, 16, 10)) |
| 86 |
assert len(todos) == 1 |
| 87 |
todo = todos[0] |
| 88 |
assert todo.start.replace(tzinfo=None) == datetime(2023, 12, 16, 9, 0) |
| 89 |
assert len(todo.alarms.times) == 1 |
| 90 |
alarm = todo.alarms.times[0] |
| 91 |
assert alarm.trigger.replace(tzinfo=None) == datetime(2023, 12, 16, 10, 0) |
| 92 |
|
| 93 |
|
| 94 |
def test_todo_occurs(calendars): |
| 95 |
"""The todo should occur so we can find the alarm.""" |
| 96 |
calendars.components = ["VTODO"] |
| 97 |
todos = calendars.alarm_removed_and_moved.at((2023, 12, 16, 9)) |
| 98 |
for x in todos: |
| 99 |
print(x.to_ical().decode()) |
| 100 |
print() |
| 101 |
assert len(todos) == 1 |
| 102 |
todo = todos[0] |
| 103 |
assert todo.start.replace(tzinfo=None) == datetime(2023, 12, 16, 9, 0) |
| 104 |
|
| 105 |
|
| 106 |
def test_collect_alarms_from_todos_absolute(alarms): |
| 107 |
"""We also collect alarms from todos.""" |
| 108 |
todos = alarms.alarm_removed_and_moved.at((2023, 12, 13, 18, 0)) |
| 109 |
assert len(todos) == 1 |
| 110 |
todo = todos[0] |
| 111 |
assert len(todo.alarms.times) == 1 |
| 112 |
alarm = todo.alarms.times[0] |
| 113 |
assert alarm.trigger.replace(tzinfo=None) == datetime(2023, 12, 13, 18, 0) |
| 114 |
|
| 115 |
|
| 116 |
def test_series_of_events_with_alarms_but_alarm_removed(alarms): |
| 117 |
"""We test that an alarm is removed and does not turn up.""" |
| 118 |
assert alarms.alarm_removed_and_moved.at("20241221") == [] |
| 119 |
|
| 120 |
|
| 121 |
def test_alarm_is_moved(alarms): |
| 122 |
"""The alarm is moved to 30 min before.""" |
| 123 |
a = alarms.alarm_removed_and_moved.at("20241222") |
| 124 |
assert len(a) == 1 |
| 125 |
e = a[0] |
| 126 |
assert len(e.alarms.times) == 1 |
| 127 |
alarm = e.alarms.times[0] |
| 128 |
assert alarm.trigger.hour == 8 |
| 129 |
assert alarm.trigger.minute == 30 |
| 130 |
|
| 131 |
|
| 132 |
def test_event_is_moved(alarms): |
| 133 |
"""The event has been moved but the alarm is still 1h before.""" |
| 134 |
a = alarms.alarm_removed_and_moved.at("20241219") |
| 135 |
for x in a: |
| 136 |
print(x.to_ical().decode()) |
| 137 |
print() |
| 138 |
assert len(a) == 1 |
| 139 |
e = a[0] |
| 140 |
assert len(e.alarms.times) == 1 |
| 141 |
alarm = e.alarms.times[0] |
| 142 |
assert alarm.trigger.hour == 11 |
| 143 |
assert alarm.trigger.minute == 0 |
| 144 |
|
| 145 |
|
| 146 |
def test_series_of_events_with_alarms_but_alarm_removed_relative_to_end(): |
| 147 |
pytest.skip("TODO - but probably covered by the calculation relative to start") |
| 148 |
|
| 149 |
|
| 150 |
def test_series_of_events_with_alarms_but_alarm_edited_relative_to_end(): |
| 151 |
pytest.skip("TODO - but probably covered by the calculation relative to start") |
| 152 |
|
| 153 |
|
| 154 |
def test_series_of_events_with_alarm_relative_to_end(alarms): |
| 155 |
"""We check alarms relative to the end and start. |
| 156 |
|
| 157 |
DTSTART;TZID=Europe/London:20241004T110000 |
| 158 |
DTEND;TZID=Europe/London:20241004T114500 |
| 159 |
|
| 160 |
15min before start&end |
| 161 |
15min after start&end |
| 162 |
|
| 163 |
""" |
| 164 |
q = alarms.alarm_around_event_boundaries |
| 165 |
assert len(q.at((2024, 10, 4, 10, 45))) == 1, "15 min before start" |
| 166 |
assert len(q.at((2024, 10, 4, 11, 15))) == 1, "15 min after start" |
| 167 |
assert len(q.at((2024, 10, 4, 11, 30))) == 1, "15 min before end" |
| 168 |
assert len(q.at((2024, 10, 4, 12, 0))) == 1, "15 min after end" |
| 169 |
|
| 170 |
|
| 171 |
@pytest.mark.parametrize( |
| 172 |
("dt", "trigger"), |
| 173 |
[ |
| 174 |
("20241126", datetime(2024, 11, 26, 13, 0, 0)), |
| 175 |
("20241127", datetime(2024, 11, 27, 13, 0, 0)), |
| 176 |
("20241128", datetime(2024, 11, 28, 13, 0, 0)), |
| 177 |
("20241129", datetime(2024, 11, 29, 13, 0, 0)), |
| 178 |
|
| 179 |
((2024, 11, 28, 12), None), |
| 180 |
((2024, 11, 28, 12, 30), None), |
| 181 |
((2024, 11, 28, 13), datetime(2024, 11, 28, 13, 0, 0)), |
| 182 |
((2024, 11, 28, 13, 0), datetime(2024, 11, 28, 13, 0, 0)), |
| 183 |
((2024, 11, 28, 13, 0, 0), datetime(2024, 11, 28, 13, 0, 0)), |
| 184 |
((2024, 11, 28, 13, 0, 1), None), |
| 185 |
((2024, 11, 28, 14), None), |
| 186 |
], |
| 187 |
) |
| 188 |
def test_series_of_event_with_alarm_relative_to_start(alarms, dt, trigger): |
| 189 |
"""This series of events all are preceded by an alarm. |
| 190 |
|
| 191 |
The alarm occurs 1h before the event starts. |
| 192 |
In this test, we narrow down our query time to make sure we find it. |
| 193 |
""" |
| 194 |
a = alarms.alarm_recurring_and_acknowledged_at_2024_11_27_16_27.at(dt) |
| 195 |
if trigger is None: |
| 196 |
assert len(a) == 0 |
| 197 |
return |
| 198 |
assert len(a) == 1, f"{dt} has {len(a)} alarms" |
| 199 |
event = a[0] |
| 200 |
assert len(event.alarms.times) == 1 |
| 201 |
only_trigger = event.alarms.times[0].trigger |
| 202 |
assert only_trigger.replace(tzinfo=None) == trigger |
| 203 |
assert icalendar.timezone.tzid_from_dt(only_trigger) == "Europe/London" |
| 204 |
|
| 205 |
|
| 206 |
def test_alarm_without_trigger_is_ignored_as_invalid(alarms): |
| 207 |
"""Alarms can be malformed in many ways. This skips a few possibilities.""" |
| 208 |
alarms.skip_bad_series = True |
| 209 |
q = alarms.issue_186_invalid_trigger |
| 210 |
e = list(q.all()) |
| 211 |
for a in e: |
| 212 |
assert len(a.alarms.times) == 1 |
| 213 |
description = a.alarms.times[0].alarm["DESCRIPTION"] |
| 214 |
assert description in ("correct trigger", "absolute trigger") |
| 215 |
assert len(e) == 2 |
| 216 |
|
| 217 |
|
| 218 |
def test_event_is_not_modified_with_2_alarms(alarms): |
| 219 |
"""The base event should not be modified.""" |
| 220 |
q = alarms.alarm_1_week_before_event |
| 221 |
assert len(q.at("20241202")) == 1, "We find the alarm" |
| 222 |
assert len(q.at("20241202")) == 1, "We find the alarm again" |
| 223 |
assert len(q.at("20241207")) == 1, "We also find the other alarm" |
| 224 |
|
| 225 |
|
| 226 |
def test_repeating_event_is_not_modified_with_repeating_alarm(alarms): |
| 227 |
"""The base event should not be modified.""" |
| 228 |
q = alarms.alarm_absolute_repeat |
| 229 |
assert len(list(q.all())) == 3, "We find the alarms" |
| 230 |
assert len(list(q.all())) == 3, "We find the alarm again" |
| 231 |
|
| 232 |
|
| 233 |
def test_repeating_event_is_not_modified(alarms): |
| 234 |
"""The base event should not be modified.""" |
| 235 |
q = alarms.alarm_recurring_and_acknowledged_at_2024_11_27_16_27 |
| 236 |
assert len(q.between("20241126", "20241130")) == 4, "We find the alarms" |
| 237 |
assert len(q.between("20241126", "20241130")) == 4, "We find the alarm again" |
| 238 |
|
| 239 |
|
| 240 |
EXPECTED_TRIGGERS = [ |
| 241 |
datetime(2024, 12, 18, 8, 0), |
| 242 |
datetime(2024, 12, 19, 11, 0), |
| 243 |
datetime(2024, 12, 20, 8, 0), |
| 244 |
|
| 245 |
datetime(2024, 12, 22, 8, 30), |
| 246 |
datetime(2024, 12, 23, 8, 0), |
| 247 |
] |
| 248 |
EXPECTED_STARTS = [ |
| 249 |
datetime(2024, 12, 18, 9, 0), |
| 250 |
datetime(2024, 12, 19, 12, 0), |
| 251 |
datetime(2024, 12, 20, 9, 0), |
| 252 |
|
| 253 |
datetime(2024, 12, 22, 9, 0), |
| 254 |
datetime(2024, 12, 23, 9, 0), |
| 255 |
] |
| 256 |
|
| 257 |
|
| 258 |
def test_after_with_alarms(alarms): |
| 259 |
"""The after function checks if an event was already returned. |
| 260 |
|
| 261 |
This is likely to cause problems because it should be there several times. |
| 262 |
""" |
| 263 |
found_triggers = [] |
| 264 |
i = 0 |
| 265 |
it = alarms.alarm_removed_and_moved.after(2024) |
| 266 |
for expected_trigger, event, expected_start in zip( |
| 267 |
EXPECTED_TRIGGERS, it, EXPECTED_STARTS |
| 268 |
): |
| 269 |
print( |
| 270 |
f"{i} start {event.start} is {('' if event.start.replace(tzinfo=None) == expected_start else 'NOT ')}as expected" |
| 271 |
) |
| 272 |
assert len(event.alarms.times) == 1 |
| 273 |
trigger = event.alarms.times[0].trigger.replace(tzinfo=None) |
| 274 |
found_triggers.append(trigger) |
| 275 |
print( |
| 276 |
f"{i} trigger {trigger} is {('' if trigger == expected_trigger else 'NOT ')}as expected" |
| 277 |
) |
| 278 |
i += 1 |
| 279 |
print() |
| 280 |
print("\n".join(map(str, zip(found_triggers, EXPECTED_TRIGGERS)))) |
| 281 |
assert found_triggers == EXPECTED_TRIGGERS |
| 282 |
with pytest.raises(StopIteration): |
| 283 |
next(it) |
| 284 |
|
| 285 |
|
| 286 |
def test_all_alarms_are_present(alarms): |
| 287 |
"""Check that we find all alarms.""" |
| 288 |
events = alarms.alarm_several_in_one.all() |
| 289 |
triggers = [] |
| 290 |
for event in events: |
| 291 |
assert len(event.alarms.times) == 1 |
| 292 |
triggers.append(event.alarms.times[0].trigger - event.start) |
| 293 |
assert triggers == [ |
| 294 |
timedelta(hours=-1), |
| 295 |
timedelta(minutes=-15), |
| 296 |
timedelta(minutes=15), |
| 297 |
timedelta(hours=1), |
| 298 |
timedelta(hours=2), |
| 299 |
timedelta(hours=3), |
| 300 |
] |
| 301 |
|
| 302 |
|
| 303 |
def test_several_alarms_occur_for_a_slightly_different_event(alarms): |
| 304 |
"""Edited subevents have all an alarm that occurs at the same time. |
| 305 |
|
| 306 |
Thus, they all should appear. |
| 307 |
""" |
| 308 |
events = list(alarms.alarms_at_the_same_time.all()) |
| 309 |
summaries = {event["SUMMARY"] for event in events} |
| 310 |
assert summaries == { |
| 311 |
"event with alarm at the same time 1", |
| 312 |
"event with alarm at the same time 2", |
| 313 |
"event with alarm at the same time 3", |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
@pytest.mark.parametrize("dt", ["20241220", "20241221", "20241222"]) |
| 318 |
def test_different_alarms_at_the_same_time_merge_into_one(alarms, dt): |
| 319 |
"""If an event has different alarms happening at the same time, |
| 320 |
|
| 321 |
these alarms are in the event. |
| 322 |
""" |
| 323 |
events: list[icalendar.Event] = alarms.alarms_different_in_same_event.at(dt) |
| 324 |
alarm_names = { |
| 325 |
alarm_time.alarm["DESCRIPTION"] |
| 326 |
for event in events |
| 327 |
for alarm_time in event.alarms.times |
| 328 |
} |
| 329 |
assert alarm_names >= {"Alarm 1", "Alarm 2", "Alarm 3"} |
| 330 |
if dt == "20241220": |
| 331 |
assert "Alarm 4" in alarm_names |