Coverage for tests/test_request_param_model_by_alias.py: 100%
55 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
1from dirty_equals import IsPartialDict 1abcdefg
2from fastapi import Cookie, FastAPI, Header, Query 1abcdefg
3from fastapi._compat import PYDANTIC_V2 1abcdefg
4from fastapi.testclient import TestClient 1abcdefg
5from pydantic import BaseModel, Field 1abcdefg
7app = FastAPI() 1abcdefg
10class Model(BaseModel): 1abcdefg
11 param: str = Field(alias="param_alias") 1abcdefg
14@app.get("/query") 1abcdefg
15async def query_model(data: Model = Query()): 1abcdefg
16 return {"param": data.param} 1JKLMNOP
19@app.get("/header") 1abcdefg
20async def header_model(data: Model = Header()): 1abcdefg
21 return {"param": data.param} 1QRSTUVW
24@app.get("/cookie") 1abcdefg
25async def cookie_model(data: Model = Cookie()): 1abcdefg
26 return {"param": data.param} 1opqrstu
29def test_query_model_with_alias(): 1abcdefg
30 client = TestClient(app) 1JKLMNOP
31 response = client.get("/query", params={"param_alias": "value"}) 1JKLMNOP
32 assert response.status_code == 200, response.text 1JKLMNOP
33 assert response.json() == {"param": "value"} 1JKLMNOP
36def test_header_model_with_alias(): 1abcdefg
37 client = TestClient(app) 1QRSTUVW
38 response = client.get("/header", headers={"param_alias": "value"}) 1QRSTUVW
39 assert response.status_code == 200, response.text 1QRSTUVW
40 assert response.json() == {"param": "value"} 1QRSTUVW
43def test_cookie_model_with_alias(): 1abcdefg
44 client = TestClient(app) 1opqrstu
45 client.cookies.set("param_alias", "value") 1opqrstu
46 response = client.get("/cookie") 1opqrstu
47 assert response.status_code == 200, response.text 1opqrstu
48 assert response.json() == {"param": "value"} 1opqrstu
51def test_query_model_with_alias_by_name(): 1abcdefg
52 client = TestClient(app) 1vwxyzAB
53 response = client.get("/query", params={"param": "value"}) 1vwxyzAB
54 assert response.status_code == 422, response.text 1vwxyzAB
55 details = response.json() 1vwxyzAB
56 if PYDANTIC_V2: 1vwxyzAB
57 assert details["detail"][0]["input"] == {"param": "value"} 1vwxyzAB
60def test_header_model_with_alias_by_name(): 1abcdefg
61 client = TestClient(app) 1CDEFGHI
62 response = client.get("/header", headers={"param": "value"}) 1CDEFGHI
63 assert response.status_code == 422, response.text 1CDEFGHI
64 details = response.json() 1CDEFGHI
65 if PYDANTIC_V2: 1CDEFGHI
66 assert details["detail"][0]["input"] == IsPartialDict({"param": "value"}) 1CDEFGHI
69def test_cookie_model_with_alias_by_name(): 1abcdefg
70 client = TestClient(app) 1hijklmn
71 client.cookies.set("param", "value") 1hijklmn
72 response = client.get("/cookie") 1hijklmn
73 assert response.status_code == 422, response.text 1hijklmn
74 details = response.json() 1hijklmn
75 if PYDANTIC_V2: 1hijklmn
76 assert details["detail"][0]["input"] == {"param": "value"} 1hijklmn