File: test_annotated.py

package info (click to toggle)
django-ninja 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,280 kB
  • sloc: python: 15,956; javascript: 1,689; makefile: 39; sh: 25
file content (206 lines) | stat: -rw-r--r-- 6,462 bytes parent folder | download | duplicates (2)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from typing import List

from typing_extensions import Annotated
from util import pydantic_ref_fix

from ninja import Body, Cookie, Form, Header, NinjaAPI, Path, Query, Schema
from ninja.testing import TestClient

api = NinjaAPI()


class FormData(Schema):
    x: int
    y: float


class Payload(Schema):
    t: int
    p: str


@api.post("/multi/{p}")
def multi_op(
    request,
    q: Annotated[str, Query(description="Query param")],
    p: Annotated[int, Path(description="Path param")],
    f: Annotated[FormData, Form(description="Form params")],
    c: Annotated[str, Cookie(description="Cookie params")],
):
    return {"q": q, "p": p, "f": f.dict(), "c": c}


@api.post("/query_list")
def query_list(
    request,
    q: Annotated[List[str], Query(description="User ID")],
):
    return {"q": q}


@api.post("/headers")
def headers(request, h: Annotated[str, Header()] = "some-default"):
    return {"h": h}


@api.post("/body")
def body_op(
    request, payload: Annotated[Payload, Body(examples=[{"t": 42, "p": "test"}])]
):
    return {"payload": payload}


client = TestClient(api)


def test_multi_op():
    response = client.post("/multi/42?q=1", data={"x": 1, "y": 2}, COOKIES={"c": "3"})
    assert response.status_code == 200, response.content
    assert response.json() == {
        "q": "1",
        "p": 42,
        "f": {"x": 1, "y": 2.0},
        "c": "3",
    }


def test_query_list():
    response = client.post("/query_list?q=1&q=2")
    assert response.status_code == 200, response.content
    assert response.json() == {"q": ["1", "2"]}


def test_body_op():
    response = client.post("/body", json={"t": 42, "p": "test"})
    assert response.status_code == 200, response.content
    assert response.json() == {"payload": {"p": "test", "t": 42}}


def test_headers():
    response = client.post("/headers", headers={"h": "test"})
    assert response.status_code == 200, response.content
    assert response.json() == {"h": "test"}


def test_openapi_schema():
    schema = api.get_openapi_schema()["paths"]
    print(schema)
    assert schema == {
        "/api/multi/{p}": {
            "post": {
                "operationId": "test_annotated_multi_op",
                "summary": "Multi Op",
                "parameters": [
                    {
                        "in": "query",
                        "name": "q",
                        "schema": {
                            "description": "Query param",
                            "title": "Q",
                            "type": "string",
                        },
                        "required": True,
                        "description": "Query param",
                    },
                    {
                        "in": "path",
                        "name": "p",
                        "schema": {
                            "description": "Path param",
                            "title": "P",
                            "type": "integer",
                        },
                        "required": True,
                        "description": "Path param",
                    },
                    {
                        "in": "cookie",
                        "name": "c",
                        "schema": {
                            "description": "Cookie params",
                            "title": "C",
                            "type": "string",
                        },
                        "required": True,
                        "description": "Cookie params",
                    },
                ],
                "responses": {200: {"description": "OK"}},
                "requestBody": {
                    "content": {
                        "application/x-www-form-urlencoded": {
                            "schema": {
                                "title": "FormParams",
                                "type": "object",
                                "properties": {
                                    "x": {"title": "X", "type": "integer"},
                                    "y": {"title": "Y", "type": "number"},
                                },
                                "required": ["x", "y"],
                            }
                        }
                    },
                    "required": True,
                },
            }
        },
        "/api/query_list": {
            "post": {
                "operationId": "test_annotated_query_list",
                "summary": "Query List",
                "parameters": [
                    {
                        "in": "query",
                        "name": "q",
                        "schema": {
                            "description": "User ID",
                            "items": {"type": "string"},
                            "title": "Q",
                            "type": "array",
                        },
                        "required": True,
                        "description": "User ID",
                    }
                ],
                "responses": {200: {"description": "OK"}},
            }
        },
        "/api/headers": {
            "post": {
                "operationId": "test_annotated_headers",
                "summary": "Headers",
                "parameters": [
                    {
                        "in": "header",
                        "name": "h",
                        "schema": {
                            "default": "some-default",
                            "title": "H",
                            "type": "string",
                        },
                        "required": False,
                    }
                ],
                "responses": {200: {"description": "OK"}},
            }
        },
        "/api/body": {
            "post": {
                "operationId": "test_annotated_body_op",
                "summary": "Body Op",
                "parameters": [],
                "responses": {200: {"description": "OK"}},
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": pydantic_ref_fix({
                                "$ref": "#/components/schemas/Payload",
                                "examples": [{"p": "test", "t": 42}],
                            })
                        }
                    },
                    "required": True,
                },
            }
        },
    }