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

docs: Refactor and add test for paginated_return_data.py #3456

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from sqlalchemy.orm import Mapped

from litestar import Litestar, get
from litestar.contrib.sqlalchemy.base import UUIDBase
from litestar.contrib.sqlalchemy.dto import SQLAlchemyDTO
from litestar.dto import DTOConfig
from litestar.pagination import ClassicPagination

from .my_lib import Base


class User(Base):
class User(UUIDBase):
name: Mapped[str]
password: Mapped[str]
created_at: Mapped[datetime]
Expand Down
9 changes: 7 additions & 2 deletions docs/usage/dto/1-abstract-dto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,14 @@ Working with Litestar's Pagination Types
Litestar offers paginated response wrapper types, and DTO Factory types can handle this out of the box.

.. literalinclude:: /examples/data_transfer_objects/factory/paginated_return_data.py
:caption: Paginated Return Data
:language: python
:linenos:
:lines: 9-11,22-39

.. dropdown:: Full Code (click to expand)

.. literalinclude:: /examples/data_transfer_objects/factory/paginated_return_data.py
:language: python
:emphasize-lines: 9,22-39

The DTO is defined and configured, in our example, we're excluding ``password`` and ``created_at`` fields from the final
representation of our users.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from litestar.status_codes import HTTP_200_OK
from litestar.testing.client import TestClient


def test_create_user() -> None:
from docs.examples.data_transfer_objects.factory.paginated_return_data import app

with TestClient(app=app) as client:
response = client.get("/users")

assert response.status_code == HTTP_200_OK
assert response.json() == {
"current_page": 1,
"items": [{"id": 1, "name": "Litestar User"}],
"page_size": 10,
"total_pages": 1,
}
Loading