Coverage for tests/test_tutorial/test_schema_extra_example/test_tutorial004.py: 100%
17 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from dirty_equals import IsDict 1abcdef
5from fastapi.testclient import TestClient 1abcdef
7from ...utils import needs_py39, needs_py310 1abcdef
10@pytest.fixture( 1abcdef
11 name="client",
12 params=[
13 "tutorial004",
14 pytest.param("tutorial004_py310", marks=needs_py310),
15 "tutorial004_an",
16 pytest.param("tutorial004_an_py39", marks=needs_py39),
17 pytest.param("tutorial004_an_py310", marks=needs_py310),
18 ],
19)
20def get_client(request: pytest.FixtureRequest): 1abcdef
21 mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}") 1abcdef
23 client = TestClient(mod.app) 1abcdef
24 return client 1abcdef
27def test_post_body_example(client: TestClient): 1abcdef
28 response = client.put( 1mnopqr
29 "/items/5",
30 json={
31 "name": "Foo",
32 "description": "A very nice Item",
33 "price": 35.4,
34 "tax": 3.2,
35 },
36 )
37 assert response.status_code == 200 1mnopqr
40def test_openapi_schema(client: TestClient): 1abcdef
41 response = client.get("/openapi.json") 1ghijkl
42 assert response.status_code == 200, response.text 1ghijkl
43 assert response.json() == { 1ghijkl
44 "openapi": "3.1.0",
45 "info": {"title": "FastAPI", "version": "0.1.0"},
46 "paths": {
47 "/items/{item_id}": {
48 "put": {
49 "summary": "Update Item",
50 "operationId": "update_item_items__item_id__put",
51 "parameters": [
52 {
53 "required": True,
54 "schema": {"title": "Item Id", "type": "integer"},
55 "name": "item_id",
56 "in": "path",
57 }
58 ],
59 "requestBody": {
60 "content": {
61 "application/json": {
62 "schema": IsDict(
63 {
64 "$ref": "#/components/schemas/Item",
65 "examples": [
66 {
67 "name": "Foo",
68 "description": "A very nice Item",
69 "price": 35.4,
70 "tax": 3.2,
71 },
72 {"name": "Bar", "price": "35.4"},
73 {
74 "name": "Baz",
75 "price": "thirty five point four",
76 },
77 ],
78 }
79 )
80 | IsDict(
81 # TODO: remove when deprecating Pydantic v1
82 {
83 "allOf": [
84 {"$ref": "#/components/schemas/Item"}
85 ],
86 "title": "Item",
87 "examples": [
88 {
89 "name": "Foo",
90 "description": "A very nice Item",
91 "price": 35.4,
92 "tax": 3.2,
93 },
94 {"name": "Bar", "price": "35.4"},
95 {
96 "name": "Baz",
97 "price": "thirty five point four",
98 },
99 ],
100 }
101 )
102 }
103 },
104 "required": True,
105 },
106 "responses": {
107 "200": {
108 "description": "Successful Response",
109 "content": {"application/json": {"schema": {}}},
110 },
111 "422": {
112 "description": "Validation Error",
113 "content": {
114 "application/json": {
115 "schema": {
116 "$ref": "#/components/schemas/HTTPValidationError"
117 }
118 }
119 },
120 },
121 },
122 }
123 }
124 },
125 "components": {
126 "schemas": {
127 "HTTPValidationError": {
128 "title": "HTTPValidationError",
129 "type": "object",
130 "properties": {
131 "detail": {
132 "title": "Detail",
133 "type": "array",
134 "items": {"$ref": "#/components/schemas/ValidationError"},
135 }
136 },
137 },
138 "Item": {
139 "title": "Item",
140 "required": ["name", "price"],
141 "type": "object",
142 "properties": {
143 "name": {"title": "Name", "type": "string"},
144 "description": IsDict(
145 {
146 "title": "Description",
147 "anyOf": [{"type": "string"}, {"type": "null"}],
148 }
149 )
150 | IsDict(
151 # TODO: remove when deprecating Pydantic v1
152 {"title": "Description", "type": "string"}
153 ),
154 "price": {"title": "Price", "type": "number"},
155 "tax": IsDict(
156 {
157 "title": "Tax",
158 "anyOf": [{"type": "number"}, {"type": "null"}],
159 }
160 )
161 | IsDict(
162 # TODO: remove when deprecating Pydantic v1
163 {"title": "Tax", "type": "number"}
164 ),
165 },
166 },
167 "ValidationError": {
168 "title": "ValidationError",
169 "required": ["loc", "msg", "type"],
170 "type": "object",
171 "properties": {
172 "loc": {
173 "title": "Location",
174 "type": "array",
175 "items": {
176 "anyOf": [{"type": "string"}, {"type": "integer"}]
177 },
178 },
179 "msg": {"title": "Message", "type": "string"},
180 "type": {"title": "Error Type", "type": "string"},
181 },
182 },
183 }
184 },
185 }