Coverage for tests/test_serialize_response_model.py: 100%
66 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from typing import Dict, List, Optional 1abcde
3from fastapi import FastAPI 1abcde
4from pydantic import BaseModel, Field 1abcde
5from starlette.testclient import TestClient 1abcde
7app = FastAPI() 1abcde
10class Item(BaseModel): 1abcde
11 name: str = Field(alias="aliased_name") 1abcde
12 price: Optional[float] = None 1abcde
13 owner_ids: Optional[List[int]] = None 1abcde
16@app.get("/items/valid", response_model=Item) 1abcde
17def get_valid(): 1abcde
18 return Item(aliased_name="valid", price=1.0) 1abcde
21@app.get("/items/coerce", response_model=Item) 1abcde
22def get_coerce(): 1abcde
23 return Item(aliased_name="coerce", price="1.0") 1abcde
26@app.get("/items/validlist", response_model=List[Item]) 1abcde
27def get_validlist(): 1abcde
28 return [ 1abcde
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]) 1abcde
36def get_validdict(): 1abcde
37 return { 1abcde
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( 1abcde
45 "/items/valid-exclude-unset", response_model=Item, response_model_exclude_unset=True
46)
47def get_valid_exclude_unset(): 1abcde
48 return Item(aliased_name="valid", price=1.0) 1abcde
51@app.get( 1abcde
52 "/items/coerce-exclude-unset",
53 response_model=Item,
54 response_model_exclude_unset=True,
55)
56def get_coerce_exclude_unset(): 1abcde
57 return Item(aliased_name="coerce", price="1.0") 1abcde
60@app.get( 1abcde
61 "/items/validlist-exclude-unset",
62 response_model=List[Item],
63 response_model_exclude_unset=True,
64)
65def get_validlist_exclude_unset(): 1abcde
66 return [ 1abcde
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( 1abcde
74 "/items/validdict-exclude-unset",
75 response_model=Dict[str, Item],
76 response_model_exclude_unset=True,
77)
78def get_validdict_exclude_unset(): 1abcde
79 return { 1abcde
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) 1abcde
89def test_valid(): 1abcde
90 response = client.get("/items/valid") 1abcde
91 response.raise_for_status() 1abcde
92 assert response.json() == {"aliased_name": "valid", "price": 1.0, "owner_ids": None} 1abcde
95def test_coerce(): 1abcde
96 response = client.get("/items/coerce") 1abcde
97 response.raise_for_status() 1abcde
98 assert response.json() == { 1abcde
99 "aliased_name": "coerce",
100 "price": 1.0,
101 "owner_ids": None,
102 }
105def test_validlist(): 1abcde
106 response = client.get("/items/validlist") 1abcde
107 response.raise_for_status() 1abcde
108 assert response.json() == [ 1abcde
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(): 1abcde
116 response = client.get("/items/validdict") 1abcde
117 response.raise_for_status() 1abcde
118 assert response.json() == { 1abcde
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(): 1abcde
126 response = client.get("/items/valid-exclude-unset") 1abcde
127 response.raise_for_status() 1abcde
128 assert response.json() == {"aliased_name": "valid", "price": 1.0} 1abcde
131def test_coerce_exclude_unset(): 1abcde
132 response = client.get("/items/coerce-exclude-unset") 1abcde
133 response.raise_for_status() 1abcde
134 assert response.json() == {"aliased_name": "coerce", "price": 1.0} 1abcde
137def test_validlist_exclude_unset(): 1abcde
138 response = client.get("/items/validlist-exclude-unset") 1abcde
139 response.raise_for_status() 1abcde
140 assert response.json() == [ 1abcde
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(): 1abcde
148 response = client.get("/items/validdict-exclude-unset") 1abcde
149 response.raise_for_status() 1abcde
150 assert response.json() == { 1abcde
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 }