Skip to content

Commit

Permalink
fix(filter_lib): total field for pagination now show true total
Browse files Browse the repository at this point in the history
  • Loading branch information
cakeinsauce committed Sep 25, 2024
1 parent 7da7bdb commit f1065a7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
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
20 changes: 9 additions & 11 deletions lib/filter_lib/tests/test_pagination.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from pydantic import ValidationError

from src.pagination import (
PaginationParams,
_calculate_num_pages,
Expand Down Expand Up @@ -60,15 +61,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 +183,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
7 changes: 2 additions & 5 deletions lib/filter_lib/tests/test_query_modifier.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from src.enum_generator import get_enum_from_orm
from src.pagination import PaginationParams
from src.query_modificator import (
Expand Down Expand Up @@ -533,11 +534,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
1 change: 1 addition & 0 deletions lib/filter_lib/tests/test_schema_generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from pydantic import ValidationError

from src.schema_generator import (
Page,
Pagination,
Expand Down
5 changes: 3 additions & 2 deletions lib/filter_lib/usage_example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

from db_example import Address, User, get_db
from fastapi import Depends, FastAPI
from pydantic import BaseModel
from sqlalchemy.orm import Session

from filter_lib import ( # type: ignore
Page,
create_filter_model,
form_query,
map_request_to_filter,
paginate,
)
from pydantic import BaseModel
from sqlalchemy.orm import Session

app = FastAPI()

Expand Down

0 comments on commit f1065a7

Please sign in to comment.