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

132 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from typing import Annotated 1abcd

2 

3import pytest 1abcd

4from dirty_equals import IsOneOf 1abcd

5from fastapi import Cookie, FastAPI 1abcd

6from fastapi.testclient import TestClient 1abcd

7from inline_snapshot import snapshot 1abcd

8from pydantic import BaseModel, Field 1abcd

9 

10app = FastAPI() 1abcd

11 

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

13# Without aliases 

14 

15 

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

17async def read_required_str(p: Annotated[str, Cookie()]): 1abcd

18 return {"p": p} 1efg

19 

20 

21class CookieModelRequiredStr(BaseModel): 1abcd

22 p: str 1abcd

23 

24 

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

26async def read_model_required_str(p: Annotated[CookieModelRequiredStr, Cookie()]): 1abcd

27 return {"p": p.p} 1efg

28 

29 

30@pytest.mark.parametrize( 1abcd

31 "path", 

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

33) 

34def test_required_str_schema(path: str): 1abcd

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

36 [ 

37 { 

38 "required": True, 

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

40 "name": "p", 

41 "in": "cookie", 

42 } 

43 ] 

44 ) 

45 

46 

47@pytest.mark.parametrize( 1abcd

48 "path", 

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

50) 

51def test_required_str_missing(path: str): 1abcd

52 client = TestClient(app) 1CDE

53 response = client.get(path) 1CDE

54 assert response.status_code == 422 1CDE

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

56 "detail": [ 

57 { 

58 "type": "missing", 

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

60 "msg": "Field required", 

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

62 } 

63 ] 

64 } 

65 

66 

67@pytest.mark.parametrize( 1abcd

68 "path", 

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

70) 

71def test_required_str(path: str): 1abcd

72 client = TestClient(app) 1efg

73 client.cookies.set("p", "hello") 1efg

74 response = client.get(path) 1efg

75 assert response.status_code == 200 1efg

76 assert response.json() == {"p": "hello"} 1efg

77 

78 

79# ===================================================================================== 

80# Alias 

81 

82 

83@app.get("/required-alias") 1abcd

84async def read_required_alias(p: Annotated[str, Cookie(alias="p_alias")]): 1abcd

85 return {"p": p} 1hij

86 

87 

88class CookieModelRequiredAlias(BaseModel): 1abcd

89 p: str = Field(alias="p_alias") 1abcd

90 

91 

92@app.get("/model-required-alias") 1abcd

93async def read_model_required_alias(p: Annotated[CookieModelRequiredAlias, Cookie()]): 1abcd

94 return {"p": p.p} 1hij

95 

96 

97@pytest.mark.parametrize( 1abcd

98 "path", 

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

100) 

101def test_required_str_alias_schema(path: str): 1abcd

102 assert app.openapi()["paths"][path]["get"]["parameters"] == snapshot( 1RST

103 [ 

104 { 

105 "required": True, 

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

107 "name": "p_alias", 

108 "in": "cookie", 

109 } 

110 ] 

111 ) 

112 

113 

114@pytest.mark.parametrize( 1abcd

115 "path", 

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

117) 

118def test_required_alias_missing(path: str): 1abcd

119 client = TestClient(app) 1FGH

120 response = client.get(path) 1FGH

121 assert response.status_code == 422 1FGH

122 assert response.json() == { 1FGH

123 "detail": [ 

124 { 

125 "type": "missing", 

126 "loc": ["cookie", "p_alias"], 

127 "msg": "Field required", 

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

129 } 

130 ] 

131 } 

132 

133 

134@pytest.mark.parametrize( 1abcd

135 "path", 

136 [ 

137 "/required-alias", 

138 "/model-required-alias", 

139 ], 

140) 

141def test_required_alias_by_name(path: str): 1abcd

142 client = TestClient(app) 1qrs

143 client.cookies.set("p", "hello") 1qrs

144 response = client.get(path) 1qrs

