Coverage for tests / test_request_params / test_query / test_required_str.py: 100%

124 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from typing import Annotated 1adbc

2 

3import pytest 1adbc

4from dirty_equals import IsOneOf 1adbc

5from fastapi import FastAPI, Query 1adbc

6from fastapi.testclient import TestClient 1adbc

7from inline_snapshot import snapshot 1adbc

8from pydantic import BaseModel, Field 1adbc

9 

10app = FastAPI() 1adbc

11 

12# ===================================================================================== 

13# Without aliases 

14 

15 

16@app.get("/required-str") 1adbc

17async def read_required_str(p: str): 1adbc

18 return {"p": p} 1efgh

19 

20 

21class QueryModelRequiredStr(BaseModel): 1adbc

22 p: str 1abc

23 

24 

25@app.get("/model-required-str") 1adbc

26async def read_model_required_str(p: Annotated[QueryModelRequiredStr, Query()]): 1adbc

27 return {"p": p.p} 2i ibj k

28 

29 

30@pytest.mark.parametrize( 1adbc

31 "path", 

32 ["/required-str", "/model-required-str"], 

33) 

34def test_required_str_schema(path: str): 1adbc

35 assert app.openapi()["paths"][path]["get"]["parameters"] == snapshot( 2jbkblbmbnbobpb

36 [ 

37 { 

38 "required": True, 

39 "schema": {"title": "P", "type": "string"}, 

40 "name": "p", 

41 "in": "query", 

42 } 

43 ] 

44 ) 

45 

46 

47@pytest.mark.parametrize( 1adbc

48 "path", 

49 ["/required-str", "/model-required-str"], 

50) 

51def test_required_str_missing(path: str): 1adbc

52 client = TestClient(app) 1JKLMNOPQ

53 response = client.get(path) 1JKLMNOPQ

54 assert response.status_code == 422 1JKLMNOPQ

55 assert response.json() == { 1JKLMNOPQ

56 "detail": [ 

57 { 

58 "type": "missing", 

59 "loc": ["query", "p"], 

60 "msg": "Field required", 

61 "input": IsOneOf(None, {}), 

62 } 

63 ] 

64 } 

65 

66 

67@pytest.mark.parametrize( 1adbc

68 "path", 

69 ["/required-str", "/model-required-str"], 

70) 

71def test_required_str(path: str): 1adbc

72 client = TestClient(app) 1iefjgkh

73 response = client.get(f"{path}?p=hello") 1iefjgkh

74 assert response.status_code == 200 1iefjgkh

75 assert response.json() == {"p": "hello"} 1iefjgkh

76 

77 

78# ===================================================================================== 

79# Alias 

80 

81 

82@app.get("/required-alias") 1adbc

83async def read_required_alias(p: Annotated[str, Query(alias="p_alias")]): 1adbc

84 return {"p": p} 1lmno

85 

86 

87class QueryModelRequiredAlias(BaseModel): 1adbc

88 p: str = Field(alias="p_alias") 1adbc

89 

90 

91@app.get("/model-required-alias") 1adbc

92async def read_model_required_alias(p: Annotated[QueryModelRequiredAlias, Query()]): 1adbc

93 return {"p": p.p} 1pqrs

94 

95 

96@pytest.mark.parametrize( 1adbc

97 "path", 

98 ["/required-alias", "/model-required-alias"], 

99) 

100def test_required_str_alias_schema(path: str): 1adbc

101 assert app.openapi()["paths"][path]["get"]["parameters"] == snapshot( 2qbrbsbtbubvbwbxb

102 [ 

103 { 

104 "required": True, 

105 "schema": {"title": "P Alias", "type": "string"}, 

106 "name": "p_alias", 

107 "in": "query", 

108 } 

109 ] 

110 ) 

111 

112 

113@pytest.mark.parametrize( 1adbc

114 "path", 

115 ["/required-alias", "/model-required-alias"], 

116) 

117def test_required_alias_missing(path: str): 1adbc

118 client = TestClient(app) 1RSTUVWXY

119 response = client.get(path) 1RSTUVWXY

120 assert response.status_code == 422 1RSTUVWXY

121 assert response.json() == { 1RSTUVWXY

122 "detail": [ 

123 { 

124 "type": "missing", 

125 "loc": ["query", "p_alias"], 

126 "msg": "Field required", 

127 "input": IsOneOf(None, {}), 

128 } 

129 ] 

130 } 

131 

132 

133@pytest.mark.parametrize( 1adbc

134 "path", 

135 [ 

136 "/required-alias", 

137 "/model-required-alias", 

138 ], 

139) 

