Coverage for tests / test_tutorial / test_schema_extra_example / test_tutorial002.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("tutorial002_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": {
101 "type": "string",
102 "title": "Name",
103 "examples": ["Foo"],
104 },
105 "description": {
106 "anyOf": [{"type": "string"}, {"type": "null"}],
107 "title": "Description",
108 "examples": ["A very nice Item"],
109 },
110 "price": {
111 "type": "number",
112 "title": "Price",
113 "examples": [35.4],
114 },
115 "tax": {
116 "anyOf": [{"type": "number"}, {"type": "null"}],
117 "title": "Tax",
118 "examples": [3.2],
119 },
120 },
121 "type": "object",
122 "required": ["name", "price"],
123 "title": "Item",
124 },
125 "ValidationError": {
126 "properties": {
127 "ctx": {"title": "Context", "type": "object"},
128 "input": {"title": "Input"},
129 "loc": {
130 "items": {
131 "anyOf": [{"type": "string"}, {"type": "integer"}]
132 },
133 "type": "array",
134 "title": "Location",
135 },
136 "msg": {"type": "string", "title": "Message"},
137 "type": {"type": "string", "title": "Error Type"},
138 },
139 "type": "object",
140 "required": ["loc", "msg", "type"],
141 "title": "ValidationError",
142 },
143 }
144 },
145 }
146 )