Debian Codegraph @

tests/test_response_multiple.py

raw symbols SCIP
1 from typing import List, Union
2
3 import pytest
4 from pydantic import ValidationError
5
6 from ninja import NinjaAPI, Schema
7 from ninja.errors import ConfigError
8 from ninja.responses import codes_2xx, codes_3xx
9 from ninja.testing import TestClient
10
11 pytestmark = pytest.mark.filterwarnings("ignore::DeprecationWarning")
12
13 api = NinjaAPI()
14
15
16 @api.get("/check_int", response={200: int})
17 def check_int(request):
18 return 200, "1"
19
20
21 @api.get("/check_int2", response={200: int})
22 def check_int2(request):
23 return 200, "str"
24
25
26 @api.get("/check_single_with_status", response=int)
27 def check_single_with_status(request, code: int):
28 return code, 1
29
30
31 @api.get("/check_response_schema", response={400: int})
32 def check_response_schema(request):
33 return 200, 1
34
35
36 @api.get("/check_no_content", response={204: None})
37 def check_no_content(request, return_code: bool):
38 if return_code:
39 return 204, None
40 return # None
41
42
43 @api.get(
44 "/check_multiple_codes",
45 response={codes_2xx: int, codes_3xx: str, ...: float},
46 )
47 def check_multiple_codes(request, code: int):
48 return code, "1"
49
50
51 class User:
52 def __init__(self, id, name, password):
53 self.id = id
54 self.name = name
55 self.password = password
56
57
58 class UserModel(Schema):
59 id: int
60 name: str
61 # skipping password output to responses
62
63
64 class ErrorModel(Schema):
65 detail: str
66
67
68 @api.get("/check_model", response={200: UserModel, 202: UserModel})
69 def check_model(request):
70 return 202, User(1, "John", "Password")
71
72
73 @api.get("/check_list_model", response={200: List[UserModel]})
74 def check_list_model(request):
75 return 200, [User(1, "John", "Password")]
76
77
78 @api.get("/check_union", response={200: Union[int, UserModel], 400: ErrorModel})
79 def check_union(request, q: int):
80 if q == 0:
81 return 200, 1
82 if q == 1:
83 return 200, User(1, "John", "Password")
84 if q == 2:
85 return 400, {"detail": "error"}
86 return "invalid"
87
88
89 client = TestClient(api)
90
91
92 @pytest.mark.parametrize(
93 "path,expected_status,expected_response",
94 [
95 ("/check_int", 200, 1),
96 ("/check_single_with_status?code=200", 200, 1),
97 ("/check_model", 202, {"id": 1, "name": "John"}), # ! the password is skipped
98 ("/check_list_model", 200, [{"id": 1, "name": "John"}]),
99 ("/check_union?q=0", 200, 1),
100 ("/check_union?q=1", 200, {"id": 1, "name": "John"}),
101 ("/check_union?q=2", 400, {"detail": "error"}),
102 ("/check_multiple_codes?code=200", 200, 1),
103 ("/check_multiple_codes?code=201", 201, 1),
104 ("/check_multiple_codes?code=202", 202, 1),
105 ("/check_multiple_codes?code=206", 206, 1),
106 ("/check_multiple_codes?code=300", 300, "1"),
107 ("/check_multiple_codes?code=308", 308, "1"),
108 ("/check_multiple_codes?code=400", 400, 1.0),
109 ("/check_multiple_codes?code=500", 500, 1.0),
110 ],
111 )
112 def test_responses(path, expected_status, expected_response):
113 response = client.get(path)
114 assert response.status_code == expected_status, response.content
115 assert response.json() == expected_response
116
117
118 def test_schema():
119 checks = [
120 ("/api/check_int", {200}),
121 ("/api/check_int2", {200}),
122 ("/api/check_single_with_status", {200}),
123 ("/api/check_response_schema", {400}),
124 ("/api/check_model", {200, 202}),
125 ("/api/check_list_model", {200}),
126 ("/api/check_union", {200, 400}),
127 ]
128 schema = api.get_openapi_schema()
129
130 # checking codes
131 for path, codes in checks:
132 responses = schema["paths"][path]["get"]["responses"]
133 responses_codes = set(responses.keys())
134 assert codes == responses_codes, f"{codes} != {responses_codes}"
135
136 # checking model
137 check_model_responses = schema["paths"]["/api/check_model"]["get"]["responses"]
138
139 assert check_model_responses == {
140 200: {
141 "content": {
142 "application/json": {
143 "schema": {"$ref": "#/components/schemas/UserModel"}
144 }
145 },
146 "description": "OK",
147 },
148 202: {
149 "content": {
150 "application/json": {
151 "schema": {"$ref": "#/components/schemas/UserModel"}
152 }
153 },
154 "description": "Accepted",
155 },
156 }
157
158
159 def test_no_content():
160 response = client.get("/check_no_content?return_code=1")
161 assert response.status_code == 204
162 assert response.content == b""
163
164 response = client.get("/check_no_content?return_code=0")
165 assert response.status_code == 204
166 assert response.content == b""
167
168 schema = api.get_openapi_schema()
169 details = schema["paths"]["/api/check_no_content"]["get"]["responses"]
170 assert details == {204: {"description": "No Content"}}
171
172
173 def test_validates():
174 with pytest.raises(ValidationError):
175 client.get("/check_int2")
176
177 with pytest.raises(ValidationError):
178 client.get("/check_union?q=3")
179
180 with pytest.raises(ConfigError):
181 client.get("/check_response_schema")
182
183 with pytest.raises(ConfigError):
184 client.get("/check_single_with_status?code=300")