Debian Codegraph @

recurring_ical_events/test/test_issue_211_pagination.py

raw symbols SCIP
1 """Test the pagination behaviour of the query."""
2
3 from datetime import date, timedelta
4 from typing import Callable, Iterator
5
6 import pytest
7 from icalendar import Component
8
9 from recurring_ical_events.occurrence import OccurrenceID
10 from recurring_ical_events.pages import Page, Pages
11 from recurring_ical_events.test.conftest import ICSCalendars
12 from recurring_ical_events.util import compare_greater
13
14
15 @pytest.fixture(params=[1, 5, 30])
16 def page_size(request: pytest.FixtureRequest) -> int:
17 """Return the number of events per page."""
18 return request.param
19
20
21 GET_PAGES = Callable[[], Pages]
22
23
24 def iterate_pages(get_pages: GET_PAGES):
25 """Iterate over the pages and return all the events as a list."""
26 pages = get_pages()
27 for page in pages:
28 components = page.components
29 assert len(components) == pages.size or (
30 not page.has_next_page() and 0 < len(components) <= pages.size
31 )
32 yield from components
33
34
35 def iterate_next_page(get_pages: GET_PAGES):
36 """Iterate with the next page attribute."""
37 pages = get_pages()
38 while True:
39 page = pages.generate_next_page()
40 yield from page.components
41 if not page:
42 break
43
44
45 def continue_iteration_with_page_id(get_pages: Callable[[str], Pages]):
46 """Restart the iteration process with a new pages object."""
47 pages: Pages = get_pages()
48 page = pages.generate_next_page()
49 while page:
50 yield from page.components
51 assert (page.next_page_id == "") == page.is_last()
52 if not page.next_page_id:
53 break
54 pages = get_pages(page.next_page_id)
55 page = pages.generate_next_page()
56
57
58 @pytest.fixture(
59 params=[iterate_pages, iterate_next_page, continue_iteration_with_page_id]
60 )
61 def page_iterator(
62 request: pytest.FixtureRequest,
63 ) -> Callable[[Pages], Iterator[Component]]:
64 return request.param
65
66
67 check_calendars = pytest.mark.parametrize(
68 ("calendar", "start", "stop", "expected_count"),
69 [
70 ("no_events", date(1970, 12, 11), date(2030, 12, 12), 0),
71 ("one_event", date(2000, 12, 11), date(2020, 12, 12), 1),
72 ("event_10_times", date(2020, 1, 13), date(2020, 1, 14), 1),
73 ("event_10_times", date(2020, 1, 13), date(2023, 12, 14), 10),
74 ("event_10_times", date(2020, 1, 13), None, 10),
75 ("event_10_times", date(2020, 1, 13), date(1970, 12, 14), 0),
76 ("several_events_at_the_same_time", date(2019, 1, 13), None, 10),
77 ],
78 )
79
80
81 @check_calendars
82 def test_compare_events_with_expected(
83 calendars: ICSCalendars,
84 calendar: str,
85 start: date,
86 stop: date,
87 page_size: int,
88 page_iterator: Callable[[Pages], Iterator[Component]],
89 expected_count: int,
90 ):
91 """Test the pagination with only one event."""
92
93 def paginate(next_page_id: str = ""):
94 return calendars[calendar].paginate(page_size, start, stop, next_page_id)
95
96 count = 0
97 for page_component, after_component in zip(
98 page_iterator(paginate), calendars[calendar].after(start)
99 ):
100 assert page_component == after_component
101 assert stop is None or compare_greater(stop, page_component.start)
102 print(f"page_component: {page_component}")
103 count += 1
104 assert count == expected_count
105
106
107 def test_empty_page_bool():
108 """Check an empty page."""
109 assert not Page([])
110
111
112 def test_filled_page_bool():
113 """Check the page has content."""
114 assert Page([1])
115
116
117 @pytest.mark.parametrize("last_page", [Page([]), Page([1])])
118 def test_next_page_absent(last_page: Page):
119 """Check not having a next page."""
120 assert not last_page.has_next_page()
121 assert last_page.next_page_id == ""
122 assert last_page.is_last()
123
124
125 @pytest.mark.parametrize(
126 "page", [Page([1, 2, 3], next_page_id="123"), Page([1], next_page_id="asd")]
127 )
128 def test_next_page_present(page: Page):
129 """Check having a next page.
130
131 We must at least have a component.
132 """
133 assert page.next_page_id != ""
134 assert page.has_next_page()
135 assert not page.is_last()
136
137
138 def test_pages_size_is_the_same_as_parameter(page_size: int):
139 """The size parameter is passed."""
140 pages = Pages([], page_size)
141 assert pages.size == page_size
142
143
144 @pytest.mark.parametrize("invalid_page_size", [-1, 0, -1100])
145 def test_paginate_invalid_int(invalid_page_size):
146 """Raise a ValueError if we have an invalid page size."""
147 with pytest.raises(ValueError):
148 Pages([], invalid_page_size)
149
150
151 def test_count_events(calendars: ICSCalendars):
152 """Check that there is an event."""
153 assert calendars.one_event.count() == 1
154 assert calendars.no_events.count() == 0
155
156
157 def test_cannot_escape_start_with_pagination_id(calendars: ICSCalendars):
158 """The pagination id is passed from outside to this.
159
160 We must make sure that we cannot be hacked.
161 """
162 start = date(2020, 1, 17) # the first event is at the 13th
163 pages: Pages = calendars.event_10_times.paginate(1, start)
164 first_page = pages.generate_next_page()
165 assert first_page
166 assert not first_page.is_last()
167 # request the next page but with an earlier id
168 oid = OccurrenceID.from_string(first_page.next_page_id)
169 new_oid = OccurrenceID(
170 oid.name, oid.uid, oid.recurrence_id, oid.start - timedelta(days=30)
171 )
172 pages: Pages = calendars.event_10_times.paginate(
173 1, start, next_page_id=new_oid.to_string()
174 )
175 # we cannot be earlier than start though
176 next_page = pages.generate_next_page()
177 assert next_page.components[0].start == first_page.components[0].start
178 assert next_page.components == first_page.components
179
180
181 def invalidate_recurrence_id(next_page_id: str) -> str:
182 """We change the recurrence id."""
183 oid = OccurrenceID.from_string(next_page_id)
184 return OccurrenceID(oid.name, oid.uid, date(1990, 10, 11), oid.start).to_string()
185
186
187 def invalidate_uid(next_page_id: str) -> str:
188 """We change the uid."""
189 oid = OccurrenceID.from_string(next_page_id)
190 return OccurrenceID(
191 oid.name, oid.uid + "-changed", oid.recurrence_id, oid.start
192 ).to_string()
193
194
195 @pytest.mark.parametrize("invalidate_id", [invalidate_uid, invalidate_recurrence_id])
196 def test_invalid_recurrence_id_uid_will_not_go_though_all_events_but_stop_after_that_date(
197 calendars: ICSCalendars, invalidate_id: Callable[[str], str]
198 ):
199 """If we cannot find an event, the page starts on the day it should."""
200 start = date(2020, 1, 17) # the first event is at the 13th
201 pages: Pages = calendars.event_10_times.paginate(1, start)
202 first_page = pages.generate_next_page()
203 real_next_page = pages.generate_next_page()
204 # invalidate the event we are looking for
205 pages: Pages = calendars.event_10_times.paginate(
206 1, start, next_page_id=invalidate_id(first_page.next_page_id)
207 )
208 # we will still find the correct event because of the start
209 # and we only have one event per day
210 modified_next_page = pages.generate_next_page()
211 assert real_next_page.components[0].start == modified_next_page.components[0].start
212 assert real_next_page.components == modified_next_page.components