Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(filter_lib): total field for pagination now show true total #979

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/filter_lib/src/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def make_pagination(
max_count: int,
page_num: Optional[int] = None,
) -> Tuple[Query, PaginationParams]:
# calculate total number of records in the database for the query
total_records = initial_query.count()

# get query for current page output
output_query = initial_query.offset(page_offset).limit(page_size)

Expand All @@ -46,7 +49,7 @@ def make_pagination(
page_num,
page_size,
min_pages_left,
total_results_count,
total_records,
has_more,
page_offset,
)
Expand Down
19 changes: 8 additions & 11 deletions lib/filter_lib/tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ def test_paginate_positive(sequence, pag_params):


@pytest.mark.parametrize(
"start, stop, page_num, page_size, page_offset, found_items, expected_result",
"start, stop, page_num, page_size, page_offset, "
"found_items, expected_result",
[
(1, 150, 1, 15, None, 15, (1, 15, 10, 149, False, 0)),
(1, 151, 1, 15, None, 15, (1, 15, 10, 150, False, 0)),
(1, 152, 1, 15, None, 15, (1, 15, 10, 150, True, 0)),
(1, 150, 11, 15, None, 0, (11, 15, 0, 0, False, 150)),
(1, 151, 5, 10, None, 10, (5, 10, 10, 100, True, 40)),
(1, 151, 6, 10, None, 10, (6, 10, 10, 100, False, 50)),
(1, 151, 7, 10, None, 10, (7, 10, 9, 90, False, 60)),
(1, 152, 1, 15, None, 15, (1, 15, 10, 151, True, 0)),
(1, 150, 11, 15, None, 0, (11, 15, 0, 149, False, 150)),
(1, 151, 5, 10, None, 10, (5, 10, 10, 150, True, 40)),
(1, 151, 6, 10, None, 10, (6, 10, 10, 150, False, 50)),
(1, 151, 7, 10, None, 10, (7, 10, 9, 150, False, 60)),
],
)
def test_make_pagination_max_count(
Expand Down Expand Up @@ -181,11 +182,7 @@ def test_paginate_uii_compatible_format(
start_slice_num = page_offset
stop_slice_num = page_offset + page_size

if page_offset + page_size > len(user_instances_to_create):
expected_total = len(user_instances_to_create) - page_offset
else:
expected_total = page_size

expected_total = len(user_instances_to_create)
paginated_data = paginate(query.all(), pag)

assert paginated_data == Page(
Expand Down
6 changes: 1 addition & 5 deletions lib/filter_lib/tests/test_query_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,7 @@ def test_form_query_uii_compatible_format(
start_slice_num = page_offset
stop_slice_num = page_offset + page_size

if page_offset + page_size > len(user_instances_to_create):
expected_total = len(user_instances_to_create) - page_offset
else:
expected_total = page_size

expected_total = len(user_instances_to_create)
assert (
query.all() == user_instances_to_create[start_slice_num:stop_slice_num]
)
Expand Down
Loading