Coverage for tests / test_tutorial / test_body_fields / test_tutorial001.py: 100%
26 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_fields.{request.param}") 1abc
20 client = TestClient(mod.app) 1abc
21 return client 1abc
24def test_items_5(client: TestClient): 1abdc
25 response = client.put("/items/5", json={"item": {"name": "Foo", "price": 3.0}}) 1efg
26 assert response.status_code == 200 1efg
27 assert response.json() == { 1efg
28 "item_id": 5,
29 "item": {"name": "Foo", "price": 3.0, "description": None, "tax": None},
30 }
33def test_items_6(client: TestClient): 1abdc
34 response = client.put( 1hij
35 "/items/6",
36 json={
37 "item": {
38 "name": "Bar",
39 "price": 0.2,
40 "description": "Some bar",
41 "tax": "5.4",
42 }
43 },
44 )
45 assert response.status_code == 200 1hij
46 assert response.json() == { 1hij
47 "item_id": 6,
48 "item": {
49 "name": "Bar",
50 "price": 0.2,
51 "description": "Some bar",
52 "tax": 5.4,
53 },
54 }
57def test_invalid_price(client: TestClient): 1abdc
58 response = client.put("/items/5", json={"item": {"name": "Foo", "price": -3.0}}) 1klm
59 assert response.status_code == 422 1klm
60 assert response.json() == { 1klm
61 "detail": [
62 {
63 "type": "greater_than",
64 "loc": ["body", "item", "price"],
65 "msg": "Input should be greater than 0",
66 "input": -3.0,
67 "ctx": {"gt": 0.0},
68 }
69 ]
70 }
73def test_openapi_schema(client: TestClient): 1abdc
74 response = client.get("/openapi.json") 1nop
75 assert response.status_code == 200, response.text 1nop
76 assert response.json() == snapshot( 1nop
77 {
78 "openapi": "3.1.0",
79 "info": {"title": "FastAPI", "version": "0.1.0"},
80 "paths": {
81 "/items/{item_id}": {
82 "put": {
83 "responses": {
84 "200": {
85 "description": "Successful Response",
86 "content": {"application/json": {"schema": {}}},
87 },
88 "422": {
89 "description": "Validation Error",
90 "content": {
91 "application/json": {
92 "schema": {
93 "$ref": "#/components/schemas/HTTPValidationError"
94 }
95 }
96 },
97 },
98 },
99 "summary": "Update Item",
100 "operationId": "update_item_items__item_id__put",
101 "parameters": [
102 {
103 "required": True,
104 "schema": {"title": "Item Id", "type": "integer"},
105 "name": "item_id",
106 "in": "path",
107 }
108 ],
109 "requestBody": {
110 "content": {
111 "application/json": {
112 "schema": {
113 "$ref": "#/components/schemas/Body_update_item_items__item_id__put"
114 }
115 }
116 },
117 "required": True,
118 },
119 }
120 }
121 },
122 "components": {
123 "schemas": {
124 "Item": {
125 "title": "Item",
126 "required": ["name", "price"],
127 "type": "object",
128 "properties": {
129 "name": {"title": "Name", "type": "string"},
130 "description": {
131 "title": "The description of the item",
132 "anyOf": [
133 {"maxLength": 300, "type": "string"},
134 {"type": "null"},
135 ],
136 },
137 "price": {
138 "title": "Price",
139 "exclusiveMinimum": 0.0,
140 "type": "number",
141 "description": "The price must be greater than zero",
142 },
143 "tax": {
144 "title": "Tax",
145 "anyOf": [{"type": "number"}, {"type": "null"}],
146 },
147 },
148 },
149 "Body_update_item_items__item_id__put": {
150 "title": "Body_update_item_items__item_id__put",
151 "required": ["item"],
152 "type": "object",
153 "properties": {"item": {"$ref": "#/components/schemas/Item"}},
154 },
155 "ValidationError": {
156 "title": "ValidationError",
157 "required": ["loc", "msg", "type"],
158 "type": "object",
159 "properties": {
160 "loc": {
161 "title": "Location",
162 "type": "array",
163 "items": {
164 "anyOf": [{"type": "string"}, {"type": "integer"}]
165 },
166 },
167 "msg": {"title": "Message", "type": "string"},
168 "type": {"title": "Error Type", "type": "string"},
169 "input": {"title": "Input"},
170 "ctx": {"title": "Context", "type": "object"},
171 },
172 },
173 "HTTPValidationError": {
174 "title": "HTTPValidationError",
175 "type": "object",
176 "properties": {
177 "detail": {
178 "title": "Detail",
179 "type": "array",
180 "items": {
181 "$ref": "#/components/schemas/ValidationError"
182 },
183 }
184 },
185 },
186 }
187 },
188 }
189 )