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

47 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-11-09 00:02 +0000

1import importlib 1jklmnopqr

2from types import ModuleType 1jklmnopqr

3 

4import pytest 1jklmnopqr

5from dirty_equals import IsOneOf 1jklmnopqr

6from fastapi.testclient import TestClient 1jklmnopqr

7from sqlalchemy import Engine 1jklmnopqr

8 

9 

10@pytest.fixture( 1jklmnopqr

11 name="module", 

12 params=[ 

13 pytest.param("tutorial001_py310"), 

14 ], 

15) 

16def get_module(request: pytest.FixtureRequest, database_engine: Engine) -> ModuleType: 1jklmnopqr

17 mod = importlib.import_module( 1jklmnopqr

18 f"docs_src.tutorial.fastapi.session_with_dependency.{request.param}" 

19 ) 

20 mod.engine = database_engine 1jklmnopqr

21 return mod 1jklmnopqr

22 

23 

24def test_tutorial(module: ModuleType): 1jklmnopqr

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

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

27 hero2_data = { 1abcdefghi

28 "name": "Spider-Boy", 

29 "secret_name": "Pedro Parqueador", 

30 "id": 9000, 

31 } 

32 hero3_data = { 1abcdefghi

33 "name": "Rusty-Man", 

34 "secret_name": "Tommy Sharp", 

35 "age": 48, 

36 } 

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

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

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

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

41 hero2 = response.json() 1abcdefghi

42 hero2_id = hero2["id"] 1abcdefghi

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

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

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

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

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

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

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

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

51 data = response.json() 1abcdefghi

52 assert len(data) == 3 1abcdefghi

53 response = client.patch( 1abcdefghi

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

55 ) 

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

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

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

59 

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

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

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

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

64 data = response.json() 1abcdefghi

65 assert len(data) == 2 1abcdefghi

66 

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

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

69 

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

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

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

73 "openapi": "3.1.0", 

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

75 "paths": { 

76 "/heroes/": { 

77 "get": { 

78 "summary": "Read Heroes", 

79 "operationId": "read_heroes_heroes__get", 

80 "parameters": [ 

81 { 

82 "required": False, 

83 "schema": { 

84 "title": "Offset", 

85 "type": "integer", 

86 "default": 0, 

87 }, 

88 "name": "offset", 

89 "in": "query", 

90 }, 

91 { 

92 "required": False, 

93 "schema": { 

94 "title": "Limit", 

95 "maximum": 100.0, 

96 "type": "integer", 

97 "default": 100, 

98 }, 

99 "name": "limit", 

100 "in": "query", 

101 }, 

102 ], 

103 "responses": { 

104 "200": { 

105 "description": "Successful Response", 

106 "content": { 

107 "application/json": { 

108 "schema": { 

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

110 "type": "array", 

111 "items": { 

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

113 }, 

114 } 

115 } 

116 }, 

117 }, 

118 "422": { 

119 "description": "Validation Error", 

120 "content": { 

121 "application/json": { 

122 "schema": { 

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

124 } 

125 } 

126 }, 

127 }, 

128 }, 

129 }, 

130 "post": { 

131 "summary": "Create Hero", 

132 "operationId": "create_hero_heroes__post", 

133 "requestBody": { 

134 "content": { 

135 "application/json": { 

136 "schema": { 

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

138 } 

139 } 

140 }, 

141 "required": True, 

142 }, 

143 "responses": { 

144 "200": { 

145 "description": "Successful Response", 

146 "content": { 

147 "application/json": { 

148 "schema": { 

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

150 } 

151 } 

152 }, 

153 }, 

154 "422": { 

155 "description": "Validation Error", 

156 "content": { 

157 "application/json": { 

158 "schema": { 

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

160 } 

161 } 

162 }, 

163 }, 

164 }, 

165 }, 

166 }, 

167 "/heroes/{hero_id}": { 

168 "get": { 

169 "summary": "Read Hero", 

170 "operationId": "read_hero_heroes__hero_id__get", 

171 "parameters": [ 

172 { 

173 "required": True, 

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

175 "name": "hero_id", 

176 "in": "path", 

177 } 

178 ], 

179 "responses": { 

180 "200": { 

181 "description": "Successful Response", 

182 "content": { 

183 "application/json": { 

184 "schema": { 

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

186 } 

187 } 

188 }, 

189 }, 

190 "422": { 

191 "description": "Validation Error", 

192 "content": { 

193 "application/json": { 

194 "schema": { 

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

196 } 

197 } 

198 }, 

199 }, 

200 }, 

201 }, 

202 "delete": { 

203 "summary": "Delete Hero", 

204 "operationId": "delete_hero_heroes__hero_id__delete", 

205 "parameters": [ 

206 { 

207 "required": True, 

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

209 "name": "hero_id", 

210 "in": "path", 

211 } 

212 ], 

213 "responses": { 

214 "200": { 

215 "description": "Successful Response", 

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

217 }, 

218 "422": { 

219 "description": "Validation Error", 

220 "content": { 

221 "application/json": { 

222 "schema": { 

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

224 } 

225 } 

226 }, 

227 }, 

228 }, 

229 }, 

230 "patch": { 

231 "summary": "Update Hero", 

232 "operationId": "update_hero_heroes__hero_id__patch", 

233 "parameters": [ 

234 { 

235 "required": True, 

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

237 "name": "hero_id", 

238 "in": "path", 

239 } 

240 ], 

241 "requestBody": { 

242 "content": { 

243 "application/json": { 

244 "schema": { 

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

246 } 

247 } 

248 }, 

249 "required": True, 

250 }, 

251 "responses": { 

252 "200": { 

253 "description": "Successful Response", 

254 "content": { 

255 "application/json": { 

256 "schema": { 

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

258 } 

259 } 

260 }, 

261 }, 

262 "422": { 

263 "description": "Validation Error", 

264 "content": { 

265 "application/json": { 

266 "schema": { 

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

268 } 

269 } 

270 }, 

271 }, 

272 }, 

273 }, 

274 }, 

275 }, 

