Coverage for tests / test_tutorial / test_request_files / test_tutorial002.py: 100%
50 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import importlib 1abcd
3import pytest 1abcd
4from fastapi import FastAPI 1abcd
5from fastapi.testclient import TestClient 1abcd
6from inline_snapshot import snapshot 1abcd
9@pytest.fixture( 1abcd
10 name="app",
11 params=[
12 "tutorial002_py310",
13 "tutorial002_an_py310",
14 ],
15)
16def get_app(request: pytest.FixtureRequest): 1abcd
17 mod = importlib.import_module(f"docs_src.request_files.{request.param}") 1,-uvwx./yz:;AB=?CDEF@[GH]^_`IJKL{|MN}~
19 return mod.app 1,-uvwx./yz:;AB=?CDEF@[GH]^_`IJKL{|MN}~
22@pytest.fixture(name="client") 1abcd
23def get_client(app: FastAPI): 1abcd
24 client = TestClient(app) 1uvwxyzABCDEFGHIJKLMN
25 return client 1uvwxyzABCDEFGHIJKLMN
28def test_post_form_no_body(client: TestClient): 1abcd
29 response = client.post("/files/") 1WXYZ0123
30 assert response.status_code == 422, response.text 1WXYZ0123
31 assert response.json() == { 1WXYZ0123
32 "detail": [
33 {
34 "type": "missing",
35 "loc": ["body", "files"],
36 "msg": "Field required",
37 "input": None,
38 }
39 ]
40 }
43def test_post_body_json(client: TestClient): 1abcd
44 response = client.post("/files/", json={"file": "Foo"}) 1456789!#
45 assert response.status_code == 422, response.text 1456789!#
46 assert response.json() == { 1456789!#
47 "detail": [
48 {
49 "type": "missing",
50 "loc": ["body", "files"],
51 "msg": "Field required",
52 "input": None,
53 }
54 ]
55 }
58def test_post_files(tmp_path, app: FastAPI): 1abcd
59 path = tmp_path / "test.txt" 1efghijkl
60 path.write_bytes(b"<file content>") 1efghijkl
61 path2 = tmp_path / "test2.txt" 1efghijkl
62 path2.write_bytes(b"<file content2>") 1efghijkl
64 client = TestClient(app) 1efghijkl
65 with path.open("rb") as file, path2.open("rb") as file2: 1efghijkl
66 response = client.post( 1efghijkl
67 "/files/",
68 files=(
69 ("files", ("test.txt", file)),
70 ("files", ("test2.txt", file2)),
71 ),
72 )
73 assert response.status_code == 200, response.text 1efghijkl
74 assert response.json() == {"file_sizes": [14, 15]} 1efghijkl
77def test_post_upload_file(tmp_path, app: FastAPI): 1abcd
78 path = tmp_path / "test.txt" 1mnopqrst
79 path.write_bytes(b"<file content>") 1mnopqrst
80 path2 = tmp_path / "test2.txt" 1mnopqrst
81 path2.write_bytes(b"<file content2>") 1mnopqrst
83 client = TestClient(app) 1mnopqrst
84 with path.open("rb") as file, path2.open("rb") as file2: 1mnopqrst
85 response = client.post( 1mnopqrst
86 "/uploadfiles/",
87 files=(
88 ("files", ("test.txt", file)),
89 ("files", ("test2.txt", file2)),
90 ),
91 )
92 assert response.status_code == 200, response.text 1mnopqrst
93 assert response.json() == {"filenames": ["test.txt", "test2.txt"]} 1mnopqrst
96def test_get_root(app: FastAPI): 1abcd
97 client = TestClient(app) 1OPQRSTUV
98 response = client.get("/") 1OPQRSTUV
99 assert response.status_code == 200, response.text 1OPQRSTUV
100 assert b"<form" in response.content 1OPQRSTUV
103def test_openapi_schema(client: TestClient): 1abcd
104 response = client.get("/openapi.json") 1$%'()*+
105 assert response.status_code == 200, response.text 1$%'()*+
106 assert response.json() == snapshot( 1$%'()*+
107 {
108 "openapi": "3.1.0",
109 "info": {"title": "FastAPI", "version": "0.1.0"},
110 "paths": {
111 "/files/": {
112 "post": {
113 "responses": {
114 "200": {
115 "description": "Successful Response",
116 "content": {"application/json": {"schema": {}}},
117 },
118 "422": {
119 "description": "Validation Error",
120 "content": {
121 "application/json": {
122 "schema": {
123 "$ref": "#/components/schemas/HTTPValidationError"
124 }
125 }
126 },
127 },
128 },
129 "summary": "Create Files",
130 "operationId": "create_files_files__post",
131 "requestBody": {
132 "content": {
133 "multipart/form-data": {
134 "schema": {
135 "$ref": "#/components/schemas/Body_create_files_files__post"
136 }
137 }
138 },
139 "required": True,
140 },
141 }
142 },
143 "/uploadfiles/": {
144 "post": {
145 "responses": {
146 "200": {
147 "description": "Successful Response",
148 "content": {"application/json": {"schema": {}}},
149 },
150 "422": {
151 "description": "Validation Error",
152 "content": {
153 "application/json": {
154 "schema": {
155 "$ref": "#/components/schemas/HTTPValidationError"
156 }
157 }
158 },
159 },
160 },
161 "summary": "Create Upload Files",
162 "operationId": "create_upload_files_uploadfiles__post",
163 "requestBody": {
164 "content": {
165 "multipart/form-data": {
166 "schema": {
167 "$ref": "#/components/schemas/Body_create_upload_files_uploadfiles__post"
168 }
169 }
170 },
171 "required": True,
172 },
173 }
174 },
175 "/": {
176 "get": {
177 "responses": {
178 "200": {
179 "description": "Successful Response",
180 "content": {"application/json": {"schema": {}}},
181 }
182 },
183 "summary": "Main",
184 "operationId": "main__get",
185 }
186 },
187 },
188 "components": {
189 "schemas": {
190 "Body_create_upload_files_uploadfiles__post": {
191 "title": "Body_create_upload_files_uploadfiles__post",
192 "required": ["files"],
193 "type": "object",
194 "properties": {
195 "files": {
196 "title": "Files",
197 "type": "array",
198 "items": {
199 "type": "string",
200 "contentMediaType": "application/octet-stream",
201 },
202 }
203 },
204 },
205 "Body_create_files_files__post": {
206 "title": "Body_create_files_files__post",
207 "required": ["files"],
208 "type": "object",
209 "properties": {
210 "files": {
211 "title": "Files",
212 "type": "array",
213 "items": {
214 "type": "string",
215 "contentMediaType": "application/octet-stream",
216 },
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 "input": {"title": "Input"},
235 "ctx": {"title": "Context", "type": "object"},
236 },
237 },
238 "HTTPValidationError": {
239 "title": "HTTPValidationError",
240 "type": "object",
241 "properties": {
242 "detail": {
243 "title": "Detail",
244 "type": "array",
245 "items": {
246 "$ref": "#/components/schemas/ValidationError"
247 },
248 }
249 },
250 },
251 }
252 },
253 }
254 )