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: default page or items_per_page for when paginating using the get on list #177

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
6 changes: 5 additions & 1 deletion fastcrud/endpoint/endpoint_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ async def endpoint(
),
filters: dict = Depends(dynamic_filters),
):
is_paginated = (page is not None) and (items_per_page is not None)
is_paginated = (page is not None) or (items_per_page is not None)
has_offset_limit = (offset is not None) and (limit is not None)

if is_paginated and has_offset_limit:
Expand All @@ -361,6 +361,10 @@ async def endpoint(
)

if is_paginated:
if not page:
page = 1
if not items_per_page:
items_per_page = 10
offset = compute_offset(page=page, items_per_page=items_per_page) # type: ignore
limit = items_per_page
crud_data = await self.crud.get_multi(
Expand Down
66 changes: 66 additions & 0 deletions tests/sqlmodel/endpoint/test_get_paginated.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,69 @@ async def test_read_items_with_pagination_and_filters(

for item in data["data"]:
assert item["name"] == name


@pytest.mark.asyncio
async def test_read_items_with_only_items_per_page_on_pagination(
client: TestClient, async_session, test_model, test_data
):
for data in test_data:
new_item = test_model(**data)
async_session.add(new_item)
await async_session.commit()

items_per_page = 1

response = client.get(f"/test/get_multi?&itemsPerPage={items_per_page}")

assert response.status_code == 200

data = response.json()

assert "data" in data
assert "total_count" in data
assert "page" in data
assert "items_per_page" in data
assert "has_more" in data

assert len(data["data"]) > 0
assert len(data["data"]) <= items_per_page

test_item = test_data[0]
assert any(item["name"] == test_item["name"] for item in data["data"])

assert data["page"] == 1
assert data["items_per_page"] == items_per_page


@pytest.mark.asyncio
async def test_read_items_with_only_page_on_pagination(
client: TestClient, async_session, test_model, test_data
):
for data in test_data:
new_item = test_model(**data)
async_session.add(new_item)
await async_session.commit()

page = 1

response = client.get(f"/test/get_multi?&page={page}")

assert response.status_code == 200

data = response.json()

assert "data" in data
assert "total_count" in data
assert "page" in data
assert "items_per_page" in data
assert "has_more" in data

assert len(data["data"]) > 0
assert len(data["data"]) <= 10

test_item = test_data[0]
assert any(item["name"] == test_item["name"] for item in data["data"])

assert data["page"] == page
assert data["items_per_page"] == 10
Loading