Debian Codegraph @

tests/test_annotated.py

raw symbols SCIP
1 from typing import List
2
3 from typing_extensions import Annotated
4 from util import pydantic_ref_fix
5
6 from ninja import Body, Cookie, Form, Header, NinjaAPI, Path, Query, Schema
7 from ninja.testing import TestClient
8
9 api = NinjaAPI()
10
11
12 class FormData(Schema):
13 x: int
14 y: float
15
16
17 class Payload(Schema):
18 t: int
19 p: str
20
21
22 @api.post("/multi/{p}")
23 def multi_op(
24 request,
25 q: Annotated[str, Query(description="Query param")],
26 p: Annotated[int, Path(description="Path param")],
27 f: Annotated[FormData, Form(description="Form params")],
28 c: Annotated[str, Cookie(description="Cookie params")],
29 ):
30 return {"q": q, "p": p, "f": f.dict(), "c": c}
31
32
33 @api.post("/query_list")
34 def query_list(
35 request,
36 q: Annotated[List[str], Query(description="User ID")],
37 ):
38 return {"q": q}
39
40
41 @api.post("/headers")
42 def headers(request, h: Annotated[str, Header()] = "some-default"):
43 return {"h": h}
44
45
46 @api.post("/body")
47 def body_op(
48 request, payload: Annotated[Payload, Body(examples=[{"t": 42, "p": "test"}])]
49 ):
50 return {"payload": payload}
51
52
53 client = TestClient(api)
54
55
56 def test_multi_op():
57 response = client.post("/multi/42?q=1", data={"x": 1, "y": 2}, COOKIES={"c": "3"})
58 assert response.status_code == 200, response.content
59 assert response.json() == {
60 "q": "1",
61 "p": 42,
62 "f": {"x": 1, "y": 2.0},
63 "c": "3",
64 }
65
66
67 def test_query_list():
68 response = client.post("/query_list?q=1&q=2")
69 assert response.status_code == 200, response.content
70 assert response.json() == {"q": ["1", "2"]}
71
72
73 def test_body_op():
74 response = client.post("/body", json={"t": 42, "p": "test"})
75 assert response.status_code == 200, response.content
76 assert response.json() == {"payload": {"p": "test", "t": 42}}
77
78
79 def test_headers():
80 response = client.post("/headers", headers={"h": "test"})
81 assert response.status_code == 200, response.content
82 assert response.json() == {"h": "test"}
83
84
85 def test_openapi_schema():
86 schema = api.get_openapi_schema()["paths"]
87 print(schema)
88 assert schema == {
89 "/api/multi/{p}": {
90 "post": {
91 "operationId": "test_annotated_multi_op",
92 "summary": "Multi Op",
93 "parameters": [
94 {
95 "in": "query",
96 "name": "q",
97 "schema": {
98 "description": "Query param",
99 "title": "Q",
100 "type": "string",
101 },
102 "required": True,
103 "description": "Query param",
104 },
105 {
106 "in": "path",
107 "name": "p",
108 "schema": {
109 "description": "Path param",
110 "title": "P",
111 "type": "integer",
112 },
113 "required": True,
114 "description": "Path param",
115 },
116 {
117 "in": "cookie",
118 "name": "c",
119 "schema": {
120 "description": "Cookie params",
121 "title": "C",
122 "type": "string",
123 },
124 "required": True,
125 "description": "Cookie params",
126 },
127 ],
128 "responses": {200: {"description": "OK"}},
129 "requestBody": {
130 "content": {
131 "application/x-www-form-urlencoded": {
132 "schema": {
133 "title": "FormParams",
134 "type": "object",
135 "properties": {
136 "x": {"title": "X", "type": "integer"},
137 "y": {"title": "Y", "type": "number"},
138 },
139 "required": ["x", "y"],
140 }
141 }
142 },
143 "required": True,
144 },
145 }
146 },
147 "/api/query_list": {
148 "post": {
149 "operationId": "test_annotated_query_list",
150 "summary": "Query List",
151 "parameters": [
152 {
153 "in": "query",
154 "name": "q",
155 "schema": {
156 "description": "User ID",
157 "items": {"type": "string"},
158 "title": "Q",
159 "type": "array",
160 },
161 "required": True,
162 "description": "User ID",
163 }
164 ],
165 "responses": {200: {"description": "OK"}},
166 }
167 },
168 "/api/headers": {
169 "post": {
170 "operationId": "test_annotated_headers",
171 "summary": "Headers",
172 "parameters": [
173 {
174 "in": "header",
175 "name": "h",
176 "schema": {
177 "default": "some-default",
178 "title": "H",
179 "type": "string",
180 },
181 "required": False,
182 }
183 ],
184 "responses": {200: {"description": "OK"}},
185 }
186 },
187 "/api/body": {
188 "post": {
189 "operationId": "test_annotated_body_op",
190 "summary": "Body Op",
191 "parameters": [],
192 "responses": {200: {"description": "OK"}},
193 "requestBody": {
194 "content": {
195 "application/json": {
196 "schema": pydantic_ref_fix({
197 "$ref": "#/components/schemas/Payload",
198 "examples": [{"p": "test", "t": 42}],
199 })
200 }
201 },
202 "required": True,
203 },
204 }
205 },
206 }