145 assert response.status_code == 422 1qrs

146 assert response.json() == { 1qrs

147 "detail": [ 

148 { 

149 "type": "missing", 

150 "loc": ["cookie", "p_alias"], 

151 "msg": "Field required", 

152 "input": IsOneOf( 

153 None, 

154 {"p": "hello"}, 

155 ), 

156 } 

157 ] 

158 } 

159 

160 

161@pytest.mark.parametrize( 1abcd

162 "path", 

163 [ 

164 "/required-alias", 

165 "/model-required-alias", 

166 ], 

167) 

168def test_required_alias_by_alias(path: str): 1abcd

169 client = TestClient(app) 1hij

170 client.cookies.set("p_alias", "hello") 1hij

171 response = client.get(path) 1hij

172 assert response.status_code == 200, response.text 1hij

173 assert response.json() == {"p": "hello"} 1hij

174 

175 

176# ===================================================================================== 

177# Validation alias 

178 

179 

180@app.get("/required-validation-alias") 1abcd

181def read_required_validation_alias( 1abcd

182 p: Annotated[str, Cookie(validation_alias="p_val_alias")], 

183): 

184 return {"p": p} 1klm

185 

186 

187class CookieModelRequiredValidationAlias(BaseModel): 1abcd

188 p: str = Field(validation_alias="p_val_alias") 1abcd

189 

190 

191@app.get("/model-required-validation-alias") 1abcd

192def read_model_required_validation_alias( 1abcd

193 p: Annotated[CookieModelRequiredValidationAlias, Cookie()], 

194): 

195 return {"p": p.p} 1klm

196 

197 

198@pytest.mark.parametrize( 1abcd

199 "path", 

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

201) 

202def test_required_validation_alias_schema(path: str): 1abcd

203 assert app.openapi()["paths"][path]["get"]["parameters"] == snapshot( 1UVW

204 [ 

205 { 

206 "required": True, 

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

208 "name": "p_val_alias", 

209 "in": "cookie", 

210 } 

211 ] 

212 ) 

213 

214 

215@pytest.mark.parametrize( 1abcd

216 "path", 

217 [ 

218 "/required-validation-alias", 

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

220 ], 

221) 

222def test_required_validation_alias_missing(path: str): 1abcd

223 client = TestClient(app) 1IJK

224 response = client.get(path) 1IJK

225 assert response.status_code == 422 1IJK

226 assert response.json() == { 1IJK

227 "detail": [ 

228 { 

229 "type": "missing", 

230 "loc": [ 

231 "cookie", 

232 "p_val_alias", 

233 ], 

234 "msg": "Field required", 

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

236 } 

237 ] 

238 } 

239 

240 

241@pytest.mark.parametrize( 1abcd

242 "path", 

243 [ 

244 "/required-validation-alias", 

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

246 ], 

247) 

248def test_required_validation_alias_by_name(path: str): 1abcd

249 client = TestClient(app) 1tuv

250 client.cookies.set("p", "hello") 1tuv

251 response = client.get(path) 1tuv

252 assert response.status_code == 422, response.text 1tuv

253 

254 assert response.json() == { 1tuv

255 "detail": [ 

256 { 

257 "type": "missing", 

258 "loc": ["cookie", "p_val_alias"], 

259 "msg": "Field required", 

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

261 } 

262 ] 

263 } 

264 

265 

266@pytest.mark.parametrize( 1abcd

267 "path", 

268 [ 

269 "/required-validation-alias", 

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

271 ], 

272) 

273def test_required_validation_alias_by_validation_alias(path: str): 1abcd

274 client = TestClient(app) 1klm

275 client.cookies.set("p_val_alias", "hello") 1klm

276 response = client.get(path) 1klm

277 assert response.status_code == 200, response.text 1klm

278 

279 assert response.json() == {"p": "hello"} 1klm

280 

281 

282# ===================================================================================== 

283# Alias and validation alias 

