diff --git a/lib/filter_lib/src/pagination.py b/lib/filter_lib/src/pagination.py index b587cc311..af3d53b53 100644 --- a/lib/filter_lib/src/pagination.py +++ b/lib/filter_lib/src/pagination.py @@ -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) @@ -46,7 +49,7 @@ def make_pagination( page_num, page_size, min_pages_left, - total_results_count, + total_records, has_more, page_offset, ) diff --git a/lib/filter_lib/tests/test_pagination.py b/lib/filter_lib/tests/test_pagination.py index ab227813e..b2fa9727f 100644 --- a/lib/filter_lib/tests/test_pagination.py +++ b/lib/filter_lib/tests/test_pagination.py @@ -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( @@ -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( diff --git a/lib/filter_lib/tests/test_query_modifier.py b/lib/filter_lib/tests/test_query_modifier.py index 83c1cacb2..756f6c714 100644 --- a/lib/filter_lib/tests/test_query_modifier.py +++ b/lib/filter_lib/tests/test_query_modifier.py @@ -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] )