Coverage for tests / test_tutorial / test_fastapi / test_delete / test_tutorial001.py: 100%

49 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-01-06 21:09 +0000

1import importlib 1jklmnopqr

2from types import ModuleType 1jklmnopqr

3 

4import pytest 1jklmnopqr

5from fastapi.testclient import TestClient 1jklmnopqr

6from sqlmodel import create_engine 1jklmnopqr

7from sqlmodel.pool import StaticPool 1jklmnopqr

8 

9from tests.conftest import needs_py310 1jklmnopqr

10 

11 

12@pytest.fixture( 1jklmnopqr

13 name="module", 

14 params=[ 

15 pytest.param("tutorial001_py39"), 

16 pytest.param("tutorial001_py310", marks=needs_py310), 

17 ], 

18) 

19def get_module(request: pytest.FixtureRequest) -> ModuleType: 1jklmnopqr

20 mod = importlib.import_module(f"docs_src.tutorial.fastapi.delete.{request.param}") 1jklmnopqr

21 mod.sqlite_url = "sqlite://" 1jklmnopqr

22 mod.engine = create_engine( 1jklmnopqr

23 mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool 

24 ) 

25 return mod 1jklmnopqr

26 

27 

28def test_tutorial(module: ModuleType): 1jklmnopqr

29 with TestClient(module.app) as client: 1abcdefghi

30 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcdefghi

31 hero2_data = { 1abcdefghi

32 "name": "Spider-Boy", 

33 "secret_name": "Pedro Parqueador", 

34 "id": 9000, 

35 } 

36 hero3_data = { 1abcdefghi

37 "name": "Rusty-Man", 

38 "secret_name": "Tommy Sharp", 

39 "age": 48, 

40 } 

41 response = client.post("/heroes/", json=hero1_data) 1abcdefghi

42 assert response.status_code == 200, response.text 1abcdefghi

43 response = client.post("/heroes/", json=hero2_data) 1abcdefghi

44 assert response.status_code == 200, response.text 1abcdefghi

45 hero2 = response.json() 1abcdefghi

46 hero2_id = hero2["id"] 1abcdefghi

47 response = client.post("/heroes/", json=hero3_data) 1abcdefghi

48 assert response.status_code == 200, response.text 1abcdefghi

49 response = client.get(f"/heroes/{hero2_id}") 1abcdefghi

50 assert response.status_code == 200, response.text 1abcdefghi

51 response = client.get("/heroes/9000") 1abcdefghi

52 assert response.status_code == 404, response.text 1abcdefghi

53 response = client.get("/heroes/") 1abcdefghi

54 assert response.status_code == 200, response.text 1abcdefghi

55 data = response.json() 1abcdefghi

56 assert len(data) == 3 1abcdefghi

57 response = client.patch( 1abcdefghi

58 f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"} 

59 ) 

60 assert response.status_code == 200, response.text 1abcdefghi

61 response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"}) 1abcdefghi

62 assert response.status_code == 404, response.text 1abcdefghi

63 

64 response = client.delete(f"/heroes/{hero2_id}") 1abcdefghi

65 assert response.status_code == 200, response.text 1abcdefghi

66 response = client.get("/heroes/") 1abcdefghi

67 assert response.status_code == 200, response.text 1abcdefghi

68 data = response.json() 1abcdefghi

69 assert len(data) == 2 1abcdefghi

70 

71 response = client.delete("/heroes/9000") 1abcdefghi

72 assert response.status_code == 404, response.text 1abcdefghi

73 

74 response = client.get("/openapi.json") 1abcdefghi

75 assert response.status_code == 200, response.text 1abcdefghi

