Coverage for tests / test_tutorial / test_encoder / test_tutorial001.py: 100%
30 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
2from types import ModuleType 1abdc
4import pytest 1abdc
5from fastapi.testclient import TestClient 1abdc
6from inline_snapshot import snapshot 1abdc
8from ...utils import needs_py310 1abdc
11@pytest.fixture( 1abdc
12 name="mod",
13 params=[
14 pytest.param("tutorial001_py310", marks=needs_py310),
15 ],
16)
17def get_module(request: pytest.FixtureRequest): 1abdc
18 module = importlib.import_module(f"docs_src.encoder.{request.param}") 1abc
19 return module 1abc
22@pytest.fixture(name="client") 1abdc
23def get_client(mod: ModuleType): 1abdc
24 client = TestClient(mod.app) 1abc
25 return client 1abc
28def test_put(client: TestClient, mod: ModuleType): 1abdc
29 fake_db = mod.fake_db 1efg
31 response = client.put( 1efg
32 "/items/123",
33 json={
34 "title": "Foo",
35 "timestamp": "2023-01-01T12:00:00",
36 "description": "An optional description",
37 },
38 )
39 assert response.status_code == 200 1efg
40 assert "123" in fake_db 1efg
41 assert fake_db["123"] == { 1efg
42 "title": "Foo",
43 "timestamp": "2023-01-01T12:00:00",
44 "description": "An optional description",
45 }
48def test_put_invalid_data(client: TestClient, mod: ModuleType): 1abdc
49 fake_db = mod.fake_db 1hij
51 response = client.put( 1hij
52 "/items/345",
53 json={
54 "title": "Foo",
55 "timestamp": "not a date",
56 },
57 )
58 assert response.status_code == 422 1hij
59 assert response.json() == { 1hij
60 "detail": [
61 {
62 "loc": ["body", "timestamp"],
63 "msg": "Input should be a valid datetime or date, invalid character in year",
64 "type": "datetime_from_date_parsing",
65 "input": "not a date",
66 "ctx": {"error": "invalid character in year"},
67 }
68 ]
69 }
70 assert "345" not in fake_db 1hij
73def test_openapi_schema(client: TestClient): 1abdc
74 response = client.get("/openapi.json") 1klm
75 assert response.status_code == 200 1klm
76 assert response.json() == snapshot( 1klm
77 {
78 "openapi": "3.1.0",
79 "info": {"title": "FastAPI", "version": "0.1.0"},
80 "paths": {
81 "/items/{id}": {
82 "put": {
83 "operationId": "update_item_items__id__put",
84 "parameters": [
85 {
86 "in": "path",
87 "name": "id",
88 "required": True,
89 "schema": {
90 "title": "Id",
91 "type": "string",
92 },
93 },
94 ],
95 "requestBody": {
96 "content": {
97 "application/json": {
98 "schema": {
99 "$ref": "#/components/schemas/Item",
100 },
101 },
102 },
103 "required": True,
104 },
105 "responses": {
106 "200": {
107 "content": {
108 "application/json": {
109 "schema": {},
110 },
111 },
112 "description": "Successful Response",
113 },
114 "422": {
115 "content": {
116 "application/json": {
117 "schema": {
118 "$ref": "#/components/schemas/HTTPValidationError",
119 },
120 },
121 },
122 "description": "Validation Error",
123 },
124 },
125 "summary": "Update Item",
126 },
127 },
128 },
129 "components": {
130 "schemas": {
131 "HTTPValidationError": {
132 "properties": {
133 "detail": {
134 "items": {
135 "$ref": "#/components/schemas/ValidationError",
136 },
137 "title": "Detail",
138 "type": "array",
139 },
140 },
141 "title": "HTTPValidationError",
142 "type": "object",
143 },
144 "Item": {
145 "properties": {
146 "description": {
147 "anyOf": [
148 {
149 "type": "string",
150 },
151 {
152 "type": "null",
153 },
154 ],
155 "title": "Description",
156 },
157 "timestamp": {
158 "format": "date-time",
159 "title": "Timestamp",
160 "type": "string",
161 },
162 "title": {
163 "title": "Title",
164 "type": "string",
165 },
166 },
167 "required": [
168 "title",
169 "timestamp",
170 ],
171 "title": "Item",
172 "type": "object",
173 },
174 "ValidationError": {
175 "properties": {
176 "ctx": {"title": "Context", "type": "object"},
177 "input": {"title": "Input"},
178 "loc": {
179 "items": {
180 "anyOf": [
181 {
182 "type": "string",
183 },
184 {
185 "type": "integer",
186 },
187 ],
188 },
189 "title": "Location",
190 "type": "array",
191 },
192 "msg": {
193 "title": "Message",
194 "type": "string",
195 },
196 "type": {
197 "title": "Error Type",
198 "type": "string",
199 },
200 },
201 "required": [
202 "loc",
203 "msg",
204 "type",
205 ],
206 "title": "ValidationError",
207 "type": "object",
208 },
209 },
210 },
211 }
212 )