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

45 statements  

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

1from dirty_equals import IsDict 1jefghi

2from fastapi.testclient import TestClient 1jefghi

3from sqlmodel import create_engine 1jefghi

4from sqlmodel.pool import StaticPool 1jefghi

5 

6from ....conftest import needs_py39 1jefghi

7 

8 

9@needs_py39 1jefghi

10def test_tutorial(clear_sqlmodel): 1efghi

11 from docs_src.tutorial.fastapi.session_with_dependency import ( 1abcd

12 tutorial001_py39 as mod, 

13 ) 

14 

15 mod.sqlite_url = "sqlite://" 1abcd

16 mod.engine = create_engine( 1abcd

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

18 ) 

19 

20 with TestClient(mod.app) as client: 1abcd

21 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcd

22 hero2_data = { 1abcd

23 "name": "Spider-Boy", 

24 "secret_name": "Pedro Parqueador", 

25 "id": 9000, 

26 } 

27 hero3_data = { 1abcd

28 "name": "Rusty-Man", 

29 "secret_name": "Tommy Sharp", 

30 "age": 48, 

31 } 

32 response = client.post("/heroes/", json=hero1_data) 1abcd

33 assert response.status_code == 200, response.text 1abcd

34 response = client.post("/heroes/", json=hero2_data) 1abcd

35 assert response.status_code == 200, response.text 1abcd

36 hero2 = response.json() 1abcd

37 hero2_id = hero2["id"] 1abcd

38 response = client.post("/heroes/", json=hero3_data) 1abcd

39 assert response.status_code == 200, response.text 1abcd

40 response = client.get(f"/heroes/{hero2_id}") 1abcd

41 assert response.status_code == 200, response.text 1abcd

42 response = client.get("/heroes/9000") 1abcd

43 assert response.status_code == 404, response.text 1abcd

44 response = client.get("/heroes/") 1abcd

45 assert response.status_code == 200, response.text 1abcd

46 data = response.json() 1abcd

47 assert len(data) == 3 1abcd

48 response = client.patch( 1abcd

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

50 ) 

51 assert response.status_code == 200, response.text 1abcd

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

53 assert response.status_code == 404, response.text 1abcd

54 

55 response = client.delete(f"/heroes/{hero2_id}") 1abcd

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

57 response = client.get("/heroes/") 1abcd

58 assert response.status_code == 200, response.text 1abcd

59 data = response.json() 1abcd

60 assert len(data) == 2 1abcd

61 

62 response = client.delete("/heroes/9000") 1abcd

63 assert response.status_code == 404, response.text 1abcd

64 

65 response = client.get("/openapi.json") 1abcd

66 assert response.status_code == 200, response.text 1abcd