76 assert response.json() == { 1abcdefghi

77 "openapi": "3.1.0", 

78 "info": {"title": "FastAPI", "version": "0.1.0"}, 

79 "paths": { 

80 "/heroes/": { 

81 "get": { 

82 "summary": "Read Heroes", 

83 "operationId": "read_heroes_heroes__get", 

84 "parameters": [ 

85 { 

86 "required": False, 

87 "schema": { 

88 "title": "Offset", 

89 "type": "integer", 

90 "default": 0, 

91 }, 

92 "name": "offset", 

93 "in": "query", 

94 }, 

95 { 

96 "required": False, 

97 "schema": { 

98 "title": "Limit", 

99 "maximum": 100.0, 

100 "type": "integer", 

101 "default": 100, 

102 }, 

103 "name": "limit", 

104 "in": "query", 

105 }, 

106 ], 

107 "responses": { 

108 "200": { 

109 "description": "Successful Response", 

110 "content": { 

111 "application/json": { 

112 "schema": { 

113 "title": "Response Read Heroes Heroes Get", 

114 "type": "array", 

115 "items": { 

116 "$ref": "#/components/schemas/HeroPublic" 

117 }, 

118 } 

119 } 

120 }, 

121 }, 

122 "422": { 

123 "description": "Validation Error", 

124 "content": { 

125 "application/json": { 

126 "schema": { 

127 "$ref": "#/components/schemas/HTTPValidationError" 

128 } 

129 } 

130 }, 

131 }, 

132 }, 

133 }, 

134 "post": { 

135 "summary": "Create Hero", 

136 "operationId": "create_hero_heroes__post", 

137 "requestBody": { 

138 "content": { 

139 "application/json": { 

140 "schema": { 

141 "$ref": "#/components/schemas/HeroCreate" 

142 } 

143 } 

144 }, 

145 "required": True, 

146 }, 

147 "responses": { 

148 "200": { 

149 "description": "Successful Response", 

150 "content": { 

151 "application/json": { 

152 "schema": { 

153 "$ref": "#/components/schemas/HeroPublic" 

154 } 

155 } 

156 }, 

157 }, 

158 "422": { 

159 "description": "Validation Error", 

160 "content": { 

161 "application/json": { 

162 "schema": { 

163 "$ref": "#/components/schemas/HTTPValidationError" 

164 } 

165 } 

166 }, 

167 }, 

168 }, 

169 }, 

170 }, 

171 "/heroes/{hero_id}": { 

172 "get": { 

173 "summary": "Read Hero", 

174 "operationId": "read_hero_heroes__hero_id__get", 

175 "parameters": [ 

176 { 

177 "required": True, 

178 "schema": {"title": "Hero Id", "type": "integer"}, 

179 "name": "hero_id", 

180 "in": "path", 

181 } 

182 ], 

183 "responses": { 

184 "200": { 

185 "description": "Successful Response", 

186 "content": { 

187 "application/json": { 

188 "schema": { 

189 "$ref": "#/components/schemas/HeroPublic" 

190 } 

191 } 

192 }, 

193 }, 

194 "422": { 

195 "description": "Validation Error", 

196 "content": { 

197 "application/json": { 

198 "schema": { 

199 "$ref": "#/components/schemas/HTTPValidationError" 

200 } 

201 } 

202 }, 

203 }, 

204 }, 

205 }, 

206 "delete": { 

207 "summary": "Delete Hero", 

208 "operationId": "delete_hero_heroes__hero_id__delete", 

209 "parameters": [ 

210 { 

211 "required": True, 

212 "schema": {"title": "Hero Id", "type": "integer"}, 

213 "name": "hero_id", 

214 "in": "path", 

215 } 

216 ], 

217 "responses": { 

218 "200": { 

219 "description": "Successful Response", 

220 "content": {"application/json": {"schema": {}}}, 

221 }, 

222 "422": { 

223 "description": "Validation Error", 

224 "content": { 

225 "application/json": { 

226 "schema": { 

227 "$ref": "#/components/schemas/HTTPValidationError" 

228 } 

229 } 

230 }, 

231 }, 

232 }, 

233 }, 

234 "patch": { 

235 "summary": "Update Hero", 

236 "operationId": "update_hero_heroes__hero_id__patch", 

237 "parameters": [ 

238 { 

239 "required": True, 

240 "schema": {"title": "Hero Id", "type": "integer"}, 

241 "name": "hero_id", 

242 "in": "path", 

243 } 

244 ], 

245 "requestBody": { 

246 "content": { 

247 "application/json": { 

248 "schema": { 

249 "$ref": "#/components/schemas/HeroUpdate" 

250 } 

251 } 

252 }, 

253 "required": True, 

254 }, 

255 "responses": { 

256 "200": { 

257 "description": "Successful Response", 

258 "content": { 

259 "application/json": { 

260 "schema": { 

261 "$ref": "#/components/schemas/HeroPublic" 

262 } 

263 } 

264 }, 

265 }, 

266 "422": { 

267 "description": "Validation Error", 

268 "content": { 

269 "application/json": { 

270 "schema": { 

271 "$ref": "#/components/schemas/HTTPValidationError" 

272 } 

273 } 

274 }, 

275 }, 

276 }, 

277 }, 

278 }, 

279 }, 

280 "components": { 

281 "schemas": { 

282 "HTTPValidationError": { 

283 "title": "HTTPValidationError", 

284 "type": "object", 

285 "properties": { 

286 "detail": { 

287 "title": "Detail", 

288 "type": "array", 

289 "items": { 

290 "$ref": "#/components/schemas/ValidationError" 

291 }, 

292 } 

293 }, 

294 }, 

295 "HeroCreate": { 

296 "title": "HeroCreate", 

297 "required": ["name", "secret_name"], 

298 "type": "object", 

299 "properties": { 

300 "name": {"title": "Name", "type": "string"}, 

301 "secret_name": {"title": "Secret Name", "type": "string"}, 

302 "age": { 

303 "title": "Age", 

304 "anyOf": [{"type": "integer"}, {"type": "null"}], 

305 }, 

306 }, 

307 }, 

308 "HeroPublic": { 

309 "title": "HeroPublic", 

310 "required": ["name", "secret_name", "id"], 

311 "type": "object", 

312 "properties": { 

313 "name": {"title": "Name", "type": "string"}, 

314 "secret_name": {"title": "Secret Name", "type": "string"}, 

315 "age": { 

316 "title": "Age", 

317 "anyOf": [{"type": "integer"}, {"type": "null"}], 

318 }, 

319 "id": {"title": "Id", "type": "integer"}, 

320 }, 

321 }, 

322 "HeroUpdate": { 

323 "title": "HeroUpdate", 

324 "type": "object", 

325 "properties": { 

326 "name": { 

327 "title": "Name", 

328 "anyOf": [{"type": "string"}, {"type": "null"}], 

329 }, 

330 "secret_name": { 

331 "title": "Secret Name", 

332 "anyOf": [{"type": "string"}, {"type": "null"}], 

333 }, 

334 "age": { 

335 "title": "Age", 

336 "anyOf": [{"type": "integer"}, {"type": "null"}], 

337 }, 

338 }, 

339 }, 

340 "ValidationError": { 

341 "title": "ValidationError", 

342 "required": ["loc", "msg", "type"], 

343 "type": "object", 

344 "properties": { 

345 "loc": { 

346 "title": "Location", 

347 "type": "array", 

348 "items": { 

349 "anyOf": [{"type": "string"}, {"type": "integer"}] 

350 }, 

351 }, 

352 "msg": {"title": "Message", "type": "string"}, 

353 "type": {"title": "Error Type", "type": "string"}, 

354 }, 

355 }, 

356 } 

357 }, 

358 }