Coverage for tests/test_tutorial/test_request_files/test_tutorial001_03.py: 100%
27 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from fastapi.testclient import TestClient 1abcdef
6from ...utils import needs_py39 1abcdef
9@pytest.fixture( 1abcdef
10 name="client",
11 params=[
12 "tutorial001_03",
13 "tutorial001_03_an",
14 pytest.param("tutorial001_03_an_py39", marks=needs_py39),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abcdef
18 mod = importlib.import_module(f"docs_src.request_files.{request.param}") 1abcdef
20 client = TestClient(mod.app) 1abcdef
21 return client 1abcdef
24def test_post_file(tmp_path, client: TestClient): 1abcdef
25 path = tmp_path / "test.txt" 1ghijkl
26 path.write_bytes(b"<file content>") 1ghijkl
28 with path.open("rb") as file: 1ghijkl
29 response = client.post("/files/", files={"file": file}) 1ghijkl
30 assert response.status_code == 200, response.text 1ghijkl
31 assert response.json() == {"file_size": 14} 1ghijkl
34def test_post_upload_file(tmp_path, client: TestClient): 1abcdef
35 path = tmp_path / "test.txt" 1mnopqr
36 path.write_bytes(b"<file content>") 1mnopqr
38 with path.open("rb") as file: 1mnopqr
39 response = client.post("/uploadfile/", files={"file": file}) 1mnopqr
40 assert response.status_code == 200, response.text 1mnopqr
41 assert response.json() == {"filename": "test.txt"} 1mnopqr
44def test_openapi_schema(client: TestClient): 1abcdef
45 response = client.get("/openapi.json") 1stuvwx
46 assert response.status_code == 200, response.text 1stuvwx
47 assert response.json() == { 1stuvwx
48 "openapi": "3.1.0",
49 "info": {"title": "FastAPI", "version": "0.1.0"},
50 "paths": {
51 "/files/": {
52 "post": {
53 "summary": "Create File",
54 "operationId": "create_file_files__post",
55 "requestBody": {
56 "content": {
57 "multipart/form-data": {
58 "schema": {
59 "$ref": "#/components/schemas/Body_create_file_files__post"
60 }
61 }
62 },
63 "required": True,
64 },
65 "responses": {
66 "200": {
67 "description": "Successful Response",
68 "content": {"application/json": {"schema": {}}},
69 },
70 "422": {
71 "description": "Validation Error",
72 "content": {
73 "application/json": {
74 "schema": {
75 "$ref": "#/components/schemas/HTTPValidationError"
76 }
77 }
78 },
79 },
80 },
81 }
82 },
83 "/uploadfile/": {
84 "post": {
85 "summary": "Create Upload File",
86 "operationId": "create_upload_file_uploadfile__post",
87 "requestBody": {
88 "content": {
89 "multipart/form-data": {
90 "schema": {
91 "$ref": "#/components/schemas/Body_create_upload_file_uploadfile__post"
92 }
93 }
94 },
95 "required": True,
96 },
97 "responses": {
98 "200": {
99 "description": "Successful Response",
100 "content": {"application/json": {"schema": {}}},
101 },
102 "422": {
103 "description": "Validation Error",
104 "content": {
105 "application/json": {
106 "schema": {
107 "$ref": "#/components/schemas/HTTPValidationError"
108 }
109 }
110 },
111 },
112 },
113 }
114 },
115 },
116 "components": {
117 "schemas": {
118 "Body_create_file_files__post": {
119 "title": "Body_create_file_files__post",
120 "required": ["file"],
121 "type": "object",
122 "properties": {
123 "file": {
124 "title": "File",
125 "type": "string",
126 "description": "A file read as bytes",
127 "format": "binary",
128 }
129 },
130 },
131 "Body_create_upload_file_uploadfile__post": {
132 "title": "Body_create_upload_file_uploadfile__post",
133 "required": ["file"],
134 "type": "object",
135 "properties": {
136 "file": {
137 "title": "File",
138 "type": "string",
139 "description": "A file read as UploadFile",
140 "format": "binary",
141 }
142 },
143 },
144 "HTTPValidationError": {
145 "title": "HTTPValidationError",
146 "type": "object",
147 "properties": {
148 "detail": {
149 "title": "Detail",
150 "type": "array",
151 "items": {"$ref": "#/components/schemas/ValidationError"},
152 }
153 },
154 },
155 "ValidationError": {
156 "title": "ValidationError",
157 "required": ["loc", "msg", "type"],
158 "type": "object",
159 "properties": {
160 "loc": {
161 "title": "Location",
162 "type": "array",
163 "items": {
164 "anyOf": [{"type": "string"}, {"type": "integer"}]
165 },
166 },
167 "msg": {"title": "Message", "type": "string"},
168 "type": {"title": "Error Type", "type": "string"},
169 },
170 },
171 }
172 },
173 }