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