Coverage for tests / test_tutorial / test_request_files / test_tutorial003.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi import FastAPI 1abcd

5from fastapi.testclient import TestClient 1abcd

6from inline_snapshot import snapshot 1abcd

7 

8 

9@pytest.fixture( 1abcd

10 name="app", 

11 params=[ 

12 "tutorial003_py310", 

13 "tutorial003_an_py310", 

14 ], 

15) 

16def get_app(request: pytest.FixtureRequest): 1abcd

17 mod = importlib.import_module(f"docs_src.request_files.{request.param}") 1PQtuRSTUVWXYvwZ01234xy5678

18 

19 return mod.app 1PQtuRSTUVWXYvwZ01234xy5678

20 

21 

22@pytest.fixture(name="client") 1abcd

23def get_client(app: FastAPI): 1abcd

24 client = TestClient(app) 1tu9!vwxy

25 return client 1tu9!vwxy

26 

27 

28def test_post_files(tmp_path, app: FastAPI): 1abcd

29 path = tmp_path / "test.txt" 1efghijkl

30 path.write_bytes(b"<file content>") 1efghijkl

31 path2 = tmp_path / "test2.txt" 1efghijkl

32 path2.write_bytes(b"<file content2>") 1efghijkl

33 

34 client = TestClient(app) 1efghijkl

35 with path.open("rb") as file, path2.open("rb") as file2: 1efghijkl

36 response = client.post( 1efghijkl

37 "/files/", 

38 files=( 

39 ("files", ("test.txt", file)), 

40 ("files", ("test2.txt", file2)), 

41 ), 

42 ) 

43 assert response.status_code == 200, response.text 1efghijkl

44 assert response.json() == {"file_sizes": [14, 15]} 1efghijkl

45 

46 

47def test_post_upload_file(tmp_path, app: FastAPI): 1abcd

48 path = tmp_path / "test.txt" 1mnopqrs

49 path.write_bytes(b"<file content>") 1mnopqrs

50 path2 = tmp_path / "test2.txt" 1mnopqrs

51 path2.write_bytes(b"<file content2>") 1mnopqrs

52 

53 client = TestClient(app) 1mnopqrs

54 with path.open("rb") as file, path2.open("rb") as file2: 1mnopqrs

55 response = client.post( 1mnopqrs

56 "/uploadfiles/", 

57 files=( 

58 ("files", ("test.txt", file)), 

59 ("files", ("test2.txt", file2)), 

60 ), 

61 ) 

62 assert response.status_code == 200, response.text 1mnopqrs

63 assert response.json() == {"filenames": ["test.txt", "test2.txt"]} 1mnopqrs

64 

65 

66def test_get_root(app: FastAPI): 1abcd

67 client = TestClient(app) 1zABCDEFG

68 response = client.get("/") 1zABCDEFG

69 assert response.status_code == 200, response.text 1zABCDEFG

70 assert b"<form" in response.content 1zABCDEFG

71 

72 

73def test_openapi_schema(client: TestClient): 1abcd

74 response = client.get("/openapi.json") 1HIJKLMNO

75 assert response.status_code == 200, response.text 1HIJKLMNO

76 assert response.json() == snapshot( 1HIJKLMNO

77 { 

78 "openapi": "3.1.0", 

79 "info": {"title": "FastAPI", "version": "0.1.0"}, 

80 "paths": { 

81 "/files/": { 

82 "post": { 

83 "summary": "Create Files", 

84 "operationId": "create_files_files__post", 

85 "requestBody": { 

86 "content": { 

87 "multipart/form-data": { 

88 "schema": { 

89 "$ref": "#/components/schemas/Body_create_files_files__post" 

90 } 

91 } 

92 }, 

93 "required": True, 

94 }, 

95 "responses": { 

96 "200": { 

97 "description": "Successful Response", 

98 "content": {"application/json": {"schema": {}}}, 

99 }, 

100 "422": { 

101 "description": "Validation Error", 

102 "content": { 

103 "application/json": { 

104 "schema": { 

105 "$ref": "#/components/schemas/HTTPValidationError" 

106 } 

107 } 

108 }, 

109 }, 

110 }, 

111 } 

112 }, 

113 "/uploadfiles/": { 

114 "post": { 

115 "summary": "Create Upload Files", 

116 "operationId": "create_upload_files_uploadfiles__post", 

117 "requestBody": { 

118 "content": { 

119 "multipart/form-data": { 

120 "schema": { 

121 "$ref": "#/components/schemas/Body_create_upload_files_uploadfiles__post" 

122 } 

123 } 

124 }, 

125 "required": True, 

126 }, 

127 "responses": { 

128 "200": { 

129 "description": "Successful Response", 

130 "content": {"application/json": {"schema": {}}}, 

131 }, 

132 "422": { 

133 "description": "Validation Error", 

134 "content": { 

135 "application/json": { 

136 "schema": { 

137 "$ref": "#/components/schemas/HTTPValidationError" 

138 } 

139 } 

140 }, 

141 }, 

142 }, 

143 } 

144 }, 

145 "/": { 

146 "get": { 

147 "summary": "Main", 

148 "operationId": "main__get", 

149 "responses": { 

150 "200": { 

151 "description": "Successful Response", 

152 "content": {"application/json": {"schema": {}}}, 

153 } 

154 }, 

155 } 

156 }, 

157 }, 

158 "components": { 

159 "schemas": { 

160 "Body_create_files_files__post": { 

161 "title": "Body_create_files_files__post", 

162 "required": ["files"], 

163 "type": "object", 

164 "properties": { 

165 "files": { 

166 "title": "Files", 

167 "type": "array", 

168 "items": { 

169 "type": "string", 

170 "contentMediaType": "application/octet-stream", 

171 }, 

172 "description": "Multiple files as bytes", 

173 } 

174 }, 

175 }, 

176 "Body_create_upload_files_uploadfiles__post": { 

177 "title": "Body_create_upload_files_uploadfiles__post", 

178 "required": ["files"], 

179 "type": "object", 

180 "properties": { 

181 "files": { 

182 "title": "Files", 

183 "type": "array", 

184 "items": { 

185 "type": "string", 

186 "contentMediaType": "application/octet-stream", 

187 }, 

188 "description": "Multiple files as UploadFile", 

189 } 

190 }, 

191 }, 

192 "HTTPValidationError": { 

193 "title": "HTTPValidationError", 

194 "type": "object", 

195 "properties": { 

196 "detail": { 

197 "title": "Detail", 

198 "type": "array", 

199 "items": { 

200 "$ref": "#/components/schemas/ValidationError" 

201 }, 

202 } 

203 }, 

204 }, 

205 "ValidationError": { 

206 "title": "ValidationError", 

207 "required": ["loc", "msg", "type"], 

208 "type": "object", 

209 "properties": { 

210 "loc": { 

211 "title": "Location", 

212 "type": "array", 

213 "items": { 

214 "anyOf": [{"type": "string"}, {"type": "integer"}] 

215 }, 

216 }, 

217 "msg": {"title": "Message", "type": "string"}, 

218 "type": {"title": "Error Type", "type": "string"}, 

219 "input": {"title": "Input"}, 

220 "ctx": {"title": "Context", "type": "object"}, 

221 }, 

222 }, 

223 } 

224 }, 

225 } 

226 )