140def test_required_alias_by_name(path: str): 1adbc

141 client = TestClient(app) 1Z012345

142 response = client.get(f"{path}?p=hello") 1Z012345

143 assert response.status_code == 422 1Z012345

144 assert response.json() == { 1Z012345

145 "detail": [ 

146 { 

147 "type": "missing", 

148 "loc": ["query", "p_alias"], 

149 "msg": "Field required", 

150 "input": IsOneOf( 

151 None, 

152 {"p": "hello"}, 

153 ), 

154 } 

155 ] 

156 } 

157 

158 

159@pytest.mark.parametrize( 1adbc

160 "path", 

161 [ 

162 "/required-alias", 

163 "/model-required-alias", 

164 ], 

165) 

166def test_required_alias_by_alias(path: str): 1adbc

167 client = TestClient(app) 1plqmrnso

168 response = client.get(f"{path}?p_alias=hello") 1plqmrnso

169 assert response.status_code == 200, response.text 1plqmrnso

170 assert response.json() == {"p": "hello"} 1plqmrnso

171 

172 

173# ===================================================================================== 

174# Validation alias 

175 

176 

177@app.get("/required-validation-alias") 1adbc

178def read_required_validation_alias( 1adbc

179 p: Annotated[str, Query(validation_alias="p_val_alias")], 

180): 

181 return {"p": p} 1tuvw

182 

183 

184class QueryModelRequiredValidationAlias(BaseModel): 1adbc

185 p: str = Field(validation_alias="p_val_alias") 1adbc

186 

187 

188@app.get("/model-required-validation-alias") 1adbc

189def read_model_required_validation_alias( 1adbc

190 p: Annotated[QueryModelRequiredValidationAlias, Query()], 

191): 

192 return {"p": p.p} 1xyzA

193 

194 

195@pytest.mark.parametrize( 1adbc

196 "path", 

197 ["/required-validation-alias", "/model-required-validation-alias"], 

198) 

199def test_required_validation_alias_schema(path: str): 1adbc

200 assert app.openapi()["paths"][path]["get"]["parameters"] == snapshot( 2ybzbAbBbCbDbEbFb

201 [ 

202 { 

203 "required": True, 

204 "schema": {"title": "P Val Alias", "type": "string"}, 

205 "name": "p_val_alias", 

206 "in": "query", 

207 } 

208 ] 

209 ) 

210 

211 

212@pytest.mark.parametrize( 1adbc

213 "path", 

214 [ 

215 "/required-validation-alias", 

216 "/model-required-validation-alias", 

217 ], 

218) 

219def test_required_validation_alias_missing(path: str): 1adbc

220 client = TestClient(app) 16789!#$

221 response = client.get(path) 16789!#$

222 assert response.status_code == 422 16789!#$

223 assert response.json() == { 16789!#$

224 "detail": [ 

225 { 

226 "type": "missing", 

227 "loc": [ 

228 "query", 

229 "p_val_alias", 

230 ], 

231 "msg": "Field required", 

232 "input": IsOneOf(None, {}), 

233 } 

234 ] 

235 } 

236 

237 

238@pytest.mark.parametrize( 1adbc

239 "path", 

240 [ 

241 "/required-validation-alias", 

242 "/model-required-validation-alias", 

243 ], 

244) 

245def test_required_validation_alias_by_name(path: str): 1adbc

246 client = TestClient(app) 1%'()*+,-

247 response = client.get(f"{path}?p=hello") 1%'()*+,-

248 assert response.status_code == 422, response.text 1%'()*+,-

249 

250 assert response.json() == { 1%'()*+,-

251 "detail": [ 

252 { 

253 "type": "missing", 

254 "loc": ["query", "p_val_alias"], 

255 "msg": "Field required", 

256 "input": IsOneOf(None, {"p": "hello"}), 

257 } 

258 ] 

259 } 

260 

261 

262@pytest.mark.parametrize( 1adbc

263 "path", 

264 [ 

265 "/required-validation-alias", 

266 "/model-required-validation-alias", 

267 ], 

268) 

269def test_required_validation_alias_by_validation_alias(path: str): 1adbc

270 client = TestClient(app) 1xtyuzvAw

271 response = client.get(f"{path}?p_val_alias=hello") 1xtyuzvAw

272 assert response.status_code == 200, response.text 1xtyuzvAw

273 

274 assert response.json() == {"p": "hello"} 1xtyuzvAw

275 

276 

277# ===================================================================================== 

278# Alias and validation alias 

279 

280 

281@app.get("/required-alias-and-validation-alias") 1adbc

