1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
from __future__ import annotations
from litestar.testing import TestClient
def test_dto_data_nested_data_create_instance_app() -> None:
from docs.examples.data_transfer_objects.factory.providing_values_for_nested_data import app
with TestClient(app) as client:
response = client.post(
"/person",
json={
"name": "John",
"age": 30,
"address": {"street": "Fake Street"},
},
)
assert response.status_code == 201
assert response.json() == {
"id": 1,
"name": "John",
"age": 30,
"address": {"id": 2, "street": "Fake Street"},
}
def test_patch_requests_app() -> None:
from docs.examples.data_transfer_objects.factory.patch_requests import app
with TestClient(app) as client:
response = client.patch(
"/person/f32ff2ce-e32f-4537-9dc0-26e7599f1380",
json={"name": "Peter Pan"},
)
assert response.status_code == 200
assert response.json() == {
"id": "f32ff2ce-e32f-4537-9dc0-26e7599f1380",
"name": "Peter Pan",
"age": 40,
}
def test_exclude_fields_app() -> None:
from docs.examples.data_transfer_objects.factory.excluding_fields import app
with TestClient(app) as client:
response = client.post(
"/users",
json={"name": "Litestar User", "password": "xyz", "created_at": "2023-04-24T00:00:00Z"},
)
assert response.status_code == 201
assert response.json() == {
"created_at": "0001-01-01T00:00:00",
"address": {"city": "Anytown", "state": "NY", "zip": "12345"},
"pets": [{"name": "Fido"}, {"name": "Spot"}],
"name": "Litestar User",
}
def test_include_fields_app() -> None:
from docs.examples.data_transfer_objects.factory.included_fields import app
with TestClient(app) as client:
response = client.post(
"/users",
json={"name": "Litestar User", "password": "xyz", "created_at": "2023-04-24T00:00:00Z"},
)
assert response.status_code == 201
assert response.json() == {
"address": {"street": "123 Main St"},
"pets": [{"name": "Fido"}, {"name": "Spot"}],
}
def test_enveloped_return_data_app() -> None:
from docs.examples.data_transfer_objects.factory.enveloping_return_data import app
with TestClient(app) as client:
response = client.get("/users")
assert response.status_code == 200
assert response.json() == {
"count": 1,
"data": [{"id": 1, "name": "Litestar User"}],
}
def test_paginated_return_data_app() -> None:
from docs.examples.data_transfer_objects.factory.paginated_return_data import app
with TestClient(app) as client:
response = client.get("/users")
assert response.status_code == 200
assert response.json() == {
"page_size": 10,
"total_pages": 1,
"current_page": 1,
"items": [{"id": 1, "name": "Litestar User"}],
}
def test_response_return_data_app() -> None:
from docs.examples.data_transfer_objects.factory.response_return_data import app
with TestClient(app) as client:
response = client.get("/users")
assert response.status_code == 200
assert response.json() == {"id": 1, "name": "Litestar User"}
assert response.headers["X-Total-Count"] == "1"
def test_unknown_fields() -> None:
from docs.examples.data_transfer_objects.factory.unknown_fields import app
with TestClient(app) as client:
response = client.post("/users", json={"id": "1", "name": "Peter"})
assert response.status_code == 400
|