Skip to content

Commit 49a9639

Browse files
committed
kernelci.cli: add pagination options and helper
Add the --page-length / -l and --page-number / -n options for paginated API data. Provide a kernelci.cli.get_pagination() function to convert these arguments into the offset / limit values used with the API as pagination is easier with relative numbers on the command line. Add unit tests accordingly to test this function. Signed-off-by: Guillaume Tucker <[email protected]>
1 parent 4c4da00 commit 49a9639

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

kernelci/cli/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ class Args: # pylint: disable=too-few-public-methods
3535
'--indent', type=int,
3636
help="Intentation level for structured data output"
3737
)
38+
page_length = click.option(
39+
'-l', '--page-length', type=int,
40+
help="Page length in paginated data"
41+
)
42+
page_number = click.option(
43+
'-n', '--page-number', type=int,
44+
help="Page number in paginated data"
45+
)
3846
settings = click.option(
3947
'-s', '--toml-settings', 'settings',
4048
help="Path to the TOML user settings"
@@ -189,3 +197,26 @@ def split_attributes(attributes: typing.List[str]):
189197
''.join((key, opstr)): value
190198
for key, (opstr, value) in parsed.items()
191199
}
200+
201+
202+
def get_pagination(page_length: int, page_number: int):
203+
"""Get the offset and limit values for paginated data
204+
205+
Return a 2-tuple (offset, limit) which can be used with paginated data from
206+
the API. The `page_length` and `page_number` values are used to get the
207+
absolute start `offset`. The `page_length` value is the same as the
208+
`limit` API value, essentially the maximum number of items per page.
209+
"""
210+
if page_length is None:
211+
page_length = 10
212+
elif page_length < 1:
213+
raise click.UsageError(
214+
f"Page length must be at least 1, got {page_length}"
215+
)
216+
if page_number is None:
217+
page_number = 0
218+
elif page_number < 0:
219+
raise click.UsageError(
220+
f"Page number must be at least 0, got {page_number}"
221+
)
222+
return page_number * page_length, page_length

tests/test_cli.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,36 @@ def test_split_invalid_attributes():
111111
for attrs in attributes:
112112
with pytest.raises(click.ClickException):
113113
kernelci.cli.split_attributes(attrs)
114+
115+
116+
def test_pagination():
117+
"""Test the logic to get pagination values"""
118+
values = {
119+
(None, None): (0, 10),
120+
(1, 0): (0, 1),
121+
(1, 1): (1, 1),
122+
(12, 345): (12 * 345, 12),
123+
(12345, 3): (3 * 12345, 12345),
124+
(4567, 0): (0, 4567),
125+
}
126+
for (p_len, p_num), (offset, limit) in values.items():
127+
result = kernelci.cli.get_pagination(p_len, p_num)
128+
assert result == (offset, limit)
129+
130+
131+
def test_invalid_pagination():
132+
"""Test that errors are reported with invalid pagination values"""
133+
values = [
134+
(0, 1),
135+
(0.1, 1),
136+
(-1, 0),
137+
(0, 0),
138+
(0, -1),
139+
(1, -1),
140+
(-123, -123),
141+
(123, -1),
142+
(0, 123),
143+
]
144+
for p_len, p_num in values:
145+
with pytest.raises(click.UsageError):
146+
kernelci.cli.get_pagination(p_len, p_num)

0 commit comments

Comments
 (0)