282def read_required_alias_and_validation_alias( 1adbc

283 p: Annotated[str, Query(alias="p_alias", validation_alias="p_val_alias")], 

284): 

285 return {"p": p} 1BCDE

286 

287 

288class QueryModelRequiredAliasAndValidationAlias(BaseModel): 1adbc

289 p: str = Field(alias="p_alias", validation_alias="p_val_alias") 1adbc

290 

291 

292@app.get("/model-required-alias-and-validation-alias") 1adbc

293def read_model_required_alias_and_validation_alias( 1adbc

294 p: Annotated[QueryModelRequiredAliasAndValidationAlias, Query()], 

295): 

296 return {"p": p.p} 1FGHI

297 

298 

299@pytest.mark.parametrize( 1adbc

300 "path", 

301 [ 

302 "/required-alias-and-validation-alias", 

303 "/model-required-alias-and-validation-alias", 

304 ], 

305) 

306def test_required_alias_and_validation_alias_schema(path: str): 1adbc

307 assert app.openapi()["paths"][path]["get"]["parameters"] == snapshot( 2GbHbIbJbKbLbMbNb

308 [ 

309 { 

310 "required": True, 

311 "schema": {"title": "P Val Alias", "type": "string"}, 

312 "name": "p_val_alias", 

313 "in": "query", 

314 } 

315 ] 

316 ) 

317 

318 

319@pytest.mark.parametrize( 1adbc

320 "path", 

321 [ 

322 "/required-alias-and-validation-alias", 

323 "/model-required-alias-and-validation-alias", 

324 ], 

325) 

326def test_required_alias_and_validation_alias_missing(path: str): 1adbc

327 client = TestClient(app) 1./:;=?@[

328 response = client.get(path) 1./:;=?@[

329 assert response.status_code == 422 1./:;=?@[

330 assert response.json() == { 1./:;=?@[

331 "detail": [ 

332 { 

333 "type": "missing", 

334 "loc": [ 

335 "query", 

336 "p_val_alias", 

337 ], 

338 "msg": "Field required", 

339 "input": IsOneOf(None, {}), 

340 } 

341 ] 

342 } 

343 

344 

345@pytest.mark.parametrize( 1adbc

346 "path", 

347 [ 

348 "/required-alias-and-validation-alias", 

349 "/model-required-alias-and-validation-alias", 

350 ], 

351) 

352def test_required_alias_and_validation_alias_by_name(path: str): 1adbc

353 client = TestClient(app) 1]^_`{|}~

354 response = client.get(f"{path}?p=hello") 1]^_`{|}~

355 assert response.status_code == 422 1]^_`{|}~

356 

357 assert response.json() == { 1]^_`{|}~

358 "detail": [ 

359 { 

360 "type": "missing", 

361 "loc": [ 

362 "query", 

363 "p_val_alias", 

364 ], 

365 "msg": "Field required", 

366 "input": IsOneOf( 

367 None, 

368 {"p": "hello"}, 

369 ), 

370 } 

371 ] 

372 } 

373 

374 

375@pytest.mark.parametrize( 1adbc

376 "path", 

377 [ 

378 "/required-alias-and-validation-alias", 

379 "/model-required-alias-and-validation-alias", 

380 ], 

381) 

382def test_required_alias_and_validation_alias_by_alias(path: str): 1adbc

383 client = TestClient(app) 2abbbcbdbebfbgbhb

384 response = client.get(f"{path}?p_alias=hello") 2abbbcbdbebfbgbhb

385 assert response.status_code == 422 2abbbcbdbebfbgbhb

386 

387 assert response.json() == { 2abbbcbdbebfbgbhb

388 "detail": [ 

389 { 

390 "type": "missing", 

391 "loc": ["query", "p_val_alias"], 

392 "msg": "Field required", 

393 "input": IsOneOf( 

394 None, 

395 {"p_alias": "hello"}, 

396 ), 

397 } 

398 ] 

399 } 

400 

401 

402@pytest.mark.parametrize( 1adbc

403 "path", 

404 [ 

405 "/required-alias-and-validation-alias", 

406 "/model-required-alias-and-validation-alias", 

407 ], 

408) 

409def test_required_alias_and_validation_alias_by_validation_alias(path: str): 1adbc

410 client = TestClient(app) 1FBGCHDIE

411 response = client.get(f"{path}?p_val_alias=hello") 1FBGCHDIE

412 assert response.status_code == 200, response.text 1FBGCHDIE

413 

414 assert response.json() == {"p": "hello"} 1FBGCHDIE