Coverage for tests / test_tutorial / test_body_multiple_params / test_tutorial001.py: 100%
30 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
7from ...utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial001_py310", marks=needs_py310),
14 pytest.param("tutorial001_an_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module(f"docs_src.body_multiple_params.{request.param}") 1abc
20 client = TestClient(mod.app) 1abc
21 return client 1abc
24def test_post_body_q_bar_content(client: TestClient): 1abdc
25 response = client.put("/items/5?q=bar", json={"name": "Foo", "price": 50.5}) 1efg
26 assert response.status_code == 200 1efg
27 assert response.json() == { 1efg
28 "item_id": 5,
29 "item": {
30 "name": "Foo",
31 "price": 50.5,
32 "description": None,
33 "tax": None,
34 },
35 "q": "bar",
36 }
39def test_post_no_body_q_bar(client: TestClient): 1abdc
40 response = client.put("/items/5?q=bar", json=None) 1hij
41 assert response.status_code == 200 1hij
42 assert response.json() == {"item_id": 5, "q": "bar"} 1hij
45def test_post_no_body(client: TestClient): 1abdc
46 response = client.put("/items/5", json=None) 1klm
47 assert response.status_code == 200 1klm
48 assert response.json() == {"item_id": 5} 1klm
51def test_post_id_foo(client: TestClient): 1abdc
52 response = client.put("/items/foo", json=None) 1nop
53 assert response.status_code == 422 1nop
54 assert response.json() == { 1nop
55 "detail": [
56 {
57 "type": "int_parsing",
58 "loc": ["path", "item_id"],
59 "msg": "Input should be a valid integer, unable to parse string as an integer",
60 "input": "foo",
61 }
62 ]
63 }
66def test_openapi_schema(client: TestClient): 1abdc
67 response = client.get("/openapi.json") 1qrs
68 assert response.status_code == 200, response.text 1qrs
69 assert response.json() == snapshot( 1qrs
70 {
71 "openapi": "3.1.0",
72 "info": {"title": "FastAPI", "version": "0.1.0"},
73 "paths": {
74 "/items/{item_id}": {
75 "put": {
76 "responses": {
77 "200": {
78 "description": "Successful Response",
79 "content": {"application/json": {"schema": {}}},
80 },
81 "422": {
82 "description": "Validation Error",
83 "content": {
84 "application/json": {
85 "schema": {
86 "$ref": "#/components/schemas/HTTPValidationError"
87 }
88 }
89 },
90 },
91 },
92 "summary": "Update Item",
93 "operationId": "update_item_items__item_id__put",
94 "parameters": [
95 {
96 "required": True,
97 "schema": {
98 "title": "The ID of the item to get",
99 "maximum": 1000.0,
100 "minimum": 0.0,
101 "type": "integer",
102 },
103 "name": "item_id",
104 "in": "path",
105 },
106 {
107 "required": False,
108 "schema": {
109 "anyOf": [{"type": "string"}, {"type": "null"}],
110 "title": "Q",
111 },
112 "name": "q",
113 "in": "query",
114 },
115 ],
116 "requestBody": {
117 "content": {
118 "application/json": {
119 "schema": {
120 "anyOf": [
121 {"$ref": "#/components/schemas/Item"},
122 {"type": "null"},
123 ],
124 "title": "Item",
125 }
126 }
127 }
128 },
129 }
130 }
131 },
132 "components": {
133 "schemas": {
134 "Item": {
135 "title": "Item",
136 "required": ["name", "price"],
137 "type": "object",
138 "properties": {
139 "name": {"title": "Name", "type": "string"},
140 "description": {
141 "title": "Description",
142 "anyOf": [{"type": "string"}, {"type": "null"}],
143 },
144 "price": {"title": "Price", "type": "number"},
145 "tax": {
146 "title": "Tax",
147 "anyOf": [{"type": "number"}, {"type": "null"}],
148 },
149 },
150 },
151 "ValidationError": {
152 "title": "ValidationError",
153 "required": ["loc", "msg", "type"],
154 "type": "object",
155 "properties": {
156 "loc": {
157 "title": "Location",
158 "type": "array",
159 "items": {
160 "anyOf": [{"type": "string"}, {"type": "integer"}]
161 },
162 },
163 "msg": {"title": "Message", "type": "string"},
164 "type": {"title": "Error Type", "type": "string"},
165 "input": {"title": "Input"},
166 "ctx": {"title": "Context", "type": "object"},
167 },
168 },
169 "HTTPValidationError": {
170 "title": "HTTPValidationError",
171 "type": "object",
172 "properties": {
173 "detail": {
174 "title": "Detail",
175 "type": "array",
176 "items": {
177 "$ref": "#/components/schemas/ValidationError"
178 },
179 }
180 },
181 },
182 }
183 },
184 }
185 )