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