Coverage for tests/test_tutorial/test_fastapi/test_limit_and_offset/test_tutorial001.py: 100%
48 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
1from dirty_equals import IsDict 1ghijkl
2from fastapi.testclient import TestClient 1ghijkl
3from sqlmodel import create_engine 1ghijkl
4from sqlmodel.pool import StaticPool 1ghijkl
7def test_tutorial(clear_sqlmodel): 1ghijkl
8 from docs_src.tutorial.fastapi.limit_and_offset import tutorial001 as mod 1fabcde
10 mod.sqlite_url = "sqlite://" 1fabcde
11 mod.engine = create_engine( 1fabcde
12 mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
13 )
15 with TestClient(mod.app) as client: 1fabcde
16 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1fabcde
17 hero2_data = { 1abcde
18 "name": "Spider-Boy",
19 "secret_name": "Pedro Parqueador",
20 "id": 9000,
21 }
22 hero3_data = { 1abcde
23 "name": "Rusty-Man",
24 "secret_name": "Tommy Sharp",
25 "age": 48,
26 }
27 response = client.post("/heroes/", json=hero1_data) 1fabcde
28 assert response.status_code == 200, response.text 1fabcde
29 response = client.post("/heroes/", json=hero2_data) 1fabcde
30 assert response.status_code == 200, response.text 1fabcde
31 hero2 = response.json() 1fabcde
32 hero_id = hero2["id"] 1fabcde
33 response = client.post("/heroes/", json=hero3_data) 1fabcde
34 assert response.status_code == 200, response.text 1fabcde
35 response = client.get(f"/heroes/{hero_id}") 1fabcde
36 assert response.status_code == 200, response.text 1fabcde
37 response = client.get("/heroes/9000") 1fabcde
38 assert response.status_code == 404, response.text 1fabcde
40 response = client.get("/heroes/") 1fabcde
41 assert response.status_code == 200, response.text 1fabcde
42 data = response.json() 1fabcde
43 assert len(data) == 3 1fabcde
45 response = client.get("/heroes/", params={"limit": 2}) 1fabcde
46 assert response.status_code == 200, response.text 1fabcde
47 data = response.json() 1fabcde
48 assert len(data) == 2 1fabcde
49 assert data[0]["name"] == hero1_data["name"] 1fabcde
50 assert data[1]["name"] == hero2_data["name"] 1fabcde
52 response = client.get("/heroes/", params={"offset": 1}) 1fabcde
53 assert response.status_code == 200, response.text 1fabcde
54 data = response.json() 1fabcde
55 assert len(data) == 2 1fabcde
56 assert data[0]["name"] == hero2_data["name"] 1fabcde
57 assert data[1]["name"] == hero3_data["name"] 1fabcde
59 response = client.get("/heroes/", params={"offset": 1, "limit": 1}) 1fabcde
60 assert response.status_code == 200, response.text 1fabcde
61 data = response.json() 1fabcde
62 assert len(data) == 1 1fabcde
63 assert data[0]["name"] == hero2_data["name"] 1fabcde
65 response = client.get("/openapi.json") 1fabcde
66 assert response.status_code == 200, response.text 1fabcde
67 assert response.json() == { 1fabcde
68 "openapi": "3.1.0",
69 "info": {"title": "FastAPI", "version": "0.1.0"},
70 "paths": {
71 "/heroes/": {
72 "get": {
73 "summary": "Read Heroes",
74 "operationId": "read_heroes_heroes__get",
75 "parameters": [
76 {
77 "required": False,
78 "schema": {
79 "title": "Offset",
80 "type": "integer",
81 "default": 0,
82 },
83 "name": "offset",
84 "in": "query",
85 },
86 {
87 "required": False,
88 "schema": {
89 "title": "Limit",
90 "maximum": 100.0,
91 "type": "integer",
92 "default": 100,
93 },
94 "name": "limit",
95 "in": "query",
96 },
97 ],
98 "responses": {
99 "200": {
100 "description": "Successful Response",
101 "content": {
102 "application/json": {
103 "schema": {
104 "title": "Response Read Heroes Heroes Get",
105 "type": "array",
106 "items": {
107 "$ref": "#/components/schemas/HeroPublic"
108 },
109 }
110 }
111 },
112 },
113 "422": {
114 "description": "Validation Error",
115 "content": {
116 "application/json": {
117 "schema": {
118 "$ref": "#/components/schemas/HTTPValidationError"
119 }
120 }
121 },
122 },
123 },
124 },
125 "post": {
126 "summary": "Create Hero",
127 "operationId": "create_hero_heroes__post",
128 "requestBody": {
129 "content": {
130 "application/json": {
131 "schema": {
132 "$ref": "#/components/schemas/HeroCreate"
133 }
134 }
135 },
136 "required": True,
137 },
138 "responses": {
139 "200": {
140 "description": "Successful Response",
141 "content": {
142 "application/json": {
143 "schema": {
144 "$ref": "#/components/schemas/HeroPublic"
145 }
146 }
147 },
148 },
149 "422": {
150 "description": "Validation Error",
151 "content": {
152 "application/json": {
153 "schema": {
154 "$ref": "#/components/schemas/HTTPValidationError"
155 }
156 }
157 },
158 },
159 },
160 },
161 },
162 "/heroes/{hero_id}": {
163 "get": {
164 "summary": "Read Hero",
165 "operationId": "read_hero_heroes__hero_id__get",
166 "parameters": [
167 {
168 "required": True,
169 "schema": {"title": "Hero Id", "type": "integer"},
170 "name": "hero_id",
171 "in": "path",
172 }
173 ],
174 "responses": {
175 "200": {
176 "description": "Successful Response",
177 "content": {
178 "application/json": {
179 "schema": {
180 "$ref": "#/components/schemas/HeroPublic"
181 }
182 }
183 },
184 },
185 "422": {
186 "description": "Validation Error",
187 "content": {
188 "application/json": {
189 "schema": {
190 "$ref": "#/components/schemas/HTTPValidationError"
191 }
192 }
193 },
194 },
195 },
196 }
197 },
198 },
199 "components": {
200 "schemas": {
201 "HTTPValidationError": {
202 "title": "HTTPValidationError",
203 "type": "object",
204 "properties": {
205 "detail": {
206 "title": "Detail",
207 "type": "array",
208 "items": {
209 "$ref": "#/components/schemas/ValidationError"
210 },
211 }
212 },
213 },
214 "HeroCreate": {
215 "title": "HeroCreate",
216 "required": ["name", "secret_name"],
217 "type": "object",
218 "properties": {
219 "name": {"title": "Name", "type": "string"},
220 "secret_name": {"title": "Secret Name", "type": "string"},
221 "age": IsDict(
222 {
223 "title": "Age",
224 "anyOf": [{"type": "integer"}, {"type": "null"}],
225 }
226 )
227 | IsDict(
228 # TODO: remove when deprecating Pydantic v1
229 {"title": "Age", "type": "integer"}
230 ),
231 },
232 },
233 "HeroPublic": {
234 "title": "HeroPublic",
235 "required": ["name", "secret_name", "id"],
236 "type": "object",
237 "properties": {
238 "name": {"title": "Name", "type": "string"},
239 "secret_name": {"title": "Secret Name", "type": "string"},
240 "age": IsDict(
241 {
242 "title": "Age",
243 "anyOf": [{"type": "integer"}, {"type": "null"}],
244 }
245 )
246 | IsDict(
247 # TODO: remove when deprecating Pydantic v1
248 {"title": "Age", "type": "integer"}
249 ),
250 "id": {"title": "Id", "type": "integer"},
251 },
252 },
253 "ValidationError": {
254 "title": "ValidationError",
255 "required": ["loc", "msg", "type"],
256 "type": "object",
257 "properties": {
258 "loc": {
259 "title": "Location",
260 "type": "array",
261 "items": {
262 "anyOf": [{"type": "string"}, {"type": "integer"}]
263 },
264 },
265 "msg": {"title": "Message", "type": "string"},
266 "type": {"title": "Error Type", "type": "string"},
267 },
268 },
269 }
270 },
271 }