Coverage for tests/test_response_model_include_exclude.py: 100%
57 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 fastapi import FastAPI 1abcde
2from fastapi.testclient import TestClient 1abcde
3from pydantic import BaseModel 1abcde
6class Model1(BaseModel): 1abcde
7 foo: str 1abcde
8 bar: str 1abcde
11class Model2(BaseModel): 1abcde
12 ref: Model1 1abcde
13 baz: str 1abcde
16class Model3(BaseModel): 1abcde
17 name: str 1abcde
18 age: int 1abcde
19 ref2: Model2 1abcde
22app = FastAPI() 1abcde
25@app.get( 1abcde
26 "/simple_include",
27 response_model=Model2,
28 response_model_include={"baz": ..., "ref": {"foo"}},
29)
30def simple_include(): 1abcde
31 return Model2( 1abcde
32 ref=Model1(foo="simple_include model foo", bar="simple_include model bar"),
33 baz="simple_include model2 baz",
34 )
37@app.get( 1abcde
38 "/simple_include_dict",
39 response_model=Model2,
40 response_model_include={"baz": ..., "ref": {"foo"}},
41)
42def simple_include_dict(): 1abcde
43 return { 1abcde
44 "ref": {
45 "foo": "simple_include_dict model foo",
46 "bar": "simple_include_dict model bar",
47 },
48 "baz": "simple_include_dict model2 baz",
49 }
52@app.get( 1abcde
53 "/simple_exclude",
54 response_model=Model2,
55 response_model_exclude={"ref": {"bar"}},
56)
57def simple_exclude(): 1abcde
58 return Model2( 1abcde
59 ref=Model1(foo="simple_exclude model foo", bar="simple_exclude model bar"),
60 baz="simple_exclude model2 baz",
61 )
64@app.get( 1abcde
65 "/simple_exclude_dict",
66 response_model=Model2,
67 response_model_exclude={"ref": {"bar"}},
68)
69def simple_exclude_dict(): 1abcde
70 return { 1abcde
71 "ref": {
72 "foo": "simple_exclude_dict model foo",
73 "bar": "simple_exclude_dict model bar",
74 },
75 "baz": "simple_exclude_dict model2 baz",
76 }
79@app.get( 1abcde
80 "/mixed",
81 response_model=Model3,
82 response_model_include={"ref2", "name"},
83 response_model_exclude={"ref2": {"baz"}},
84)
85def mixed(): 1abcde
86 return Model3( 1abcde
87 name="mixed model3 name",
88 age=3,
89 ref2=Model2(
90 ref=Model1(foo="mixed model foo", bar="mixed model bar"),
91 baz="mixed model2 baz",
92 ),
93 )
96@app.get( 1abcde
97 "/mixed_dict",
98 response_model=Model3,
99 response_model_include={"ref2", "name"},
100 response_model_exclude={"ref2": {"baz"}},
101)
102def mixed_dict(): 1abcde
103 return { 1abcde
104 "name": "mixed_dict model3 name",
105 "age": 3,
106 "ref2": {
107 "ref": {"foo": "mixed_dict model foo", "bar": "mixed_dict model bar"},
108 "baz": "mixed_dict model2 baz",
109 },
110 }
113client = TestClient(app) 1abcde
116def test_nested_include_simple(): 1abcde
117 response = client.get("/simple_include") 1abcde
119 assert response.status_code == 200, response.text 1abcde
121 assert response.json() == { 1abcde
122 "baz": "simple_include model2 baz",
123 "ref": {"foo": "simple_include model foo"},
124 }
127def test_nested_include_simple_dict(): 1abcde
128 response = client.get("/simple_include_dict") 1abcde
130 assert response.status_code == 200, response.text 1abcde
132 assert response.json() == { 1abcde
133 "baz": "simple_include_dict model2 baz",
134 "ref": {"foo": "simple_include_dict model foo"},
135 }
138def test_nested_exclude_simple(): 1abcde
139 response = client.get("/simple_exclude") 1abcde
140 assert response.status_code == 200, response.text 1abcde
141 assert response.json() == { 1abcde
142 "baz": "simple_exclude model2 baz",
143 "ref": {"foo": "simple_exclude model foo"},
144 }
147def test_nested_exclude_simple_dict(): 1abcde
148 response = client.get("/simple_exclude_dict") 1abcde
149 assert response.status_code == 200, response.text 1abcde
150 assert response.json() == { 1abcde
151 "baz": "simple_exclude_dict model2 baz",
152 "ref": {"foo": "simple_exclude_dict model foo"},
153 }
156def test_nested_include_mixed(): 1abcde
157 response = client.get("/mixed") 1abcde
158 assert response.status_code == 200, response.text 1abcde
159 assert response.json() == { 1abcde
160 "name": "mixed model3 name",
161 "ref2": {
162 "ref": {"foo": "mixed model foo", "bar": "mixed model bar"},
163 },
164 }
167def test_nested_include_mixed_dict(): 1abcde
168 response = client.get("/mixed_dict") 1abcde
169 assert response.status_code == 200, response.text 1abcde
170 assert response.json() == { 1abcde
171 "name": "mixed_dict model3 name",
172 "ref2": {
173 "ref": {"foo": "mixed_dict model foo", "bar": "mixed_dict model bar"},
174 },
175 }