Debian Codegraph @

tests/test_tutorial/test_schema_extra_example/test_tutorial003.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7 from ...utils import needs_py310
8
9
10 @pytest.fixture(
11 name="client",
12 params=[
13 pytest.param("tutorial003_py310", marks=needs_py310),
14 pytest.param("tutorial003_an_py310", marks=needs_py310),
15 ],
16 )
17 def get_client(request: pytest.FixtureRequest):
18 mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}")
19
20 client = TestClient(mod.app)
21 return client
22
23
24 def test_post_body_example(client: TestClient):
25 response = client.put(
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
35
36
37 def test_openapi_schema(client: TestClient):
38 response = client.get("/openapi.json")
39 assert response.status_code == 200, response.text
40 assert response.json() == snapshot(
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 "name": "item_id",
52 "in": "path",
53 "required": True,
54 "schema": {"type": "integer", "title": "Item Id"},
55 }
56 ],
57 "requestBody": {
58 "required": True,
59 "content": {
60 "application/json": {
61 "schema": {
62 "$ref": "#/components/schemas/Item",
63 "examples": [
64 {
65 "description": "A very nice Item",
66 "name": "Foo",
67 "price": 35.4,
68 "tax": 3.2,
69 }
70 ],
71 },
72 }
73 },
74 },
75 "responses": {
76 "200": {
77 "description": "Successful Response",
78 "content": {"application/json": {"schema": {}}},
79 },
80 "422": {
81 "description": "Validation Error",
82 "content": {
83 "application/json": {
84 "schema": {
85 "$ref": "#/components/schemas/HTTPValidationError"
86 }
87 }
88 },
89 },
90 },
91 }
92 }
93 },
94 "components": {
95 "schemas": {
96 "HTTPValidationError": {
97 "properties": {
98 "detail": {
99 "items": {
100 "$ref": "#/components/schemas/ValidationError"
101 },
102 "type": "array",
103 "title": "Detail",
104 }
105 },
106 "type": "object",
107 "title": "HTTPValidationError",
108 },
109 "Item": {
110 "properties": {
111 "name": {"type": "string", "title": "Name"},
112 "description": {
113 "anyOf": [{"type": "string"}, {"type": "null"}],
114 "title": "Description",
115 },
116 "price": {"type": "number", "title": "Price"},
117 "tax": {
118 "anyOf": [{"type": "number"}, {"type": "null"}],
119 "title": "Tax",
120 },
121 },
122 "type": "object",
123 "required": ["name", "price"],
124 "title": "Item",
125 },
126 "ValidationError": {
127 "properties": {
128 "ctx": {"title": "Context", "type": "object"},
129 "input": {"title": "Input"},
130 "loc": {
131 "items": {
132 "anyOf": [{"type": "string"}, {"type": "integer"}]
133 },
134 "type": "array",
135 "title": "Location",
136 },
137 "msg": {"type": "string", "title": "Message"},
138 "type": {"type": "string", "title": "Error Type"},
139 },
140 "type": "object",
141 "required": ["loc", "msg", "type"],
142 "title": "ValidationError",
143 },
144 }
145 },
146 }
147 )