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