Coverage for tests / test_tutorial / test_body / test_tutorial003.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("tutorial003_py310", marks=needs_py310),
14 ],
15)
16def get_client(request: pytest.FixtureRequest): 1abdc
17 mod = importlib.import_module(f"docs_src.body.{request.param}") 1abc
19 client = TestClient(mod.app) 1abc
20 return client 1abc
23def test_put_all(client: TestClient): 1abdc
24 response = client.put( 1efg
25 "/items/123",
26 json={"name": "Foo", "price": 50.1, "description": "Some Foo", "tax": 0.3},
27 )
28 assert response.status_code == 200 1efg
29 assert response.json() == { 1efg
30 "item_id": 123,
31 "name": "Foo",
32 "price": 50.1,
33 "description": "Some Foo",
34 "tax": 0.3,
35 }
38def test_put_only_required(client: TestClient): 1abdc
39 response = client.put( 1hij
40 "/items/123",
41 json={"name": "Foo", "price": 50.1},
42 )
43 assert response.status_code == 200 1hij
44 assert response.json() == { 1hij
45 "item_id": 123,
46 "name": "Foo",
47 "price": 50.1,
48 "description": None,
49 "tax": None,
50 }
53def test_put_with_no_data(client: TestClient): 1abdc
54 response = client.put("/items/123", json={}) 1klm
55 assert response.status_code == 422 1klm
56 assert response.json() == { 1klm
57 "detail": [
58 {
59 "type": "missing",
60 "loc": ["body", "name"],
61 "msg": "Field required",
62 "input": {},
63 },
64 {
65 "type": "missing",
66 "loc": ["body", "price"],
67 "msg": "Field required",
68 "input": {},
69 },
70 ]
71 }
74def test_openapi_schema(client: TestClient): 1abdc
75 response = client.get("/openapi.json") 1nop
76 assert response.status_code == 200, response.text 1nop
77 assert response.json() == snapshot( 1nop
78 {
79 "openapi": "3.1.0",
80 "info": {"title": "FastAPI", "version": "0.1.0"},
81 "paths": {
82 "/items/{item_id}": {
83 "put": {
84 "parameters": [
85 {
86 "in": "path",
87 "name": "item_id",
88 "required": True,
89 "schema": {
90 "title": "Item Id",
91 "type": "integer",
92 },
93 },
94 ],
95 "responses": {
96 "200": {
97 "description": "Successful Response",
98 "content": {"application/json": {"schema": {}}},
99 },
100 "422": {
101 "description": "Validation Error",
102 "content": {
103 "application/json": {
104 "schema": {
105 "$ref": "#/components/schemas/HTTPValidationError"
106 }
107 }
108 },
109 },
110 },
111 "summary": "Update Item",
112 "operationId": "update_item_items__item_id__put",
113 "requestBody": {
114 "content": {
115 "application/json": {
116 "schema": {"$ref": "#/components/schemas/Item"}
117 }
118 },
119 "required": True,
120 },
121 }
122 }
123 },
124 "components": {
125 "schemas": {
126 "Item": {
127 "title": "Item",
128 "required": ["name", "price"],
129 "type": "object",
130 "properties": {
131 "name": {"title": "Name", "type": "string"},
132 "price": {"title": "Price", "type": "number"},
133 "description": {
134 "title": "Description",
135 "anyOf": [{"type": "string"}, {"type": "null"}],
136 },
137 "tax": {
138 "title": "Tax",
139 "anyOf": [{"type": "number"}, {"type": "null"}],
140 },
141 },
142 },
143 "ValidationError": {
144 "title": "ValidationError",
145 "required": ["loc", "msg", "type"],
146 "type": "object",
147 "properties": {
148 "loc": {
149 "title": "Location",
150 "type": "array",
151 "items": {
152 "anyOf": [{"type": "string"}, {"type": "integer"}]
153 },
154 },
155 "msg": {"title": "Message", "type": "string"},
156 "type": {"title": "Error Type", "type": "string"},
157 "input": {"title": "Input"},
158 "ctx": {"title": "Context", "type": "object"},
159 },
160 },
161 "HTTPValidationError": {
162 "title": "HTTPValidationError",
163 "type": "object",
164 "properties": {
165 "detail": {
166 "title": "Detail",
167 "type": "array",
168 "items": {
169 "$ref": "#/components/schemas/ValidationError"
170 },
171 }
172 },
173 },
174 }
175 },
176 }
177 )