Coverage for tests/test_tutorial/test_request_files/test_tutorial003_py39.py: 100%
46 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 fastapi import FastAPI 1eabcd
3from fastapi.testclient import TestClient 1eabcd
5from ...utils import needs_py39 1eabcd
8@pytest.fixture(name="app") 1eabcd
9def get_app(): 1eabcd
10 from docs_src.request_files.tutorial003_py39 import app 1abcd
12 return app 1abcd
15@pytest.fixture(name="client") 1eabcd
16def get_client(app: FastAPI): 1eabcd
17 client = TestClient(app) 1abcd
18 return client 1abcd
21file_required = { 1eabcd
22 "detail": [
23 {
24 "loc": ["body", "files"],
25 "msg": "field required",
26 "type": "value_error.missing",
27 }
28 ]
29}
32@needs_py39 1eabcd
33def test_post_files(tmp_path, app: FastAPI): 1eabcd
34 path = tmp_path / "test.txt" 1abcd
35 path.write_bytes(b"<file content>") 1abcd
36 path2 = tmp_path / "test2.txt" 1abcd
37 path2.write_bytes(b"<file content2>") 1abcd
39 client = TestClient(app) 1abcd
40 with path.open("rb") as file, path2.open("rb") as file2: 1abcd
41 response = client.post( 1abcd
42 "/files/",
43 files=(
44 ("files", ("test.txt", file)),
45 ("files", ("test2.txt", file2)),
46 ),
47 )
48 assert response.status_code == 200, response.text 1abcd
49 assert response.json() == {"file_sizes": [14, 15]} 1abcd
52@needs_py39 1eabcd
53def test_post_upload_file(tmp_path, app: FastAPI): 1eabcd
54 path = tmp_path / "test.txt" 1abcd
55 path.write_bytes(b"<file content>") 1abcd
56 path2 = tmp_path / "test2.txt" 1abcd
57 path2.write_bytes(b"<file content2>") 1abcd
59 client = TestClient(app) 1abcd
60 with path.open("rb") as file, path2.open("rb") as file2: 1abcd
61 response = client.post( 1abcd
62 "/uploadfiles/",
63 files=(
64 ("files", ("test.txt", file)),
65 ("files", ("test2.txt", file2)),
66 ),
67 )
68 assert response.status_code == 200, response.text 1abcd
69 assert response.json() == {"filenames": ["test.txt", "test2.txt"]} 1abcd
72@needs_py39 1eabcd
73def test_get_root(app: FastAPI): 1eabcd
74 client = TestClient(app) 1abcd
75 response = client.get("/") 1abcd
76 assert response.status_code == 200, response.text 1abcd
77 assert b"<form" in response.content 1abcd
80@needs_py39 1eabcd
81def test_openapi_schema(client: TestClient): 1eabcd
82 response = client.get("/openapi.json") 1abcd
83 assert response.status_code == 200, response.text 1abcd
84 assert response.json() == { 1abcd
85 "openapi": "3.1.0",
86 "info": {"title": "FastAPI", "version": "0.1.0"},
87 "paths": {
88 "/files/": {
89 "post": {
90 "summary": "Create Files",
91 "operationId": "create_files_files__post",
92 "requestBody": {
93 "content": {
94 "multipart/form-data": {
95 "schema": {
96 "$ref": "#/components/schemas/Body_create_files_files__post"
97 }
98 }
99 },
100 "required": True,
101 },
102 "responses": {
103 "200": {
104 "description": "Successful Response",
105 "content": {"application/json": {"schema": {}}},
106 },
107 "422": {
108 "description": "Validation Error",
109 "content": {
110 "application/json": {
111 "schema": {
112 "$ref": "#/components/schemas/HTTPValidationError"
113 }
114 }
115 },
116 },
117 },
118 }
119 },
120 "/uploadfiles/": {
121 "post": {
122 "summary": "Create Upload Files",
123 "operationId": "create_upload_files_uploadfiles__post",
124 "requestBody": {
125 "content": {
126 "multipart/form-data": {
127 "schema": {
128 "$ref": "#/components/schemas/Body_create_upload_files_uploadfiles__post"
129 }
130 }
131 },
132 "required": True,
133 },
134 "responses": {
135 "200": {
136 "description": "Successful Response",
137 "content": {"application/json": {"schema": {}}},
138 },
139 "422": {
140 "description": "Validation Error",
141 "content": {
142 "application/json": {
143 "schema": {
144 "$ref": "#/components/schemas/HTTPValidationError"
145 }
146 }
147 },
148 },
149 },
150 }
151 },
152 "/": {
153 "get": {
154 "summary": "Main",
155 "operationId": "main__get",
156 "responses": {
157 "200": {
158 "description": "Successful Response",
159 "content": {"application/json": {"schema": {}}},
160 }
161 },
162 }
163 },
164 },
165 "components": {
166 "schemas": {
167 "Body_create_files_files__post": {
168 "title": "Body_create_files_files__post",
169 "required": ["files"],
170 "type": "object",
171 "properties": {
172 "files": {
173 "title": "Files",
174 "type": "array",
175 "items": {"type": "string", "format": "binary"},
176 "description": "Multiple files as bytes",
177 }
178 },
179 },
180 "Body_create_upload_files_uploadfiles__post": {
181 "title": "Body_create_upload_files_uploadfiles__post",
182 "required": ["files"],
183 "type": "object",
184 "properties": {
185 "files": {
186 "title": "Files",
187 "type": "array",
188 "items": {"type": "string", "format": "binary"},
189 "description": "Multiple files as UploadFile",
190 }
191 },
192 },
193 "HTTPValidationError": {
194 "title": "HTTPValidationError",
195 "type": "object",
196 "properties": {
197 "detail": {
198 "title": "Detail",
199 "type": "array",
200 "items": {"$ref": "#/components/schemas/ValidationError"},
201 }
202 },
203 },
204 "ValidationError": {
205 "title": "ValidationError",
206 "required": ["loc", "msg", "type"],
207 "type": "object",
208 "properties": {
209 "loc": {
210 "title": "Location",
211 "type": "array",
212 "items": {
213 "anyOf": [{"type": "string"}, {"type": "integer"}]
214 },
215 },
216 "msg": {"title": "Message", "type": "string"},
217 "type": {"title": "Error Type", "type": "string"},
218 },
219 },
220 }
221 },
222 }