276 "components": { 

277 "schemas": { 

278 "HTTPValidationError": { 

279 "title": "HTTPValidationError", 

280 "type": "object", 

281 "properties": { 

282 "detail": { 

283 "title": "Detail", 

284 "type": "array", 

285 "items": { 

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

287 }, 

288 } 

289 }, 

290 }, 

291 "HeroCreate": { 

292 "title": "HeroCreate", 

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

294 "type": "object", 

295 "properties": { 

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

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

298 "age": { 

299 "title": "Age", 

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

301 }, 

302 }, 

303 }, 

304 "HeroPublic": { 

305 "title": "HeroPublic", 

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

307 "type": "object", 

308 "properties": { 

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

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

311 "age": { 

312 "title": "Age", 

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

314 }, 

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

316 }, 

317 }, 

318 "HeroUpdate": { 

319 "title": "HeroUpdate", 

320 "type": "object", 

321 "properties": { 

322 "name": { 

323 "title": "Name", 

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

325 }, 

326 "secret_name": { 

327 "title": "Secret Name", 

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

329 }, 

330 "age": { 

331 "title": "Age", 

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

333 }, 

334 }, 

335 }, 

336 "ValidationError": { 

337 "title": "ValidationError", 

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

339 "type": "object", 

340 "properties": IsOneOf( 

341 { 

342 "loc": { 

343 "title": "Location", 

344 "type": "array", 

345 "items": { 

346 "anyOf": [ 

347 {"type": "string"}, 

348 {"type": "integer"}, 

349 ] 

350 }, 

351 }, 

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

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

354 }, 

355 { 

356 "loc": { 

357 "title": "Location", 

358 "type": "array", 

359 "items": { 

360 "anyOf": [ 

361 {"type": "string"}, 

362 {"type": "integer"}, 

363 ] 

364 }, 

365 }, 

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

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

368 "ctx": {"title": "Context", "type": "object"}, 

369 "input": {"title": "Input"}, 

370 }, 

371 ), 

372 }, 

373 } 

374 }, 

375 }