67 assert response.json() == { 1abcd

68 "openapi": "3.1.0", 

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

70 "paths": { 

71 "/heroes/": { 

72 "get": { 

73 "summary": "Read Heroes", 

74 "operationId": "read_heroes_heroes__get", 

75 "parameters": [ 

76 { 

77 "required": False, 

78 "schema": { 

79 "title": "Offset", 

80 "type": "integer", 

81 "default": 0, 

82 }, 

83 "name": "offset", 

84 "in": "query", 

85 }, 

86 { 

87 "required": False, 

88 "schema": { 

89 "title": "Limit", 

90 "maximum": 100.0, 

91 "type": "integer", 

92 "default": 100, 

93 }, 

94 "name": "limit", 

95 "in": "query", 

96 }, 

97 ], 

98 "responses": { 

99 "200": { 

100 "description": "Successful Response", 

101 "content": { 

102 "application/json": { 

103 "schema": { 

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

105 "type": "array", 

106 "items": { 

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

108 }, 

109 } 

110 } 

111 }, 

112 }, 

113 "422": { 

114 "description": "Validation Error", 

115 "content": { 

116 "application/json": { 

117 "schema": { 

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

119 } 

120 } 

121 }, 

122 }, 

123 }, 

124 }, 

125 "post": { 

126 "summary": "Create Hero", 

127 "operationId": "create_hero_heroes__post", 

128 "requestBody": { 

129 "content": { 

130 "application/json": { 

131 "schema": { 

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

133 } 

134 } 

135 }, 

136 "required": True, 

137 }, 

138 "responses": { 

139 "200": { 

140 "description": "Successful Response", 

141 "content": { 

142 "application/json": { 

143 "schema": { 

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

145 } 

146 } 

147 }, 

148 }, 

149 "422": { 

150 "description": "Validation Error", 

151 "content": { 

152 "application/json": { 

153 "schema": { 

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

155 } 

156 } 

157 }, 

158 }, 

159 }, 

160 }, 

161 }, 

162 "/heroes/{hero_id}": { 

163 "get": { 

164 "summary": "Read Hero", 

165 "operationId": "read_hero_heroes__hero_id__get", 

166 "parameters": [ 

167 { 

168 "required": True, 

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

170 "name": "hero_id", 

171 "in": "path", 

172 } 

173 ], 

174 "responses": { 

175 "200": { 

176 "description": "Successful Response", 

177 "content": { 

178 "application/json": { 

179 "schema": { 

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

181 } 

182 } 

183 }, 

184 }, 

185 "422": { 

186 "description": "Validation Error", 

187 "content": { 

188 "application/json": { 

189 "schema": { 

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

191 } 

192 } 

193 }, 

194 }, 

195 }, 

196 }, 

197 "delete": { 

198 "summary": "Delete Hero", 

199 "operationId": "delete_hero_heroes__hero_id__delete", 

200 "parameters": [ 

201 { 

202 "required": True, 

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

204 "name": "hero_id", 

205 "in": "path", 

206 } 

207 ], 

208 "responses": { 

209 "200": { 

210 "description": "Successful Response", 

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

212 }, 

213 "422": { 

214 "description": "Validation Error", 

215 "content": { 

216 "application/json": { 

217 "schema": { 

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

219 } 

220 } 

221 }, 

222 }, 

223 }, 

224 }, 

225 "patch": { 

226 "summary": "Update Hero", 

227 "operationId": "update_hero_heroes__hero_id__patch", 

228 "parameters": [ 

229 { 

230 "required": True, 

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

232 "name": "hero_id", 

233 "in": "path", 

234 } 

235 ], 

236 "requestBody": { 

237 "content": { 

238 "application/json": { 

239 "schema": { 

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

241 } 

242 } 

243 }, 

244 "required": True, 

245 }, 

246 "responses": { 

247 "200": { 

248 "description": "Successful Response", 

249 "content": { 

250 "application/json": { 

251 "schema": { 

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

253 } 

254 } 

255 }, 

256 }, 

257 "422": { 

258 "description": "Validation Error", 

259 "content": { 

260 "application/json": { 

261 "schema": { 

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

263 } 

264 } 

265 }, 

266 }, 

267 }, 

268 }, 

269 }, 

270 }, 

271 "components": { 

272 "schemas": { 

273 "HTTPValidationError": { 

274 "title": "HTTPValidationError", 

275 "type": "object", 

276 "properties": { 

277 "detail": { 

278 "title": "Detail", 

279 "type": "array", 

280 "items": { 

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

282 }, 

283 } 

284 }, 

285 }, 

286 "HeroCreate": { 

287 "title": "HeroCreate", 

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

289 "type": "object", 

290 "properties": { 

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

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

293 "age": IsDict( 

294 { 

295 "title": "Age", 

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

297 } 

298 ) 

299 | IsDict( 

300 # TODO: remove when deprecating Pydantic v1 

301 {"title": "Age", "type": "integer"} 

302 ), 

303 }, 

304 }, 

305 "HeroPublic": { 

306 "title": "HeroPublic", 

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

308 "type": "object", 

309 "properties": { 

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

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

312 "age": IsDict( 

313 { 

314 "title": "Age", 

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

316 } 

317 ) 

318 | IsDict( 

319 # TODO: remove when deprecating Pydantic v1 

320 {"title": "Age", "type": "integer"} 

321 ), 

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

323 }, 

324 }, 

325 "HeroUpdate": { 

326 "title": "HeroUpdate", 

327 "type": "object", 

328 "properties": { 

329 "name": IsDict( 

330 { 

331 "title": "Name", 

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

333 } 

334 ) 

335 | IsDict( 

336 # TODO: remove when deprecating Pydantic v1 

337 {"title": "Name", "type": "string"} 

338 ), 

339 "secret_name": IsDict( 

340 { 

341 "title": "Secret Name", 

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

343 } 

344 ) 

345 | IsDict( 

346 # TODO: remove when deprecating Pydantic v1 

347 {"title": "Secret Name", "type": "string"} 

348 ), 

349 "age": IsDict( 

350 { 

351 "title": "Age", 

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

353 } 

354 ) 

355 | IsDict( 

356 # TODO: remove when deprecating Pydantic v1 

357 {"title": "Age", "type": "integer"} 

358 ), 

359 }, 

360 }, 

361 "ValidationError": { 

362 "title": "ValidationError", 

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

364 "type": "object", 

365 "properties": { 

366 "loc": { 

367 "title": "Location", 

368 "type": "array", 

369 "items": { 

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

371 }, 

372 }, 

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

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

375 }, 

376 }, 

377 } 

378 }, 

379 }