284 

285 

286@app.get("/required-alias-and-validation-alias") 1abcd

287def read_required_alias_and_validation_alias( 1abcd

288 p: Annotated[str, Cookie(alias="p_alias", validation_alias="p_val_alias")], 

289): 

290 return {"p": p} 1nop

291 

292 

293class CookieModelRequiredAliasAndValidationAlias(BaseModel): 1abcd

294 p: str = Field(alias="p_alias", validation_alias="p_val_alias") 1abcd

295 

296 

297@app.get("/model-required-alias-and-validation-alias") 1abcd

298def read_model_required_alias_and_validation_alias( 1abcd

299 p: Annotated[CookieModelRequiredAliasAndValidationAlias, Cookie()], 

300): 

301 return {"p": p.p} 1nop

302 

303 

304@pytest.mark.parametrize( 1abcd

305 "path", 

306 [ 

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

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

309 ], 

310) 

311def test_required_alias_and_validation_alias_schema(path: str): 1abcd

312 assert app.openapi()["paths"][path]["get"]["parameters"] == snapshot( 1XYZ

313 [ 

314 { 

315 "required": True, 

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

317 "name": "p_val_alias", 

318 "in": "cookie", 

319 } 

320 ] 

321 ) 

322 

323 

324@pytest.mark.parametrize( 1abcd

325 "path", 

326 [ 

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

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

329 ], 

330) 

331def test_required_alias_and_validation_alias_missing(path: str): 1abcd

332 client = TestClient(app) 1LMN

333 response = client.get(path) 1LMN

334 assert response.status_code == 422 1LMN

335 assert response.json() == { 1LMN

336 "detail": [ 

337 { 

338 "type": "missing", 

339 "loc": [ 

340 "cookie", 

341 "p_val_alias", 

342 ], 

343 "msg": "Field required", 

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

345 } 

346 ] 

347 } 

348 

349 

350@pytest.mark.parametrize( 1abcd

351 "path", 

352 [ 

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

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

355 ], 

356) 

357def test_required_alias_and_validation_alias_by_name(path: str): 1abcd

358 client = TestClient(app) 1wxy

359 client.cookies.set("p", "hello") 1wxy

360 response = client.get(path) 1wxy

361 assert response.status_code == 422 1wxy

362 

363 assert response.json() == { 1wxy

364 "detail": [ 

365 { 

366 "type": "missing", 

367 "loc": [ 

368 "cookie", 

369 "p_val_alias", 

370 ], 

371 "msg": "Field required", 

372 "input": IsOneOf( 

373 None, 

374 {"p": "hello"}, 

375 ), 

376 } 

377 ] 

378 } 

379 

380 

381@pytest.mark.parametrize( 1abcd

382 "path", 

383 [ 

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

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

386 ], 

387) 

388def test_required_alias_and_validation_alias_by_alias(path: str): 1abcd

389 client = TestClient(app) 1zAB

390 client.cookies.set("p_alias", "hello") 1zAB

391 response = client.get(path) 1zAB

392 assert response.status_code == 422 1zAB

393 

394 assert response.json() == { 1zAB

395 "detail": [ 

396 { 

397 "type": "missing", 

398 "loc": ["cookie", "p_val_alias"], 

399 "msg": "Field required", 

400 "input": IsOneOf( 

401 None, 

402 {"p_alias": "hello"}, 

403 ), 

404 } 

405 ] 

406 } 

407 

408 

409@pytest.mark.parametrize( 1abcd

410 "path", 

411 [ 

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

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

414 ], 

415) 

416def test_required_alias_and_validation_alias_by_validation_alias(path: str): 1abcd

417 client = TestClient(app) 1nop

418 client.cookies.set("p_val_alias", "hello") 1nop

419 response = client.get(path) 1nop

420 assert response.status_code == 200, response.text 1nop

421 

422 assert response.json() == {"p": "hello"} 1nop