Coverage for tests / test_tutorial / test_request_files / test_tutorial001_03.py: 100%
27 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
8@pytest.fixture( 1abdc
9 name="client",
10 params=[
11 "tutorial001_03_py310",
12 "tutorial001_03_an_py310",
13 ],
14)
15def get_client(request: pytest.FixtureRequest): 1abdc
16 mod = importlib.import_module(f"docs_src.request_files.{request.param}") 1abc
18 client = TestClient(mod.app) 1abc
19 return client 1abc
22def test_post_file(tmp_path, client: TestClient): 1abdc
23 path = tmp_path / "test.txt" 1efg
24 path.write_bytes(b"<file content>") 1efg
26 with path.open("rb") as file: 1efg
27 response = client.post("/files/", files={"file": file}) 1efg
28 assert response.status_code == 200, response.text 1efg
29 assert response.json() == {"file_size": 14} 1efg
32def test_post_upload_file(tmp_path, client: TestClient): 1abdc
33 path = tmp_path / "test.txt" 1hij
34 path.write_bytes(b"<file content>") 1hij
36 with path.open("rb") as file: 1hij
37 response = client.post("/uploadfile/", files={"file": file}) 1hij
38 assert response.status_code == 200, response.text 1hij
39 assert response.json() == {"filename": "test.txt"} 1hij
42def test_openapi_schema(client: TestClient): 1abdc
43 response = client.get("/openapi.json") 1klm
44 assert response.status_code == 200, response.text 1klm
45 assert response.json() == snapshot( 1klm
46 {
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": {
58 "$ref": "#/components/schemas/Body_create_file_files__post"
59 }
60 }
61 },
62 "required": True,
63 },
64 "responses": {
65 "200": {
66 "description": "Successful Response",
67 "content": {"application/json": {"schema": {}}},
68 },
69 "422": {
70 "description": "Validation Error",
71 "content": {
72 "application/json": {
73 "schema": {
74 "$ref": "#/components/schemas/HTTPValidationError"
75 }
76 }
77 },
78 },
79 },
80 }
81 },
82 "/uploadfile/": {
83 "post": {
84 "summary": "Create Upload File",
85 "operationId": "create_upload_file_uploadfile__post",
86 "requestBody": {
87 "content": {
88 "multipart/form-data": {
89 "schema": {
90 "$ref": "#/components/schemas/Body_create_upload_file_uploadfile__post"
91 }
92 }
93 },
94 "required": True,
95 },
96 "responses": {
97 "200": {
98 "description": "Successful Response",
99 "content": {"application/json": {"schema": {}}},
100 },
101 "422": {
102 "description": "Validation Error",
103 "content": {
104 "application/json": {
105 "schema": {
106 "$ref": "#/components/schemas/HTTPValidationError"
107 }
108 }
109 },
110 },
111 },
112 }
113 },
114 },
115 "components": {
116 "schemas": {
117 "Body_create_file_files__post": {
118 "title": "Body_create_file_files__post",
119 "required": ["file"],
120 "type": "object",
121 "properties": {
122 "file": {
123 "title": "File",
124 "type": "string",
125 "description": "A file read as bytes",
126 "format": "binary",
127 }
128 },
129 },
130 "Body_create_upload_file_uploadfile__post": {
131 "title": "Body_create_upload_file_uploadfile__post",
132 "required": ["file"],
133 "type": "object",
134 "properties": {
135 "file": {
136 "title": "File",
137 "type": "string",
138 "description": "A file read as UploadFile",
139 "format": "binary",
140 }
141 },
142 },
143 "HTTPValidationError": {
144 "title": "HTTPValidationError",
145 "type": "object",
146 "properties": {
147 "detail": {
148 "title": "Detail",
149 "type": "array",
150 "items": {
151 "$ref": "#/components/schemas/ValidationError"
152 },
153 }
154 },
155 },
156 "ValidationError": {
157 "title": "ValidationError",
158 "required": ["loc", "msg", "type"],
159 "type": "object",
160 "properties": {
161 "loc": {
162 "title": "Location",
163 "type": "array",
164 "items": {
165 "anyOf": [{"type": "string"}, {"type": "integer"}]
166 },
167 },
168 "msg": {"title": "Message", "type": "string"},
169 "type": {"title": "Error Type", "type": "string"},
170 "input": {"title": "Input"},
171 "ctx": {"title": "Context", "type": "object"},
172 },
173 },
174 }
175 },
176 }
177 )