File: test_time_span_contains_event.py

package info (click to toggle)
python-recurring-ical-events 3.3.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,128 kB
  • sloc: python: 2,896; sh: 15; makefile: 3
file content (227 lines) | stat: -rw-r--r-- 7,423 bytes parent folder | download | duplicates (2)
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
from datetime import date, datetime

import pytest
from pytz import timezone, utc

from recurring_ical_events import PeriodEndBeforeStart, time_span_contains_event

berlin = timezone("Europe/Berlin").localize


@pytest.mark.parametrize(
    ("span_start", "span_stop", "event_start", "event_stop", "result", "message"),
    [
        (0, 5, 1, 3, True, "event lies inside"),
        (0, 5, -3, -1, False, "event lies before"),
        (0, 5, 34, 41, False, "event lies after "),
        (0, 5, 4, 6, True, "event overlaps end"),
        (0, 5, -1, 2, True, "event overlaps start"),
        (0, 5, -1, 7, True, "event overlaps start and end"),
        (0, 5, 0, 4, True, "event begins at start"),
        (0, 5, 1, 5, True, "event ends at end"),
        # check that datetime and date can be used and mixed
        (
            datetime(2019, 1, 1, 1),
            datetime(2019, 1, 4, 1),
            date(2019, 1, 2),
            date(2019, 1, 3),
            True,
            "date is in datetime span",
        ),
        (
            date(2019, 1, 3),
            date(2019, 1, 4),
            date(2019, 1, 2),
            date(2019, 1, 3),
            False,
            "date is before span",
        ),
        (
            date(2019, 1, 4),
            date(2019, 1, 4),
            datetime(2019, 1, 2, 1),
            datetime(2019, 1, 4),
            False,
            "datetime is before date span start",
        ),
        (
            date(2019, 1, 4),
            date(2019, 1, 5),
            datetime(2019, 1, 4, 1),
            datetime(2019, 1, 4, 2),
            True,
            "datetime is in date span end",
        ),
        (
            berlin(datetime(2019, 1, 1, 1)),
            berlin(datetime(2019, 1, 4, 2)),
            datetime(2019, 1, 4, 1, 10),
            datetime(2019, 1, 4, 1, 20),
            True,
            "without time zone is put into time zone",
        ),
        (
            berlin(datetime(2019, 1, 1, 1)),
            berlin(datetime(2019, 1, 1, 2)),
            datetime(2019, 1, 1, 0, tzinfo=utc),
            datetime(2019, 1, 1, 1, tzinfo=utc),
            True,
            "comparing times from different time zones 1",
        ),
        (
            berlin(datetime(2019, 1, 1, 4)),
            berlin(datetime(2019, 1, 1, 5)),
            datetime(2019, 1, 1, 0, tzinfo=utc),
            datetime(2019, 1, 1, 1, tzinfo=utc),
            False,
            "comparing times from different time zones 2",
        ),
        # The end of the VEVENT is exclusive, see RFC5545
        #    Note that the "DTEND" property is
        #    set to July 9th, 2007, since the "DTEND" property specifies the
        #    non-inclusive end of the event.
        #
        # Tests:
        # - We should not include an event which ends at a requested start date.
        # - We should include an event which ends at an end date but is included
        #  in the range.
        (
            date(2019, 1, 4),
            date(2019, 1, 5),
            date(2019, 1, 3),
            date(2019, 1, 4),
            False,
            "We should not include an event which ends at a requested start date.",
        ),
        (
            datetime(2019, 1, 4),
            datetime(2019, 1, 5),
            date(2019, 1, 3),
            date(2019, 1, 4),
            False,
            "We should not include an event which ends at a requested start date.",
        ),
        (
            datetime(2019, 1, 4),
            datetime(2019, 1, 5),
            datetime(2019, 1, 4, 3),
            datetime(2019, 1, 5),
            True,
            "We should include an event which ends at an end date but is included",
        ),
        # Events with 0 duration should be tested
        (0, 1, 0, 0, True, "zero size event at start"),
        (0, 2, 1, 1, True, "zero size event in middle"),
        (0, 1, 1, 1, False, "zero size event at end"),
        (1, 1, 1, 1, True, "zero size event at zero size span"),
        (
            date(2019, 1, 4),
            date(2019, 1, 4),
            date(2019, 1, 4),
            date(2019, 1, 4),
            True,
            "We should include an event which is of the same size as the span",
        ),
        (
            date(2019, 1, 3),
            date(2019, 1, 4),
            date(2019, 1, 4),
            date(2019, 1, 4),
            False,
            "We should NOT include an event which ends at an end date but is included",
        ),
        (0, 0, 1, 1, False, "zero size event after"),
        (2, 2, 1, 1, False, "zero size event before"),
        # Test the exclusivity of the end of the span
        (
            1,
            3,
            3,
            4,
            False,
            "When the event starts at the end of the span, it should not be included.",
        ),
        # Test zero size spans
        (1, 1, 0, 0, False, "zero size event before zero size span"),
        (1, 1, 0, 1, False, "event ending at zero size span"),
        (1, 1, 0, 2, True, "event around zero size span"),
        (1, 1, 1, 2, True, "event starting at zero size span"),
        (1, 1, 2, 3, False, "event after zero size span"),
        (1, 1, 2, 2, False, "zero size event after zero size span"),
    ],
)
def test_time_span_inclusion(
    span_start, span_stop, event_start, event_stop, result, message
):
    assert (
        time_span_contains_event(
            span_start,
            span_stop,
            event_start,
            event_stop,
        )
        == result
    ), message


@pytest.mark.parametrize(
    ("span_start", "span_stop", "event_start", "event_stop", "exception_message"),
    [
        (1, 2, 1, 2, None),
        (1, 1, 1, 1, None),
        (1, 2, 2, 1, r"^the event"),
        (2, 1, 1, 2, r"^the time span"),
        (date(2024, 4, 4), date(2024, 4, 4), date(2024, 4, 4), date(2024, 4, 4), None),
        (date(2024, 4, 4), date(2024, 4, 5), date(2024, 4, 4), date(2024, 4, 5), None),
        (
            date(2024, 4, 4),
            date(2024, 4, 5),
            date(2024, 4, 5),
            date(2024, 4, 4),
            r"^the event",
        ),
        (
            date(2024, 4, 5),
            date(2024, 4, 4),
            date(2024, 4, 4),
            date(2024, 4, 5),
            r"^the time span",
        ),
        (
            datetime(2024, 4, 4),
            datetime(2024, 4, 4),
            datetime(2024, 4, 4),
            datetime(2024, 4, 4),
            None,
        ),
        (
            datetime(2024, 4, 4),
            datetime(2024, 4, 5),
            datetime(2024, 4, 4),
            datetime(2024, 4, 5),
            None,
        ),
        (
            datetime(2024, 4, 4),
            datetime(2024, 4, 5),
            datetime(2024, 4, 5),
            datetime(2024, 4, 4),
            r"^the event",
        ),
        (
            datetime(2024, 4, 5),
            datetime(2024, 4, 4),
            datetime(2024, 4, 4),
            datetime(2024, 4, 5),
            r"^the time span",
        ),
    ],
)
def test_time_span_end_before_start_raise_exception(
    span_start, span_stop, event_start, event_stop, exception_message
):
    if exception_message:
        with pytest.raises(PeriodEndBeforeStart, match=exception_message):
            time_span_contains_event(span_start, span_stop, event_start, event_stop)
    else:
        time_span_contains_event(span_start, span_stop, event_start, event_stop)