Coverage for tests / test_request_param_model_by_alias.py: 100%

51 statements  

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

1from dirty_equals import IsPartialDict 1abcd

2from fastapi import Cookie, FastAPI, Header, Query 1abcd

3from fastapi.testclient import TestClient 1abcd

4from pydantic import BaseModel, Field 1abcd

5 

6app = FastAPI() 1abcd

7 

8 

9class Model(BaseModel): 1abcd

10 param: str = Field(alias="param_alias") 1abcd

11 

12 

13@app.get("/query") 1abcd

14async def query_model(data: Model = Query()): 1abcd

15 return {"param": data.param} 1klm

16 

17 

18@app.get("/header") 1abcd

19async def header_model(data: Model = Header()): 1abcd

20 return {"param": data.param} 1nop

21 

22 

23@app.get("/cookie") 1abcd

24async def cookie_model(data: Model = Cookie()): 1abcd

25 return {"param": data.param} 1efg

26 

27 

28def test_query_model_with_alias(): 1abcd

29 client = TestClient(app) 1klm

30 response = client.get("/query", params={"param_alias": "value"}) 1klm

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

32 assert response.json() == {"param": "value"} 1klm

33 

34 

35def test_header_model_with_alias(): 1abcd

36 client = TestClient(app) 1nop

37 response = client.get("/header", headers={"param_alias": "value"}) 1nop

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

39 assert response.json() == {"param": "value"} 1nop

40 

41 

42def test_cookie_model_with_alias(): 1abcd

43 client = TestClient(app) 1efg

44 client.cookies.set("param_alias", "value") 1efg

45 response = client.get("/cookie") 1efg

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

47 assert response.json() == {"param": "value"} 1efg

48 

49 

50def test_query_model_with_alias_by_name(): 1abcd

51 client = TestClient(app) 1qrs

52 response = client.get("/query", params={"param": "value"}) 1qrs

53 assert response.status_code == 422, response.text 1qrs

54 details = response.json() 1qrs

55 assert details["detail"][0]["input"] == {"param": "value"} 1qrs

56 

57 

58def test_header_model_with_alias_by_name(): 1abcd

59 client = TestClient(app) 1tuv

60 response = client.get("/header", headers={"param": "value"}) 1tuv

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

62 details = response.json() 1tuv

63 assert details["detail"][0]["input"] == IsPartialDict({"param": "value"}) 1tuv

64 

65 

66def test_cookie_model_with_alias_by_name(): 1abcd

67 client = TestClient(app) 1hij

68 client.cookies.set("param", "value") 1hij

69 response = client.get("/cookie") 1hij

70 assert response.status_code == 422, response.text 1hij

71 details = response.json() 1hij

72 assert details["detail"][0]["input"] == {"param": "value"} 1hij