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