Coverage for tests/test_serialize_response_model.py: 100%
66 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 typing import Dict, List, Optional 1abcdefg
3from fastapi import FastAPI 1abcdefg
4from pydantic import BaseModel, Field 1abcdefg
5from starlette.testclient import TestClient 1abcdefg
7app = FastAPI() 1abcdefg
10class Item(BaseModel): 1abcdefg
11 name: str = Field(alias="aliased_name") 1abcdefg
12 price: Optional[float] = None 1abcdefg
13 owner_ids: Optional[List[int]] = None 1abcdefg
16@app.get("/items/valid", response_model=Item) 1abcdefg
17def get_valid(): 1abcdefg
18 return Item(aliased_name="valid", price=1.0) 1hijklmn
21@app.get("/items/coerce", response_model=Item) 1abcdefg
22def get_coerce(): 1abcdefg
23 return Item(aliased_name="coerce", price="1.0") 1opqrstu
26@app.get("/items/validlist", response_model=List[Item]) 1abcdefg
27def get_validlist(): 1abcdefg
28 return [ 1vwxyzAB
29 Item(aliased_name="foo"),
30 Item(aliased_name="bar", price=1.0),
31 Item(aliased_name="baz", price=2.0, owner_ids=[1, 2, 3]),
32 ]
35@app.get("/items/validdict", response_model=Dict[str, Item]) 1abcdefg
36def get_validdict(): 1abcdefg
37 return { 1CDEFGHI
38 "k1": Item(aliased_name="foo"),
39 "k2": Item(aliased_name="bar", price=1.0),
40 "k3": Item(aliased_name="baz", price=2.0, owner_ids=[1, 2, 3]),
41 }
44@app.get( 1abcdefg
45 "/items/valid-exclude-unset", response_model=Item, response_model_exclude_unset=True
46)
47def get_valid_exclude_unset(): 1abcdefg
48 return Item(aliased_name="valid", price=1.0) 1JKLMNOP
51@app.get( 1abcdefg
52 "/items/coerce-exclude-unset",
53 response_model=Item,
54 response_model_exclude_unset=True,
55)
56def get_coerce_exclude_unset(): 1abcdefg
57 return Item(aliased_name="coerce", price="1.0") 1QRSTUVW
60@app.get( 1abcdefg
61 "/items/validlist-exclude-unset",
62 response_model=List[Item],
63 response_model_exclude_unset=True,
64)
65def get_validlist_exclude_unset(): 1abcdefg
66 return [ 1XYZ0123
67 Item(aliased_name="foo"),
68 Item(aliased_name="bar", price=1.0),
69 Item(aliased_name="baz", price=2.0, owner_ids=[1, 2, 3]),
70 ]
73@app.get( 1abcdefg
74 "/items/validdict-exclude-unset",
75 response_model=Dict[str, Item],
76 response_model_exclude_unset=True,
77)
78def get_validdict_exclude_unset(): 1abcdefg
79 return { 1456789!
80 "k1": Item(aliased_name="foo"),
81 "k2": Item(aliased_name="bar", price=1.0),
82 "k3": Item(aliased_name="baz", price=2.0, owner_ids=[1, 2, 3]),
83 }
86client = TestClient(app) 1abcdefg
89def test_valid(): 1abcdefg
90 response = client.get("/items/valid") 1hijklmn
91 response.raise_for_status() 1hijklmn
92 assert response.json() == {"aliased_name": "valid", "price": 1.0, "owner_ids": None} 1hijklmn
95def test_coerce(): 1abcdefg
96 response = client.get("/items/coerce") 1opqrstu
97 response.raise_for_status() 1opqrstu
98 assert response.json() == { 1opqrstu
99 "aliased_name": "coerce",
100 "price": 1.0,
101 "owner_ids": None,
102 }
105def test_validlist(): 1abcdefg
106 response = client.get("/items/validlist") 1vwxyzAB
107 response.raise_for_status() 1vwxyzAB
108 assert response.json() == [ 1vwxyzAB
109 {"aliased_name": "foo", "price": None, "owner_ids": None},
110 {"aliased_name": "bar", "price": 1.0, "owner_ids": None},
111 {"aliased_name": "baz", "price": 2.0, "owner_ids": [1, 2, 3]},
112 ]
115def test_validdict(): 1abcdefg
116 response = client.get("/items/validdict") 1CDEFGHI
117 response.raise_for_status() 1CDEFGHI
118 assert response.json() == { 1CDEFGHI
119 "k1": {"aliased_name": "foo", "price": None, "owner_ids": None},
120 "k2": {"aliased_name": "bar", "price": 1.0, "owner_ids": None},
121 "k3": {"aliased_name": "baz", "price": 2.0, "owner_ids": [1, 2, 3]},
122 }
125def test_valid_exclude_unset(): 1abcdefg
126 response = client.get("/items/valid-exclude-unset") 1JKLMNOP
127 response.raise_for_status() 1JKLMNOP
128 assert response.json() == {"aliased_name": "valid", "price": 1.0} 1JKLMNOP
131def test_coerce_exclude_unset(): 1abcdefg
132 response = client.get("/items/coerce-exclude-unset") 1QRSTUVW
133 response.raise_for_status() 1QRSTUVW
134 assert response.json() == {"aliased_name": "coerce", "price": 1.0} 1QRSTUVW
137def test_validlist_exclude_unset(): 1abcdefg
138 response = client.get("/items/validlist-exclude-unset") 1XYZ0123
139 response.raise_for_status() 1XYZ0123
140 assert response.json() == [ 1XYZ0123
141 {"aliased_name": "foo"},
142 {"aliased_name": "bar", "price": 1.0},
143 {"aliased_name": "baz", "price": 2.0, "owner_ids": [1, 2, 3]},
144 ]
147def test_validdict_exclude_unset(): 1abcdefg
148 response = client.get("/items/validdict-exclude-unset") 1456789!
149 response.raise_for_status() 1456789!
150 assert response.json() == { 1456789!
151 "k1": {"aliased_name": "foo"},
152 "k2": {"aliased_name": "bar", "price": 1.0},
153 "k3": {"aliased_name": "baz", "price": 2.0, "owner_ids": [1, 2, 3]},
154 }