File: test_tutorial.py

package info (click to toggle)
litestar 2.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,568 kB
  • sloc: python: 70,588; makefile: 254; javascript: 104; sh: 60
file content (164 lines) | stat: -rw-r--r-- 6,148 bytes parent folder | download
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from __future__ import annotations

from litestar.testing.client import TestClient


def test_initial_pattern_app():
    from docs.examples.data_transfer_objects.factory.tutorial.initial_pattern import app

    with TestClient(app=app) as client:
        response = client.get("/person/peter")
    assert response.status_code == 200
    assert response.json() == {"name": "peter", "age": 30, "email": "email_of_peter@example.com"}


def test_simple_dto_exclude():
    from docs.examples.data_transfer_objects.factory.tutorial.simple_dto_exclude import app

    with TestClient(app=app) as client:
        response = client.get("/person/peter")
    assert response.status_code == 200
    assert response.json() == {"name": "peter", "age": 30}


def test_nested_exclude():
    from docs.examples.data_transfer_objects.factory.tutorial.nested_exclude import app

    with TestClient(app=app) as client:
        response = client.get("/person/peter")
    assert response.status_code == 200
    assert response.json() == {"name": "peter", "age": 30, "address": {"city": "Cityville", "country": "Countryland"}}


def test_nested_collection_exclude():
    from docs.examples.data_transfer_objects.factory.tutorial.nested_collection_exclude import app

    with TestClient(app=app) as client:
        response = client.get("/person/peter")
    assert response.status_code == 200
    assert response.json() == {
        "name": "peter",
        "age": 30,
        "address": {"city": "Cityville", "country": "Countryland"},
        "children": [{"name": "Child1", "age": 10}, {"name": "Child2", "age": 8}],
    }


def test_max_nested_depth():
    from docs.examples.data_transfer_objects.factory.tutorial.max_nested_depth import app

    with TestClient(app=app) as client:
        response = client.get("/person/peter")
    assert response.status_code == 200
    assert response.json() == {
        "name": "peter",
        "age": 30,
        "address": {"city": "Cityville", "country": "Countryland"},
        "children": [{"name": "Child1", "age": 10, "children": []}, {"name": "Child2", "age": 8, "children": []}],
    }


def test_explicit_field_renaming():
    from docs.examples.data_transfer_objects.factory.tutorial.explicit_field_renaming import app

    with TestClient(app=app) as client:
        response = client.get("/person/peter")
    assert response.status_code == 200
    assert response.json() == {
        "name": "peter",
        "age": 30,
        "location": {"city": "Cityville", "country": "Countryland"},
        "children": [{"name": "Child1", "age": 10}, {"name": "Child2", "age": 8}],
    }


def test_field_renaming_strategy():
    from docs.examples.data_transfer_objects.factory.tutorial.field_renaming_strategy import app

    with TestClient(app=app) as client:
        response = client.get("/person/peter")
    assert response.status_code == 200
    assert response.json() == {
        "NAME": "peter",
        "AGE": 30,
        "ADDRESS": {"CITY": "Cityville", "COUNTRY": "Countryland"},
        "CHILDREN": [{"NAME": "Child1", "AGE": 10}, {"NAME": "Child2", "AGE": 8}],
    }


def test_simple_receiving_data():
    from docs.examples.data_transfer_objects.factory.tutorial.simple_receiving_data import app

    with TestClient(app=app) as client:
        response = client.post("/person", json={"name": "peter", "age": 40, "email": "email_of_peter@example.com"})
    assert response.status_code == 201
    assert response.json() == {"name": "peter", "age": 40}


def test_read_only_fields():
    from docs.examples.data_transfer_objects.factory.tutorial.read_only_fields_error import app

    with TestClient(app=app) as client:
        response = client.post("/person", json={"name": "peter", "age": 40, "email": "email_of_peter@example.com"})

    assert response.status_code == 500


def test_dto_data():
    from docs.examples.data_transfer_objects.factory.tutorial.dto_data import app

    with TestClient(app=app) as client:
        response = client.post("/person", json={"name": "peter", "age": 40, "email": "email_of_peter@example.com"})

    assert response.status_code == 201
    assert response.json() == {"id": 1, "name": "peter", "age": 40}


def test_put_handler():
    from docs.examples.data_transfer_objects.factory.tutorial.put_handlers import app

    with TestClient(app=app) as client:
        response = client.put("/person/1", json={"name": "peter", "age": 50, "email": "email_of_peter@example.com"})

    assert response.status_code == 200
    assert response.json() == {"id": 1, "name": "peter", "age": 50}


def test_patch_handler():
    from docs.examples.data_transfer_objects.factory.tutorial.patch_handlers import app

    with TestClient(app=app) as client:
        response = client.patch("/person/1", json={"name": "peter"})

    assert response.status_code == 200
    assert response.json() == {"id": 1, "name": "peter", "age": 50}


def test_multiple_handlers():
    from docs.examples.data_transfer_objects.factory.tutorial.multiple_handlers import app

    with TestClient(app=app) as client:
        response = client.put("/person/1", json={"name": "peter", "age": 50, "email": "email_of_peter@example.com"})
    assert response.status_code == 200

    with TestClient(app=app) as client:
        response = client.patch("/person/1", json={"name": "peter"})
    assert response.status_code == 200

    with TestClient(app=app) as client:
        response = client.post("/person", json={"name": "peter", "age": 40, "email": "email_of_peter@example.com"})
    assert response.status_code == 201


def test_controller():
    from docs.examples.data_transfer_objects.factory.tutorial.controller import app

    with TestClient(app=app) as client:
        response = client.put("/person/1", json={"name": "peter", "age": 50, "email": "email_of_peter@example.com"})
        assert response.status_code == 200

        response = client.patch("/person/1", json={"name": "peter"})
        assert response.status_code == 200

        response = client.post("/person", json={"name": "peter", "age": 40, "email": "email_of_peter@example.com"})
        assert response.status_code == 201