Coverage for tests / test_tutorial / test_response_model / test_tutorial001_tutorial001_01.py: 100%
27 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_01_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module(f"docs_src.response_model.{request.param}") 1abc
20 client = TestClient(mod.app) 1abc
21 return client 1abc
24def test_read_items(client: TestClient): 1abdc
25 response = client.get("/items/") 1hij
26 assert response.status_code == 200, response.text 1hij
27 assert response.json() == [ 1hij
28 {
29 "name": "Portal Gun",
30 "description": None,
31 "price": 42.0,
32 "tags": [],
33 "tax": None,
34 },
35 {
36 "name": "Plumbus",
37 "description": None,
38 "price": 32.0,
39 "tags": [],
40 "tax": None,
41 },
42 ]
45def test_create_item(client: TestClient): 1abdc
46 item_data = { 1efg
47 "name": "Test Item",
48 "description": "A test item",
49 "price": 10.5,
50 "tax": 1.5,
51 "tags": ["test", "item"],
52 }
53 response = client.post("/items/", json=item_data) 1efg
54 assert response.status_code == 200, response.text 1efg
55 assert response.json() == item_data 1efg
58def test_create_item_only_required(client: TestClient): 1abdc
59 response = client.post( 1klm
60 "/items/",
61 json={
62 "name": "Test Item",
63 "price": 10.5,
64 },
65 )
66 assert response.status_code == 200, response.text 1klm
67 assert response.json() == { 1klm
68 "name": "Test Item",
69 "price": 10.5,
70 "description": None,
71 "tax": None,
72 "tags": [],
73 }
76def test_openapi_schema(client: TestClient): 1abdc
77 response = client.get("/openapi.json") 1nop
78 assert response.status_code == 200, response.text 1nop
79 assert response.json() == snapshot( 1nop
80 {
81 "openapi": "3.1.0",
82 "info": {"title": "FastAPI", "version": "0.1.0"},
83 "paths": {
84 "/items/": {
85 "get": {
86 "responses": {
87 "200": {
88 "description": "Successful Response",
89 "content": {
90 "application/json": {
91 "schema": {
92 "type": "array",
93 "items": {
94 "$ref": "#/components/schemas/Item"
95 },
96 "title": "Response Read Items Items Get",
97 }
98 }
99 },
100 },
101 },
102 "summary": "Read Items",
103 "operationId": "read_items_items__get",
104 },
105 "post": {
106 "requestBody": {
107 "content": {
108 "application/json": {
109 "schema": {
110 "$ref": "#/components/schemas/Item",
111 },
112 },
113 },
114 "required": True,
115 },
116 "responses": {
117 "200": {
118 "description": "Successful Response",
119 "content": {
120 "application/json": {
121 "schema": {"$ref": "#/components/schemas/Item"},
122 }
123 },
124 },
125 "422": {
126 "description": "Validation Error",
127 "content": {
128 "application/json": {
129 "schema": {
130 "$ref": "#/components/schemas/HTTPValidationError"
131 }
132 }
133 },
134 },
135 },
136 "summary": "Create Item",
137 "operationId": "create_item_items__post",
138 },
139 }
140 },
141 "components": {
142 "schemas": {
143 "Item": {
144 "title": "Item",
145 "required": ["name", "price"],
146 "type": "object",
147 "properties": {
148 "name": {"title": "Name", "type": "string"},
149 "price": {"title": "Price", "type": "number"},
150 "description": {
151 "title": "Description",
152 "anyOf": [{"type": "string"}, {"type": "null"}],
153 },
154 "tax": {
155 "title": "Tax",
156 "anyOf": [{"type": "number"}, {"type": "null"}],
157 },
158 "tags": {
159 "title": "Tags",
160 "type": "array",
161 "items": {"type": "string"},
162 "default": [],
163 },
164 },
165 },
166 "ValidationError": {
167 "title": "ValidationError",
168 "required": ["loc", "msg", "type"],
169 "type": "object",
170 "properties": {
171 "loc": {
172 "title": "Location",
173 "type": "array",
174 "items": {
175 "anyOf": [{"type": "string"}, {"type": "integer"}]
176 },
177 },
178 "msg": {"title": "Message", "type": "string"},
179 "type": {"title": "Error Type", "type": "string"},
180 "input": {"title": "Input"},
181 "ctx": {"title": "Context", "type": "object"},
182 },
183 },
184 "HTTPValidationError": {
185 "title": "HTTPValidationError",
186 "type": "object",
187 "properties": {
188 "detail": {
189 "title": "Detail",
190 "type": "array",
191 "items": {
192 "$ref": "#/components/schemas/ValidationError"
193 },
194 }
195 },
196 },
197 }
198 },
199 }
200 )