Debian Codegraph @

tests/test_tutorial/test_body/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 ],
15 )
16 def get_client(request: pytest.FixtureRequest):
17 mod = importlib.import_module(f"docs_src.body.{request.param}")
18
19 client = TestClient(mod.app)
20 return client
21
22
23 def test_put_all(client: TestClient):
24 response = client.put(
25 "/items/123",
26 json={"name": "Foo", "price": 50.1, "description": "Some Foo", "tax": 0.3},
27 )
28 assert response.status_code == 200
29 assert response.json() == {
30 "item_id": 123,
31 "name": "Foo",
32 "price": 50.1,
33 "description": "Some Foo",
34 "tax": 0.3,
35 }
36
37
38 def test_put_only_required(client: TestClient):
39 response = client.put(
40 "/items/123",
41 json={"name": "Foo", "price": 50.1},
42 )
43 assert response.status_code == 200
44 assert response.json() == {
45 "item_id": 123,
46 "name": "Foo",
47 "price": 50.1,
48 "description": None,
49 "tax": None,
50 }
51
52
53 def test_put_with_no_data(client: TestClient):
54 response = client.put("/items/123", json={})
55 assert response.status_code == 422
56 assert response.json() == {
57 "detail": [
58 {
59 "type": "missing",
60 "loc": ["body", "name"],
61 "msg": "Field required",
62 "input": {},
63 },
64 {
65 "type": "missing",
66 "loc": ["body", "price"],
67 "msg": "Field required",
68 "input": {},
69 },
70 ]
71 }
72
73
74 def test_openapi_schema(client: TestClient):
75 response = client.get("/openapi.json")
76 assert response.status_code == 200, response.text
77 assert response.json() == snapshot(
78 {
79 "openapi": "3.1.0",
80 "info": {"title": "FastAPI", "version": "0.1.0"},
81 "paths": {
82 "/items/{item_id}": {
83 "put": {
84 "parameters": [
85 {
86 "in": "path",
87 "name": "item_id",
88 "required": True,
89 "schema": {
90 "title": "Item Id",
91 "type": "integer",
92 },
93 },
94 ],
95 "responses": {
96 "200": {
97 "description": "Successful Response",
98 "content": {"application/json": {"schema": {}}},
99 },
100 "422": {
101 "description": "Validation Error",
102 "content": {
103 "application/json": {
104 "schema": {
105 "$ref": "#/components/schemas/HTTPValidationError"
106 }
107 }
108 },
109 },
110 },
111 "summary": "Update Item",
112 "operationId": "update_item_items__item_id__put",
113 "requestBody": {
114 "content": {
115 "application/json": {
116 "schema": {"$ref": "#/components/schemas/Item"}
117 }
118 },
119 "required": True,
120 },
121 }
122 }
123 },
124 "components": {
125 "schemas": {
126 "Item": {
127 "title": "Item",
128 "required": ["name", "price"],
129 "type": "object",
130 "properties": {
131 "name": {"title": "Name", "type": "string"},
132 "price": {"title": "Price", "type": "number"},
133 "description": {
134 "title": "Description",
135 "anyOf": [{"type": "string"}, {"type": "null"}],
136 },
137 "tax": {
138 "title": "Tax",
139 "anyOf": [{"type": "number"}, {"type": "null"}],
140 },
141 },
142 },
143 "ValidationError": {
144 "title": "ValidationError",
145 "required": ["loc", "msg", "type"],
146 "type": "object",
147 "properties": {
148 "loc": {
149 "title": "Location",
150 "type": "array",
151 "items": {
152 "anyOf": [{"type": "string"}, {"type": "integer"}]
153 },
154 },
155 "msg": {"title": "Message", "type": "string"},
156 "type": {"title": "Error Type", "type": "string"},
157 "input": {"title": "Input"},
158 "ctx": {"title": "Context", "type": "object"},
159 },
160 },
161 "HTTPValidationError": {
162 "title": "HTTPValidationError",
163 "type": "object",
164 "properties": {
165 "detail": {
166 "title": "Detail",
167 "type": "array",
168 "items": {
169 "$ref": "#/components/schemas/ValidationError"
170 },
171 }
172 },
173 },
174 }
175 },
176 }
177 )