Coverage for tests / test_tutorial / test_schema_extra_example / test_tutorial004.py: 100%
17 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("tutorial004_py310", marks=needs_py310),
14 pytest.param("tutorial004_an_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}") 1abc
20 client = TestClient(mod.app) 1abc
21 return client 1abc
24def test_post_body_example(client: TestClient): 1abdc
25 response = client.put( 1hij
26 "/items/5",
27 json={
28 "name": "Foo",
29 "description": "A very nice Item",
30 "price": 35.4,
31 "tax": 3.2,
32 },
33 )
34 assert response.status_code == 200 1hij
37def test_openapi_schema(client: TestClient): 1abdc
38 response = client.get("/openapi.json") 1efg
39 assert response.status_code == 200, response.text 1efg
40 assert response.json() == snapshot( 1efg
41 {
42 "openapi": "3.1.0",
43 "info": {"title": "FastAPI", "version": "0.1.0"},
44 "paths": {
45 "/items/{item_id}": {
46 "put": {
47 "summary": "Update Item",
48 "operationId": "update_item_items__item_id__put",
49 "parameters": [
50 {
51 "required": True,
52 "schema": {"title": "Item Id", "type": "integer"},
53 "name": "item_id",
54 "in": "path",
55 }
56 ],
57 "requestBody": {
58 "content": {
59 "application/json": {
60 "schema": {
61 "$ref": "#/components/schemas/Item",
62 "examples": [
63 {
64 "name": "Foo",
65 "description": "A very nice Item",
66 "price": 35.4,
67 "tax": 3.2,
68 },
69 {"name": "Bar", "price": "35.4"},
70 {
71 "name": "Baz",
72 "price": "thirty five point four",
73 },
74 ],
75 }
76 }
77 },
78 "required": True,
79 },
80 "responses": {
81 "200": {
82 "description": "Successful Response",
83 "content": {"application/json": {"schema": {}}},
84 },
85 "422": {
86 "description": "Validation Error",
87 "content": {
88 "application/json": {
89 "schema": {
90 "$ref": "#/components/schemas/HTTPValidationError"
91 }
92 }
93 },
94 },
95 },
96 }
97 }
98 },
99 "components": {
100 "schemas": {
101 "HTTPValidationError": {
102 "title": "HTTPValidationError",
103 "type": "object",
104 "properties": {
105 "detail": {
106 "title": "Detail",
107 "type": "array",
108 "items": {
109 "$ref": "#/components/schemas/ValidationError"
110 },
111 }
112 },
113 },
114 "Item": {
115 "title": "Item",
116 "required": ["name", "price"],
117 "type": "object",
118 "properties": {
119 "name": {"title": "Name", "type": "string"},
120 "description": {
121 "title": "Description",
122 "anyOf": [{"type": "string"}, {"type": "null"}],
123 },
124 "price": {"title": "Price", "type": "number"},
125 "tax": {
126 "title": "Tax",
127 "anyOf": [{"type": "number"}, {"type": "null"}],
128 },
129 },
130 },
131 "ValidationError": {
132 "title": "ValidationError",
133 "required": ["loc", "msg", "type"],
134 "type": "object",
135 "properties": {
136 "loc": {
137 "title": "Location",
138 "type": "array",
139 "items": {
140 "anyOf": [{"type": "string"}, {"type": "integer"}]
141 },
142 },
143 "msg": {"title": "Message", "type": "string"},
144 "type": {"title": "Error Type", "type": "string"},
145 "input": {"title": "Input"},
146 "ctx": {"title": "Context", "type": "object"},
147 },
148 },
149 }
150 },
151 }
152 )