Skip to content

Commit d42bf9d

Browse files
gctuckerJenySadadia
authored andcommitted
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 1878fde commit d42bf9d

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"
@@ -190,3 +198,26 @@ def split_attributes(attributes: typing.List[str]):
190198
''.join((key, opstr)): value
191199
for key, (opstr, value) in parsed.items()
192200
}
201+
202+
203+
def get_pagination(page_length: int, page_number: int):
204+
"""Get the offset and limit values for paginated data
205+
206+
Return a 2-tuple (offset, limit) which can be used with paginated data from
207+
the API. The `page_length` and `page_number` values are used to get the
208+
absolute start `offset`. The `page_length` value is the same as the
209+
`limit` API value, essentially the maximum number of items per page.
210+
"""
211+
if page_length is None:
212+
page_length = 10
213+
elif page_length < 1:
214+
raise click.UsageError(
215+
f"Page length must be at least 1, got {page_length}"
216+
)
217+
if page_number is None:
218+
page_number = 0
219+
elif page_number < 0:
220+
raise click.UsageError(
221+
f"Page number must be at least 0, got {page_number}"
222+
)
223+
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
@@ -113,3 +113,36 @@ def test_split_invalid_attributes():
113113
for attrs in attributes:
114114
with pytest.raises(click.ClickException):
115115
kernelci.cli.split_attributes(attrs)
116+
117+
118+
def test_pagination():
119+
"""Test the logic to get pagination values"""
120+
values = {
121+
(None, None): (0, 10),
122+
(1, 0): (0, 1),
123+
(1, 1): (1, 1),
124+
(12, 345): (12 * 345, 12),
125+
(12345, 3): (3 * 12345, 12345),
126+
(4567, 0): (0, 4567),
127+
}
128+
for (p_len, p_num), (offset, limit) in values.items():
129+
result = kernelci.cli.get_pagination(p_len, p_num)
130+
assert result == (offset, limit)
131+
132+
133+
def test_invalid_pagination():
134+
"""Test that errors are reported with invalid pagination values"""
135+
values = [
136+
(0, 1),
137+
(0.1, 1),
138+
(-1, 0),
139+
(0, 0),
140+
(0, -1),
141+
(1, -1),
142+
(-123, -123),
143+
(123, -1),
144+
(0, 123),
145+
]
146+
for p_len, p_num in values:
147+
with pytest.raises(click.UsageError):
148+
kernelci.cli.get_pagination(p_len, p_num)

0 commit comments

Comments
 (0)