| 1 |
"""This tests the range parameter for ics file. |
| 2 |
see https://github.com/niccokunzmann/python-recurring-ical-events/issues/75 |
| 3 |
Description: This parameter can be specified on a property that |
| 4 |
specifies a recurrence identifier. The parameter specifies the |
| 5 |
effective range of recurrence instances that is specified by the |
| 6 |
property. The effective range is from the recurrence identifier |
| 7 |
specified by the property. If this parameter is not specified on |
| 8 |
an allowed property, then the default range is the single instance |
| 9 |
specified by the recurrence identifier value of the property. The |
| 10 |
parameter value can only be "THISANDFUTURE" to indicate a range |
| 11 |
defined by the recurrence identifier and all subsequent instances. |
| 12 |
The value "THISANDPRIOR" is deprecated by this revision of |
| 13 |
iCalendar and MUST NOT be generated by applications. |
| 14 |
|
| 15 |
- https://www.rfc-editor.org/rfc/rfc5545.html#section-3.2.13 |
| 16 |
""" |
| 17 |
|
| 18 |
from datetime import time |
| 19 |
from datetime import timedelta as td |
| 20 |
from typing import TYPE_CHECKING |
| 21 |
|
| 22 |
import pytest |
| 23 |
|
| 24 |
from recurring_ical_events.adapters.event import EventAdapter |
| 25 |
|
| 26 |
if TYPE_CHECKING: |
| 27 |
from calendar import Calendar |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.parametrize( |
| 31 |
("date", "summary"), |
| 32 |
[ |
| 33 |
("20240901", "ORIGINAL EVENT"), |
| 34 |
("20240911", "ORIGINAL EVENT"), |
| 35 |
("20240913", "MODIFIED EVENT"), |
| 36 |
("20240914", "MODIFIED EVENT"), |
| 37 |
("20240915", "MODIFIED EVENT"), |
| 38 |
("20240917", "MODIFIED EVENT"), |
| 39 |
("20240919", "MODIFIED EVENT"), |
| 40 |
("20240922", "EDITED EVENT"), |
| 41 |
("20240924", "EDITED EVENT"), |
| 42 |
("20240926", "EDITED EVENT"), |
| 43 |
], |
| 44 |
) |
| 45 |
def test_issue_75_RANGE_AT_parameter(calendars, date, summary): |
| 46 |
events = calendars.issue_75_range_parameter.at(date) |
| 47 |
assert len(events) == 1, f"Expecting one event at {date}" |
| 48 |
event = events[0] |
| 49 |
assert str(event["SUMMARY"]) == summary |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.parametrize( |
| 53 |
("start", "end", "summary", "total"), |
| 54 |
[ |
| 55 |
("20240901T000000Z", "20240911T235959Z", "ORIGINAL EVENT", 6), |
| 56 |
("20240901T000000Z", "20240913T000000Z", "ORIGINAL EVENT", 6), |
| 57 |
("20240901T000000Z", "20240913T235959Z", "MODIFIED EVENT", 7), |
| 58 |
("20240901T000000Z", "20240914T235959Z", "MODIFIED EVENT", 8), |
| 59 |
( |
| 60 |
"20240901T000000Z", |
| 61 |
"20240915T235959Z", |
| 62 |
"MODIFIED EVENT", |
| 63 |
9, |
| 64 |
), |
| 65 |
("20240901T000000Z", "20240917T235959Z", "MODIFIED EVENT", 10), |
| 66 |
("20240901T000000Z", "20240919T235959Z", "MODIFIED EVENT", 11), |
| 67 |
("20240901T000000Z", "20240921T235959Z", "MODIFIED EVENT", 11), |
| 68 |
("20240901T000000Z", "20240922T000000Z", "MODIFIED EVENT", 11), |
| 69 |
("20240901T000000Z", "20240922T235959Z", "EDITED EVENT", 12), |
| 70 |
("20240901T000000Z", "20240923T000000Z", "EDITED EVENT", 12), |
| 71 |
("20240901T000000Z", "20240923T235959Z", "EDITED EVENT", 12), |
| 72 |
("20240901T000000Z", "20240924T235959Z", "EDITED EVENT", 13), |
| 73 |
("20240901T000000Z", "20240925T235959Z", "EDITED EVENT", 13), |
| 74 |
( |
| 75 |
"20240913T000000Z", |
| 76 |
"20240922T000000Z", |
| 77 |
"MODIFIED EVENT", |
| 78 |
5, |
| 79 |
), |
| 80 |
( |
| 81 |
"20240913T000000Z", |
| 82 |
"20240922T235959Z", |
| 83 |
"EDITED EVENT", |
| 84 |
6, |
| 85 |
), |
| 86 |
( |
| 87 |
"20240924T000000Z", |
| 88 |
"20240925T235959Z", |
| 89 |
"EDITED EVENT", |
| 90 |
1, |
| 91 |
), |
| 92 |
], |
| 93 |
) |
| 94 |
def test_issue_75_RANGE_BETWEEN_parameter(calendars, start, end, summary, total): |
| 95 |
events = calendars.issue_75_range_parameter.between(start, end) |
| 96 |
assert len(events) == total, ( |
| 97 |
f"Expecting {total} events at range {start}, {end}, get {len(events)}" |
| 98 |
) |
| 99 |
event = events[-1] |
| 100 |
assert str(event["SUMMARY"]) == summary |
| 101 |
|
| 102 |
|
| 103 |
@pytest.mark.parametrize( |
| 104 |
("date", "start", "end"), |
| 105 |
[ |
| 106 |
|
| 107 |
((2024, 9, 13, 9), (9, 0), (16, 0)), |
| 108 |
((2024, 9, 17, 9), (9, 0), (16, 0)), |
| 109 |
|
| 110 |
((2024, 9, 22, 14, 22), (14, 22), (16, 13)), |
| 111 |
((2024, 9, 24, 14, 22), (14, 22), (16, 13)), |
| 112 |
], |
| 113 |
) |
| 114 |
def test_the_length_of_modified_events(calendars, date, start, end): |
| 115 |
"""There should be one event exactly starting and ending at these times.""" |
| 116 |
events = calendars.issue_75_range_parameter.at(date) |
| 117 |
assert len(events) != 0, "The calculation could not find an event!" |
| 118 |
assert len(events) == 1, "Modify the test to yield one event only!" |
| 119 |
event = events[0] |
| 120 |
assert event["DTSTART"].dt.time() == time(*start) |
| 121 |
assert event["DTEND"].dt.time() == time(*end) |
| 122 |
|
| 123 |
|
| 124 |
@pytest.mark.parametrize( |
| 125 |
("calendar", "event_index", "expected_start_delta", "expected_end_delta"), |
| 126 |
[ |
| 127 |
|
| 128 |
("issue_62_moved_event", 1, td(0), td(0)), |
| 129 |
|
| 130 |
("issue_62_moved_event", 0, td(0), td(14)), |
| 131 |
|
| 132 |
("one_event", 0, td(minutes=30), td(0)), |
| 133 |
|
| 134 |
("same_event_recurring_at_same_time", 1, td(days=1, hours=1), td(0)), |
| 135 |
|
| 136 |
("same_event_recurring_at_same_time", 2, td(0), td(hours=1)), |
| 137 |
|
| 138 |
( |
| 139 |
"issue_75_range_parameter", |
| 140 |
3, |
| 141 |
td(days=1, hours=2, minutes=22) + td(hours=1, minutes=51), |
| 142 |
td(0), |
| 143 |
), |
| 144 |
], |
| 145 |
) |
| 146 |
def test_span_extension( |
| 147 |
calendars, calendar, event_index, expected_start_delta, expected_end_delta |
| 148 |
): |
| 149 |
"""If we have an event that is moved with THISANDFUTURE, |
| 150 |
other events move, too. |
| 151 |
|
| 152 |
This requires us to extend the range which we query: |
| 153 |
- If an event moves forward, we need to extend the span to the back ... |
| 154 |
- If an event moves backward, we need to extend the span to the front ... |
| 155 |
... in order to capture the recurrences from the rrule that would yield |
| 156 |
the occurrence. |
| 157 |
|
| 158 |
If the length is extended, we can shorten the span |
| 159 |
If the length is reduced, we have to extend the span |
| 160 |
|
| 161 |
This tests the adapter to yield the correct values for the given types |
| 162 |
of moves. |
| 163 |
|
| 164 |
We only have to extend the range for THISANDFUTURE events because |
| 165 |
we iterate over all modifications either way. |
| 166 |
TODO: However, for optimization, one could approach to create ranges that |
| 167 |
specify how to extend and contract the spans. |
| 168 |
|
| 169 |
This test has to test of types of recurrence id, start and end. |
| 170 |
- date |
| 171 |
- datetime without tzinfo |
| 172 |
- datetime with UTC |
| 173 |
- datetime with tzinfo other than UTC |
| 174 |
|
| 175 |
>.The default value type is DATE-TIME. The value type can |
| 176 |
be set to a DATE value type. This property MUST have the same |
| 177 |
value type as the "DTSTART" property contained within the |
| 178 |
recurring component. Furthermore, this property MUST be specified |
| 179 |
as a date with local time if and only if the "DTSTART" property |
| 180 |
contained within the recurring component is specified as a date |
| 181 |
with local time. |
| 182 |
- https://www.rfc-editor.org/rfc/rfc5545.html#section-3.8.4.4 |
| 183 |
|
| 184 |
moves must include: |
| 185 |
- time forward |
| 186 |
- time backward |
| 187 |
- several days forward |
| 188 |
- several days backward |
| 189 |
|
| 190 |
Assumptions |
| 191 |
----------- |
| 192 |
|
| 193 |
This test is for a rought estimate. We can extend the range by +1 day into each direction. |
| 194 |
This will allow us to capture everything. |
| 195 |
Future examples and tests may help us improve the situation by narrowing it further down. |
| 196 |
|
| 197 |
Safe: |
| 198 |
- move 1 h forward -> END: add 1 day for timezone + 1 day for timedelta without timezone involvement (round up) |
| 199 |
""" |
| 200 |
cal = calendars.raw[calendar] |
| 201 |
event = list(cal.walk("VEVENT"))[event_index] |
| 202 |
adapter = EventAdapter(event) |
| 203 |
assert adapter.duration >= td(0) |
| 204 |
start_delta, end_delta = adapter.extend_query_span_by |
| 205 |
assert start_delta >= expected_start_delta |
| 206 |
assert end_delta >= expected_end_delta |
| 207 |
|
| 208 |
|
| 209 |
def test_can_calculate_query_span_extension_on_all_events(calendars, calendar_name): |
| 210 |
"""Check that the calclulation succeeds.""" |
| 211 |
for i, event in enumerate(calendars.raw[calendar_name].walk("VEVENT")): |
| 212 |
adapter = EventAdapter(event) |
| 213 |
start_delta, end_delta = adapter.extend_query_span_by |
| 214 |
message = f"{calendar_name}.VEVENT[{i}]" |
| 215 |
assert isinstance(start_delta, td), message |
| 216 |
assert isinstance(end_delta, td), message |
| 217 |
assert start_delta >= td(0), message |
| 218 |
assert end_delta >= td(0), message |
| 219 |
|
| 220 |
|
| 221 |
def test_deletion_of_THISANDFUTURE_by_SEQUENCE(): |
| 222 |
"""We need to make sure that the components we have only work on what is actual.""" |
| 223 |
pytest.skip("TODO") |
| 224 |
|
| 225 |
|
| 226 |
def test_RDATE_with_PERIOD(): |
| 227 |
"""When an RDATE has a PERIOD, we can assume that that defines the new length.""" |
| 228 |
pytest.skip("TODO") |
| 229 |
|
| 230 |
|
| 231 |
@pytest.mark.parametrize( |
| 232 |
("calendar_name", "event_index", "delta"), |
| 233 |
[ |
| 234 |
("one_event", 0, td(0)), |
| 235 |
("same_event_recurring_at_same_time", 0, td(0)), |
| 236 |
("issue_75_range_parameter", 1, td(hours=-3)), |
| 237 |
("issue_75_range_parameter", 3, td(days=1, hours=2, minutes=22)), |
| 238 |
], |
| 239 |
) |
| 240 |
def test_move_by_time(calendars, calendar_name, event_index, delta): |
| 241 |
"""Check the moving of events.""" |
| 242 |
cal: Calendar = calendars.raw[calendar_name] |
| 243 |
event = list(cal.walk("VEVENT"))[event_index] |
| 244 |
adapter = EventAdapter(event) |
| 245 |
assert adapter.move_recurrences_by == delta |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|