Coverage for tests/test_tutorial/test_request_files/test_tutorial001_02.py: 100%
32 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.tutorial001_02 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 == 200, response.text 1abcde
12 assert response.json() == {"message": "No file sent"} 1abcde
15def test_post_uploadfile_no_body(): 1abcde
16 response = client.post("/uploadfile/") 1abcde
17 assert response.status_code == 200, response.text 1abcde
18 assert response.json() == {"message": "No upload file sent"} 1abcde
21def test_post_file(tmp_path): 1abcde
22 path = tmp_path / "test.txt" 1abcde
23 path.write_bytes(b"<file content>") 1abcde
25 client = TestClient(app) 1abcde
26 with path.open("rb") as file: 1abcde
27 response = client.post("/files/", files={"file": file}) 1abcde
28 assert response.status_code == 200, response.text 1abcde
29 assert response.json() == {"file_size": 14} 1abcde
32def test_post_upload_file(tmp_path): 1abcde
33 path = tmp_path / "test.txt" 1abcde
34 path.write_bytes(b"<file content>") 1abcde
36 client = TestClient(app) 1abcde
37 with path.open("rb") as file: 1abcde
38 response = client.post("/uploadfile/", files={"file": file}) 1abcde
39 assert response.status_code == 200, response.text 1abcde
40 assert response.json() == {"filename": "test.txt"} 1abcde
43def test_openapi_schema(): 1abcde
44 response = client.get("/openapi.json") 1abcde
45 assert response.status_code == 200, response.text 1abcde
46 assert response.json() == { 1abcde
47 "openapi": "3.1.0",
48 "info": {"title": "FastAPI", "version": "0.1.0"},
49 "paths": {
50 "/files/": {
51 "post": {
52 "summary": "Create File",
53 "operationId": "create_file_files__post",
54 "requestBody": {
55 "content": {
56 "multipart/form-data": {
57 "schema": IsDict(
58 {
59 "allOf": [
60 {
61 "$ref": "#/components/schemas/Body_create_file_files__post"
62 }
63 ],
64 "title": "Body",
65 }
66 )
67 | IsDict(
68 # TODO: remove when deprecating Pydantic v1
69 {
70 "$ref": "#/components/schemas/Body_create_file_files__post"
71 }
72 )
73 }
74 }
75 },
76 "responses": {
77 "200": {
78 "description": "Successful Response",
79 "content": {"application/json": {"schema": {}}},
80 },
81 "422": {
82 "description": "Validation Error",
83 "content": {
84 "application/json": {
85 "schema": {
86 "$ref": "#/components/schemas/HTTPValidationError"
87 }
88 }
89 },
90 },
91 },
92 }
93 },
94 "/uploadfile/": {
95 "post": {
96 "summary": "Create Upload File",
97 "operationId": "create_upload_file_uploadfile__post",
98 "requestBody": {
99 "content": {
100 "multipart/form-data": {
101 "schema": IsDict(
102 {
103 "allOf": [
104 {
105 "$ref": "#/components/schemas/Body_create_upload_file_uploadfile__post"
106 }
107 ],
108 "title": "Body",
109 }
110 )
111 | IsDict(
112 # TODO: remove when deprecating Pydantic v1
113 {
114 "$ref": "#/components/schemas/Body_create_upload_file_uploadfile__post"
115 }
116 )
117 }
118 }
119 },
120 "responses": {
121 "200": {
122 "description": "Successful Response",
123 "content": {"application/json": {"schema": {}}},
124 },
125 "422": {
126 "description": "Validation Error",
127 "content": {
128 "application/json": {
129 "schema": {
130 "$ref": "#/components/schemas/HTTPValidationError"
131 }
132 }
133 },
134 },
135 },
136 }
137 },
138 },
139 "components": {
140 "schemas": {
141 "Body_create_file_files__post": {
142 "title": "Body_create_file_files__post",
143 "type": "object",
144 "properties": {
145 "file": IsDict(
146 {
147 "title": "File",
148 "anyOf": [
149 {"type": "string", "format": "binary"},
150 {"type": "null"},
151 ],
152 }
153 )
154 | IsDict(
155 # TODO: remove when deprecating Pydantic v1
156 {"title": "File", "type": "string", "format": "binary"}
157 )
158 },
159 },
160 "Body_create_upload_file_uploadfile__post": {
161 "title": "Body_create_upload_file_uploadfile__post",
162 "type": "object",
163 "properties": {
164 "file": IsDict(
165 {
166 "title": "File",
167 "anyOf": [
168 {"type": "string", "format": "binary"},
169 {"type": "null"},
170 ],
171 }
172 )
173 | IsDict(
174 # TODO: remove when deprecating Pydantic v1
175 {"title": "File", "type": "string", "format": "binary"}
176 )
177 },
178 },
179 "HTTPValidationError": {
180 "title": "HTTPValidationError",
181 "type": "object",
182 "properties": {
183 "detail": {
184 "title": "Detail",
185 "type": "array",
186 "items": {"$ref": "#/components/schemas/ValidationError"},
187 }
188 },
189 },
190 "ValidationError": {
191 "title": "ValidationError",
192 "required": ["loc", "msg", "type"],
193 "type": "object",
194 "properties": {
195 "loc": {
196 "title": "Location",
197 "type": "array",
198 "items": {
199 "anyOf": [{"type": "string"}, {"type": "integer"}]
200 },
201 },
202 "msg": {"title": "Message", "type": "string"},
203 "type": {"title": "Error Type", "type": "string"},
204 },
205 },
206 }
207 },
208 }