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-04-06 01:24 +0000

1from typing import Annotated 1adbc

2 

3import pytest 1adbc

4from dirty_equals import IsOneOf 1adbc

5from fastapi import Cookie, FastAPI 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: Annotated[str, Cookie()]): 1adbc

18 return {"p": p} 1efgh

19 

20 

21class CookieModelRequiredStr(BaseModel): 1adbc

22 p: str 1abc

23 

24 

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

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

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

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( 2lbmbnbobpbqbrbsb

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( 1adbc

48 "path", 

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

50) 

51def test_required_str_missing(path: str): 1adbc

52 client = TestClient(app) 1)*+,-./:

53 response = client.get(path) 1)*+,-./:

54 assert response.status_code == 422 1)*+,-./:

55 assert response.json() == { 1)*+,-./:

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( 1adbc

68 "path", 

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

70) 

71def test_required_str(path: str): 1adbc

72 client = TestClient(app) 1iejfkglh

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

74 response = client.get(path) 1iejfkglh

75 assert response.status_code == 200 1iejfkglh

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

77 

78 

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

80# Alias 

81 

82 

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

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

85 return {"p": p} 1mnop

86 

87 

88class CookieModelRequiredAlias(BaseModel): 1adbc

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

90 

91 

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

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

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

95 

96 

97@pytest.mark.parametrize( 1adbc

98 "path", 

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

100) 

101def test_required_str_alias_schema(path: str): 1adbc

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

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( 1adbc

115 "path", 

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

117) 

118def test_required_alias_missing(path: str): 1adbc

119 client = TestClient(app) 1;=?@[]^_

120 response = client.get(path) 1;=?@[]^_

121 assert response.status_code == 422 1;=?@[]^_

122 assert response.json() == { 1;=?@[]^_

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( 1adbc

135 "path", 

136 [ 

137 "/required-alias", 

138 "/model-required-alias", 

139 ], 

140) 

141def test_required_alias_by_name(path: str): 1adbc

142 client = TestClient(app) 1KLMNOPQR

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

144 response = client.get(path) 1KLMNOPQR

145 assert response.status_code == 422 1KLMNOPQR

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

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( 1adbc

162 "path", 

163 [ 

164 "/required-alias", 

165 "/model-required-alias", 

166 ], 

167) 

168def test_required_alias_by_alias(path: str): 1adbc

169 client = TestClient(app) 1qmrnsotp

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

171 response = client.get(path) 1qmrnsotp

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

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

174 

175 

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

177# Validation alias 

178 

179 

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

181def read_required_validation_alias( 1adbc

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

183): 

184 return {"p": p} 1uvwx

185 

186 

187class CookieModelRequiredValidationAlias(BaseModel): 1adbc

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

189 

190 

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

192def read_model_required_validation_alias( 1adbc

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

194): 

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

196 

197 

198@pytest.mark.parametrize( 1adbc

199 "path", 

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

201) 

202def test_required_validation_alias_schema(path: str): 1adbc

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

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( 1adbc

216 "path", 

217 [ 

218 "/required-validation-alias", 

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

220 ], 

221) 

222def test_required_validation_alias_missing(path: str): 1adbc

223 client = TestClient(app) 2` { | } ~ abbbcb

224 response = client.get(path) 2` { | } ~ abbbcb

225 assert response.status_code == 422 2` { | } ~ abbbcb

226 assert response.json() == { 2` { | } ~ abbbcb

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( 1adbc

242 "path", 

243 [ 

244 "/required-validation-alias", 

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

246 ], 

247) 

248def test_required_validation_alias_by_name(path: str): 1adbc

249 client = TestClient(app) 1STUVWXYZ

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

251 response = client.get(path) 1STUVWXYZ

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

253 

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

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( 1adbc

267 "path", 

268 [ 

269 "/required-validation-alias", 

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

271 ], 

272) 

273def test_required_validation_alias_by_validation_alias(path: str): 1adbc

274 client = TestClient(app) 1yuzvAwBx

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

276 response = client.get(path) 1yuzvAwBx

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

278 

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

280 

281 

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

283# Alias and validation alias 

284 

285 

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

287def read_required_alias_and_validation_alias( 1adbc

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

289): 

290 return {"p": p} 1CDEF

291 

292 

293class CookieModelRequiredAliasAndValidationAlias(BaseModel): 1adbc

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

295 

296 

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

298def read_model_required_alias_and_validation_alias( 1adbc

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

300): 

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

302 

303 

304@pytest.mark.parametrize( 1adbc

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): 1adbc

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

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( 1adbc

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): 1adbc

332 client = TestClient(app) 2dbebfbgbhbibjbkb

333 response = client.get(path) 2dbebfbgbhbibjbkb

334 assert response.status_code == 422 2dbebfbgbhbibjbkb

335 assert response.json() == { 2dbebfbgbhbibjbkb

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( 1adbc

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): 1adbc

358 client = TestClient(app) 101234567

359 client.cookies.set("p", "hello") 101234567

360 response = client.get(path) 101234567

361 assert response.status_code == 422 101234567

362 

363 assert response.json() == { 101234567

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( 1adbc

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): 1adbc

389 client = TestClient(app) 189!#$%'(

390 client.cookies.set("p_alias", "hello") 189!#$%'(

391 response = client.get(path) 189!#$%'(

392 assert response.status_code == 422 189!#$%'(

393 

394 assert response.json() == { 189!#$%'(

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( 1adbc

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): 1adbc

417 client = TestClient(app) 1GCHDIEJF

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

419 response = client.get(path) 1GCHDIEJF

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

421 

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