Coverage for tests/test_tutorial/test_body_multiple_params/test_tutorial001.py: 100%
30 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from dirty_equals import IsDict 1abcdef
5from fastapi.testclient import TestClient 1abcdef
7from ...utils import needs_py39, needs_py310 1abcdef
10@pytest.fixture( 1abcdef
11 name="client",
12 params=[
13 "tutorial001",
14 pytest.param("tutorial001_py310", marks=needs_py310),
15 "tutorial001_an",
16 pytest.param("tutorial001_an_py39", marks=needs_py39),
17 pytest.param("tutorial001_an_py310", marks=needs_py310),
18 ],
19)
20def get_client(request: pytest.FixtureRequest): 1abcdef
21 mod = importlib.import_module(f"docs_src.body_multiple_params.{request.param}") 1abcdef
23 client = TestClient(mod.app) 1abcdef
24 return client 1abcdef
27def test_post_body_q_bar_content(client: TestClient): 1abcdef
28 response = client.put("/items/5?q=bar", json={"name": "Foo", "price": 50.5}) 1ghijkl
29 assert response.status_code == 200 1ghijkl
30 assert response.json() == { 1ghijkl
31 "item_id": 5,
32 "item": {
33 "name": "Foo",
34 "price": 50.5,
35 "description": None,
36 "tax": None,
37 },
38 "q": "bar",
39 }
42def test_post_no_body_q_bar(client: TestClient): 1abcdef
43 response = client.put("/items/5?q=bar", json=None) 1mnopqr
44 assert response.status_code == 200 1mnopqr
45 assert response.json() == {"item_id": 5, "q": "bar"} 1mnopqr
48def test_post_no_body(client: TestClient): 1abcdef
49 response = client.put("/items/5", json=None) 1stuvwx
50 assert response.status_code == 200 1stuvwx
51 assert response.json() == {"item_id": 5} 1stuvwx
54def test_post_id_foo(client: TestClient): 1abcdef
55 response = client.put("/items/foo", json=None) 1yzABCD
56 assert response.status_code == 422 1yzABCD
57 assert response.json() == IsDict( 1yzABCD
58 {
59 "detail": [
60 {
61 "type": "int_parsing",
62 "loc": ["path", "item_id"],
63 "msg": "Input should be a valid integer, unable to parse string as an integer",
64 "input": "foo",
65 }
66 ]
67 }
68 ) | IsDict(
69 # TODO: remove when deprecating Pydantic v1
70 {
71 "detail": [
72 {
73 "loc": ["path", "item_id"],
74 "msg": "value is not a valid integer",
75 "type": "type_error.integer",
76 }
77 ]
78 }
79 )
82def test_openapi_schema(client: TestClient): 1abcdef
83 response = client.get("/openapi.json") 1EFGHIJ
84 assert response.status_code == 200, response.text 1EFGHIJ
85 assert response.json() == { 1EFGHIJ
86 "openapi": "3.1.0",
87 "info": {"title": "FastAPI", "version": "0.1.0"},
88 "paths": {
89 "/items/{item_id}": {
90 "put": {
91 "responses": {
92 "200": {
93 "description": "Successful Response",
94 "content": {"application/json": {"schema": {}}},
95 },
96 "422": {
97 "description": "Validation Error",
98 "content": {
99 "application/json": {
100 "schema": {
101 "$ref": "#/components/schemas/HTTPValidationError"
102 }
103 }
104 },
105 },
106 },
107 "summary": "Update Item",
108 "operationId": "update_item_items__item_id__put",
109 "parameters": [
110 {
111 "required": True,
112 "schema": {
113 "title": "The ID of the item to get",
114 "maximum": 1000.0,
115 "minimum": 0.0,
116 "type": "integer",
117 },
118 "name": "item_id",
119 "in": "path",
120 },
121 {
122 "required": False,
123 "schema": IsDict(
124 {
125 "anyOf": [{"type": "string"}, {"type": "null"}],
126 "title": "Q",
127 }
128 )
129 | IsDict(
130 # TODO: remove when deprecating Pydantic v1
131 {"title": "Q", "type": "string"}
132 ),
133 "name": "q",
134 "in": "query",
135 },
136 ],
137 "requestBody": {
138 "content": {
139 "application/json": {
140 "schema": IsDict(
141 {
142 "anyOf": [
143 {"$ref": "#/components/schemas/Item"},
144 {"type": "null"},
145 ],
146 "title": "Item",
147 }
148 )
149 | IsDict(
150 # TODO: remove when deprecating Pydantic v1
151 {"$ref": "#/components/schemas/Item"}
152 )
153 }
154 }
155 },
156 }
157 }
158 },
159 "components": {
160 "schemas": {
161 "Item": {
162 "title": "Item",
163 "required": ["name", "price"],
164 "type": "object",
165 "properties": {
166 "name": {"title": "Name", "type": "string"},
167 "description": IsDict(
168 {
169 "title": "Description",
170 "anyOf": [{"type": "string"}, {"type": "null"}],
171 }
172 )
173 | IsDict(
174 # TODO: remove when deprecating Pydantic v1
175 {"title": "Description", "type": "string"}
176 ),
177 "price": {"title": "Price", "type": "number"},
178 "tax": IsDict(
179 {
180 "title": "Tax",
181 "anyOf": [{"type": "number"}, {"type": "null"}],
182 }
183 )
184 | IsDict(
185 # TODO: remove when deprecating Pydantic v1
186 {"title": "Tax", "type": "number"}
187 ),
188 },
189 },
190 "ValidationError": {
191 "title": "ValidationError",
192 "required": ["loc", "msg", "type"],
193 "type": "object",
194 "properties": {
195 "loc": {
196 "title": "Location",
197 "type": "array",
198 "items": {
199 "anyOf": [{"type": "string"}, {"type": "integer"}]
200 },
201 },
202 "msg": {"title": "Message", "type": "string"},
203 "type": {"title": "Error Type", "type": "string"},
204 },
205 },
206 "HTTPValidationError": {
207 "title": "HTTPValidationError",
208 "type": "object",
209 "properties": {
210 "detail": {
211 "title": "Detail",
212 "type": "array",
213 "items": {"$ref": "#/components/schemas/ValidationError"},
214 }
215 },
216 },
217 }
218 },
219 }