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