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