-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add tests for dto factory doc examples (#3453)
* test: Add tests for dto factory doc examples --------- Co-authored-by: kedod <kedod>
- Loading branch information
Showing
14 changed files
with
117 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from uuid import UUID | ||
from dataclasses import dataclass, field | ||
from uuid import UUID, uuid4 | ||
|
||
from litestar import Litestar, post | ||
from litestar.dto import DataclassDTO, DTOConfig | ||
|
||
|
||
@dataclass | ||
class Person: | ||
id: UUID | ||
class User: | ||
name: str | ||
email: str | ||
age: int | ||
id: UUID = field(default_factory=uuid4) | ||
|
||
|
||
class WriteDTO(DataclassDTO[Person]): | ||
class UserWriteDTO(DataclassDTO[User]): | ||
"""Don't allow client to set the id.""" | ||
|
||
config = DTOConfig(exclude={"id"}) | ||
|
@@ -23,12 +24,12 @@ class WriteDTO(DataclassDTO[Person]): | |
# We need a dto for the handler to parse the request data per the configuration, however, | ||
# we don't need a return DTO as we are returning a dataclass, and Litestar already knows | ||
# how to serialize dataclasses. | ||
@post("/person", dto=WriteDTO, return_dto=None, sync_to_thread=False) | ||
def create_person(data: Person) -> Person: | ||
"""Create a person.""" | ||
@post("/users", dto=UserWriteDTO, return_dto=None, sync_to_thread=False) | ||
def create_user(data: User) -> User: | ||
"""Create an user.""" | ||
return data | ||
|
||
|
||
app = Litestar(route_handlers=[create_person]) | ||
app = Litestar(route_handlers=[create_user]) | ||
|
||
# run: /person -H "Content-Type: application/json" -d '{"name":"Peter","age":41}' | ||
# run: /users -H "Content-Type: application/json" -d '{"name":"Peter","email": "[email protected]", "age":41}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from uuid import UUID, uuid4 | ||
|
||
|
@@ -8,24 +6,25 @@ | |
|
||
|
||
@dataclass | ||
class Person: | ||
id: UUID | ||
class User: | ||
name: str | ||
email: str | ||
age: int | ||
id: UUID | ||
|
||
|
||
class WriteDTO(DataclassDTO[Person]): | ||
class UserWriteDTO(DataclassDTO[User]): | ||
"""Don't allow client to set the id.""" | ||
|
||
config = DTOConfig(exclude={"id"}) | ||
|
||
|
||
@post("/person", dto=WriteDTO, return_dto=None, sync_to_thread=False) | ||
def create_person(data: DTOData[Person]) -> Person: | ||
"""Create a person.""" | ||
@post("/users", dto=UserWriteDTO, return_dto=None, sync_to_thread=False) | ||
def create_user(data: DTOData[User]) -> User: | ||
"""Create an user.""" | ||
return data.create_instance(id=uuid4()) | ||
|
||
|
||
app = Litestar(route_handlers=[create_person]) | ||
app = Litestar(route_handlers=[create_user]) | ||
|
||
# run: /person -H "Content-Type: application/json" -d '{"name":"Peter","age":41}' | ||
# run: /users -H "Content-Type: application/json" -d '{"name":"Peter", "email": "[email protected]", "age":41}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 19 additions & 3 deletions
22
docs/examples/data_transfer_objects/overriding_implicit_return_dto.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
from litestar import post | ||
from dataclasses import dataclass, field | ||
from uuid import UUID, uuid4 | ||
|
||
from .models import User, UserDTO | ||
from litestar import Litestar, post | ||
from litestar.dto import DataclassDTO | ||
|
||
|
||
@post(dto=UserDTO, return_dto=None) | ||
@dataclass | ||
class User: | ||
name: str | ||
email: str | ||
age: int | ||
id: UUID = field(default_factory=uuid4) | ||
|
||
|
||
UserDTO = DataclassDTO[User] | ||
|
||
|
||
@post(dto=UserDTO, return_dto=None, sync_to_thread=False) | ||
def create_user(data: User) -> bytes: | ||
return data.name.encode(encoding="utf-8") | ||
|
||
|
||
app = Litestar([create_user]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
14 changes: 14 additions & 0 deletions
14
tests/examples/test_data_transfer_objects/test_factory/test_dto_data_problem_statement.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from unittest.mock import ANY | ||
|
||
from litestar.status_codes import HTTP_201_CREATED | ||
from litestar.testing.client import TestClient | ||
|
||
|
||
def test_create_user(user_data: dict) -> None: | ||
from docs.examples.data_transfer_objects.factory.dto_data_problem_statement import app | ||
|
||
with TestClient(app=app) as client: | ||
response = client.post("/users", json=user_data) | ||
|
||
assert response.status_code == HTTP_201_CREATED | ||
assert response.json() == {"id": ANY, "name": "Mr Sunglass", "email": "[email protected]", "age": 30} |
13 changes: 13 additions & 0 deletions
13
tests/examples/test_data_transfer_objects/test_factory/test_dto_data_usage.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from unittest.mock import ANY | ||
|
||
from litestar.testing import TestClient | ||
|
||
|
||
def test_create_user(user_data) -> None: | ||
from docs.examples.data_transfer_objects.factory.dto_data_usage import app | ||
|
||
with TestClient(app) as client: | ||
response = client.post("/users", json=user_data) | ||
|
||
assert response.status_code == 201 | ||
assert response.json() == {"id": ANY, "name": "Mr Sunglass", "email": "[email protected]", "age": 30} |
12 changes: 12 additions & 0 deletions
12
tests/examples/test_data_transfer_objects/test_factory/test_leading_underscore_private.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from litestar.status_codes import HTTP_201_CREATED | ||
from litestar.testing.client import TestClient | ||
|
||
|
||
def test_create_underscored_value() -> None: | ||
from docs.examples.data_transfer_objects.factory.leading_underscore_private import app | ||
|
||
with TestClient(app=app) as client: | ||
response = client.post("/", json={"this_will": "stay", "_this_will": "go_away!"}) | ||
|
||
assert response.status_code == HTTP_201_CREATED | ||
assert response.json() == {"this_will": "stay"} |
12 changes: 12 additions & 0 deletions
12
...mples/test_data_transfer_objects/test_factory/test_leading_underscore_private_override.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from litestar.status_codes import HTTP_201_CREATED | ||
from litestar.testing.client import TestClient | ||
|
||
|
||
def test_create_underscored_field() -> None: | ||
from docs.examples.data_transfer_objects.factory.leading_underscore_private_override import app | ||
|
||
with TestClient(app=app) as client: | ||
response = client.post("/", json={"this_will": "stay", "_this_will": "not_go_away!"}) | ||
|
||
assert response.status_code == HTTP_201_CREATED | ||
assert response.json() == {"this_will": "stay", "_this_will": "not_go_away!"} |
8 changes: 8 additions & 0 deletions
8
tests/examples/test_data_transfer_objects/test_factory/test_type_checking.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import pytest | ||
|
||
from litestar.exceptions.dto_exceptions import InvalidAnnotationException | ||
|
||
|
||
def test_should_raise_error_on_route_registration() -> None: | ||
with pytest.raises(InvalidAnnotationException): | ||
from docs.examples.data_transfer_objects.factory.type_checking import app # noqa: F401 |
12 changes: 12 additions & 0 deletions
12
tests/examples/test_data_transfer_objects/test_overriding_implicit_return_dto.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from litestar.status_codes import HTTP_201_CREATED | ||
from litestar.testing.client import TestClient | ||
|
||
|
||
def test_create_user(user_data: dict) -> None: | ||
from docs.examples.data_transfer_objects.overriding_implicit_return_dto import app | ||
|
||
with TestClient(app=app) as client: | ||
response = client.post("/", json=user_data) | ||
|
||
assert response.status_code == HTTP_201_CREATED | ||
assert response.content == b"Mr Sunglass" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters