Skip to content

Commit

Permalink
test: Refactor and add test for paginated_return_data.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kedod committed Apr 30, 2024
1 parent d5196ed commit 652c43f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from datetime import datetime

from sqlalchemy.orm import Mapped
from sqlalchemy.orm import DeclarativeBase, Mapped

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

from .my_lib import Base

class Base(CommonTableAttributes, UUIDPrimaryKey, DeclarativeBase): ...


class User(Base):
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 @@ -352,9 +352,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,26-40

.. dropdown:: Full code

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

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,
}

0 comments on commit 652c43f

Please sign in to comment.