Coverage for tests / test_tutorial / test_fastapi / test_session_with_dependency / 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( 1jklmnopqr

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

22 ) 

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

24 mod.engine = create_engine( 1jklmnopqr

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

26 ) 

27 return mod 1jklmnopqr

28 

29 

30def test_tutorial(module: ModuleType): 1jklmnopqr

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

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

33 hero2_data = { 1abcdefghi

34 "name": "Spider-Boy", 

35 "secret_name": "Pedro Parqueador", 

36 "id": 9000, 

37 } 

38 hero3_data = { 1abcdefghi

39 "name": "Rusty-Man", 

40 "secret_name": "Tommy Sharp", 

41 "age": 48, 

42 } 

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

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

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

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

47 hero2 = response.json() 1abcdefghi

48 hero2_id = hero2["id"] 1abcdefghi

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

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

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

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

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

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

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

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

57 data = response.json() 1abcdefghi

58 assert len(data) == 3 1abcdefghi

59 response = client.patch( 1abcdefghi

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

61 ) 

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

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

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

65 

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

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

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

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

70 data = response.json() 1abcdefghi

71 assert len(data) == 2 1abcdefghi

72 

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

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

75 

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

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

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

79 "openapi": "3.1.0", 

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

81 "paths": { 

82 "/heroes/": { 

83 "get": { 

84 "summary": "Read Heroes", 

85 "operationId": "read_heroes_heroes__get", 

86 "parameters": [ 

87 { 

88 "required": False, 

89 "schema": { 

90 "title": "Offset", 

91 "type": "integer", 

92 "default": 0, 

93 }, 

94 "name": "offset", 

95 "in": "query", 

96 }, 

97 { 

98 "required": False, 

99 "schema": { 

100 "title": "Limit", 

101 "maximum": 100.0, 

102 "type": "integer", 

103 "default": 100, 

104 }, 

105 "name": "limit", 

106 "in": "query", 

107 }, 

108 ], 

109 "responses": { 

110 "200": { 

111 "description": "Successful Response", 

112 "content": { 

113 "application/json": { 

114 "schema": { 

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

116 "type": "array", 

117 "items": { 

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

119 }, 

120 } 

121 } 

122 }, 

123 }, 

124 "422": { 

125 "description": "Validation Error", 

126 "content": { 

127 "application/json": { 

128 "schema": { 

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

130 } 

131 } 

132 }, 

133 }, 

134 }, 

135 }, 

136 "post": { 

137 "summary": "Create Hero", 

138 "operationId": "create_hero_heroes__post", 

139 "requestBody": { 

140 "content": { 

141 "application/json": { 

142 "schema": { 

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

144 } 

145 } 

146 }, 

147 "required": True, 

148 }, 

149 "responses": { 

150 "200": { 

151 "description": "Successful Response", 

152 "content": { 

153 "application/json": { 

154 "schema": { 

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

156 } 

157 } 

158 }, 

159 }, 

160 "422": { 

161 "description": "Validation Error", 

162 "content": { 

163 "application/json": { 

164 "schema": { 

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

166 } 

167 } 

168 }, 

169 }, 

170 }, 

171 }, 

172 }, 

173 "/heroes/{hero_id}": { 

174 "get": { 

175 "summary": "Read Hero", 

176 "operationId": "read_hero_heroes__hero_id__get", 

177 "parameters": [ 

178 { 

179 "required": True, 

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

181 "name": "hero_id", 

182 "in": "path", 

183 } 

184 ], 

185 "responses": { 

186 "200": { 

187 "description": "Successful Response", 

188 "content": { 

189 "application/json": { 

190 "schema": { 

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

192 } 

193 } 

194 }, 

195 }, 

196 "422": { 

197 "description": "Validation Error", 

198 "content": { 

199 "application/json": { 

200 "schema": { 

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

202 } 

203 } 

204 }, 

205 }, 

206 }, 

207 }, 

208 "delete": { 

209 "summary": "Delete Hero", 

210 "operationId": "delete_hero_heroes__hero_id__delete", 

211 "parameters": [ 

212 { 

213 "required": True, 

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

215 "name": "hero_id", 

216 "in": "path", 

217 } 

218 ], 

219 "responses": { 

220 "200": { 

221 "description": "Successful Response", 

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

223 }, 

224 "422": { 

225 "description": "Validation Error", 

226 "content": { 

227 "application/json": { 

228 "schema": { 

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

230 } 

231 } 

232 }, 

233 }, 

234 }, 

235 }, 

236 "patch": { 

237 "summary": "Update Hero", 

238 "operationId": "update_hero_heroes__hero_id__patch", 

239 "parameters": [ 

240 { 

241 "required": True, 

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

243 "name": "hero_id", 

244 "in": "path", 

245 } 

246 ], 

247 "requestBody": { 

248 "content": { 

249 "application/json": { 

250 "schema": { 

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

252 } 

253 } 

254 }, 

255 "required": True, 

256 }, 

257 "responses": { 

258 "200": { 

259 "description": "Successful Response", 

260 "content": { 

261 "application/json": { 

262 "schema": { 

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

264 } 

265 } 

266 }, 

267 }, 

268 "422": { 

269 "description": "Validation Error", 

270 "content": { 

271 "application/json": { 

272 "schema": { 

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

274 } 

275 } 

276 }, 

277 }, 

278 }, 

279 }, 

280 }, 

281 }, 

282 "components": { 

283 "schemas": { 

284 "HTTPValidationError": { 

285 "title": "HTTPValidationError", 

286 "type": "object", 

287 "properties": { 

288 "detail": { 

289 "title": "Detail", 

290 "type": "array", 

291 "items": { 

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

293 }, 

294 } 

295 }, 

296 }, 

297 "HeroCreate": { 

298 "title": "HeroCreate", 

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

300 "type": "object", 

301 "properties": { 

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

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

304 "age": { 

305 "title": "Age", 

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

307 }, 

308 }, 

309 }, 

310 "HeroPublic": { 

311 "title": "HeroPublic", 

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

313 "type": "object", 

314 "properties": { 

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

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

317 "age": { 

318 "title": "Age", 

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

320 }, 

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

322 }, 

323 }, 

324 "HeroUpdate": { 

325 "title": "HeroUpdate", 

326 "type": "object", 

327 "properties": { 

328 "name": { 

329 "title": "Name", 

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

331 }, 

332 "secret_name": { 

333 "title": "Secret Name", 

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

335 }, 

336 "age": { 

337 "title": "Age", 

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

339 }, 

340 }, 

341 }, 

342 "ValidationError": { 

343 "title": "ValidationError", 

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

345 "type": "object", 

346 "properties": { 

347 "loc": { 

348 "title": "Location", 

349 "type": "array", 

350 "items": { 

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

352 }, 

353 }, 

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

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

356 }, 

357 }, 

358 } 

359 }, 

360 }