Coverage for tests/test_tutorial/test_request_files/test_tutorial002_py39.py: 100%
57 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1import pytest 1eabcd
2from dirty_equals import IsDict 1eabcd
3from fastapi import FastAPI 1eabcd
4from fastapi.testclient import TestClient 1eabcd
6from ...utils import needs_py39 1eabcd
9@pytest.fixture(name="app") 1eabcd
10def get_app(): 1eabcd
11 from docs_src.request_files.tutorial002_py39 import app 1abcd
13 return app 1abcd
16@pytest.fixture(name="client") 1eabcd
17def get_client(app: FastAPI): 1eabcd
18 client = TestClient(app) 1abcd
19 return client 1abcd
22file_required = { 1eabcd
23 "detail": [
24 {
25 "loc": ["body", "files"],
26 "msg": "field required",
27 "type": "value_error.missing",
28 }
29 ]
30}
33@needs_py39 1eabcd
34def test_post_form_no_body(client: TestClient): 1eabcd
35 response = client.post("/files/") 1abcd
36 assert response.status_code == 422, response.text 1abcd
37 assert response.json() == IsDict( 1abcd
38 {
39 "detail": [
40 {
41 "type": "missing",
42 "loc": ["body", "files"],
43 "msg": "Field required",
44 "input": None,
45 }
46 ]
47 }
48 ) | IsDict(
49 # TODO: remove when deprecating Pydantic v1
50 {
51 "detail": [
52 {
53 "loc": ["body", "files"],
54 "msg": "field required",
55 "type": "value_error.missing",
56 }
57 ]
58 }
59 )
62@needs_py39 1eabcd
63def test_post_body_json(client: TestClient): 1eabcd
64 response = client.post("/files/", json={"file": "Foo"}) 1abcd
65 assert response.status_code == 422, response.text 1abcd
66 assert response.json() == IsDict( 1abcd
67 {
68 "detail": [
69 {
70 "type": "missing",
71 "loc": ["body", "files"],
72 "msg": "Field required",
73 "input": None,
74 }
75 ]
76 }
77 ) | IsDict(
78 # TODO: remove when deprecating Pydantic v1
79 {
80 "detail": [
81 {
82 "loc": ["body", "files"],
83 "msg": "field required",
84 "type": "value_error.missing",
85 }
86 ]
87 }
88 )
91@needs_py39 1eabcd
92def test_post_files(tmp_path, app: FastAPI): 1eabcd
93 path = tmp_path / "test.txt" 1abcd
94 path.write_bytes(b"<file content>") 1abcd
95 path2 = tmp_path / "test2.txt" 1abcd
96 path2.write_bytes(b"<file content2>") 1abcd
98 client = TestClient(app) 1abcd
99 with path.open("rb") as file, path2.open("rb") as file2: 1abcd
100 response = client.post( 1abcd
101 "/files/",
102 files=(
103 ("files", ("test.txt", file)),
104 ("files", ("test2.txt", file2)),
105 ),
106 )
107 assert response.status_code == 200, response.text 1abcd
108 assert response.json() == {"file_sizes": [14, 15]} 1abcd
111@needs_py39 1eabcd
112def test_post_upload_file(tmp_path, app: FastAPI): 1eabcd
113 path = tmp_path / "test.txt" 1abcd
114 path.write_bytes(b"<file content>") 1abcd
115 path2 = tmp_path / "test2.txt" 1abcd
116 path2.write_bytes(b"<file content2>") 1abcd
118 client = TestClient(app) 1abcd
119 with path.open("rb") as file, path2.open("rb") as file2: 1abcd
120 response = client.post( 1abcd
121 "/uploadfiles/",
122 files=(
123 ("files", ("test.txt", file)),
124 ("files", ("test2.txt", file2)),
125 ),
126 )
127 assert response.status_code == 200, response.text 1abcd
128 assert response.json() == {"filenames": ["test.txt", "test2.txt"]} 1abcd
131@needs_py39 1eabcd
132def test_get_root(app: FastAPI): 1eabcd
133 client = TestClient(app) 1abcd
134 response = client.get("/") 1abcd
135 assert response.status_code == 200, response.text 1abcd
136 assert b"<form" in response.content 1abcd
139@needs_py39 1eabcd
140def test_openapi_schema(client: TestClient): 1eabcd
141 response = client.get("/openapi.json") 1abcd
142 assert response.status_code == 200, response.text 1abcd
143 assert response.json() == { 1abcd
144 "openapi": "3.1.0",
145 "info": {"title": "FastAPI", "version": "0.1.0"},
146 "paths": {
147 "/files/": {
148 "post": {
149 "responses": {
150 "200": {
151 "description": "Successful Response",
152 "content": {"application/json": {"schema": {}}},
153 },
154 "422": {
155 "description": "Validation Error",
156 "content": {
157 "application/json": {
158 "schema": {
159 "$ref": "#/components/schemas/HTTPValidationError"
160 }
161 }
162 },
163 },
164 },
165 "summary": "Create Files",
166 "operationId": "create_files_files__post",
167 "requestBody": {
168 "content": {
169 "multipart/form-data": {
170 "schema": {
171 "$ref": "#/components/schemas/Body_create_files_files__post"
172 }
173 }
174 },
175 "required": True,
176 },
177 }
178 },
179 "/uploadfiles/": {
180 "post": {
181 "responses": {
182 "200": {
183 "description": "Successful Response",
184 "content": {"application/json": {"schema": {}}},
185 },
186 "422": {
187 "description": "Validation Error",
188 "content": {
189 "application/json": {
190 "schema": {
191 "$ref": "#/components/schemas/HTTPValidationError"
192 }
193 }
194 },
195 },
196 },
197 "summary": "Create Upload Files",
198 "operationId": "create_upload_files_uploadfiles__post",
199 "requestBody": {
200 "content": {
201 "multipart/form-data": {
202 "schema": {
203 "$ref": "#/components/schemas/Body_create_upload_files_uploadfiles__post"
204 }
205 }
206 },
207 "required": True,
208 },
209 }
210 },
211 "/": {
212 "get": {
213 "responses": {
214 "200": {
215 "description": "Successful Response",
216 "content": {"application/json": {"schema": {}}},
217 }
218 },
219 "summary": "Main",
220 "operationId": "main__get",
221 }
222 },
223 },
224 "components": {
225 "schemas": {
226 "Body_create_upload_files_uploadfiles__post": {
227 "title": "Body_create_upload_files_uploadfiles__post",
228 "required": ["files"],
229 "type": "object",
230 "properties": {
231 "files": {
232 "title": "Files",
233 "type": "array",
234 "items": {"type": "string", "format": "binary"},
235 }
236 },
237 },
238 "Body_create_files_files__post": {
239 "title": "Body_create_files_files__post",
240 "required": ["files"],
241 "type": "object",
242 "properties": {
243 "files": {
244 "title": "Files",
245 "type": "array",
246 "items": {"type": "string", "format": "binary"},
247 }
248 },
249 },
250 "ValidationError": {
251 "title": "ValidationError",
252 "required": ["loc", "msg", "type"],
253 "type": "object",
254 "properties": {
255 "loc": {
256 "title": "Location",
257 "type": "array",
258 "items": {
259 "anyOf": [{"type": "string"}, {"type": "integer"}]
260 },
261 },
262 "msg": {"title": "Message", "type": "string"},
263 "type": {"title": "Error Type", "type": "string"},
264 },
265 },
266 "HTTPValidationError": {
267 "title": "HTTPValidationError",
268 "type": "object",
269 "properties": {
270 "detail": {
271 "title": "Detail",
272 "type": "array",
273 "items": {"$ref": "#/components/schemas/ValidationError"},
274 }
275 },
276 },
277 }
278 },
279 }