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-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi import FastAPI 1abdc

5from fastapi.testclient import TestClient 1abdc

6from inline_snapshot import snapshot 1abdc

7 

8 

9@pytest.fixture( 1abdc

10 name="app", 

11 params=[ 

12 "tutorial003_py310", 

13 "tutorial003_an_py310", 

14 ], 

15) 

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

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

18 

19 return mod.app 1abc

20 

21 

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

23def get_client(app: FastAPI): 1abdc

24 client = TestClient(app) 1abc

25 return client 1abc

26 

27 

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

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

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

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

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

33 

34 client = TestClient(app) 1efg

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

36 response = client.post( 1efg

37 "/files/", 

38 files=( 

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

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

41 ), 

42 ) 

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

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

45 

46 

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

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

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

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

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

52 

53 client = TestClient(app) 1hij

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

55 response = client.post( 1hij

56 "/uploadfiles/", 

57 files=( 

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

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

60 ), 

61 ) 

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

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

64 

65 

66def test_get_root(app: FastAPI): 1abdc

67 client = TestClient(app) 1klm

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

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

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

71 

72 

73def test_openapi_schema(client: TestClient): 1abdc

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

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

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

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": {"type": "string", "format": "binary"}, 

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

170 } 

171 }, 

172 }, 

173 "Body_create_upload_files_uploadfiles__post": { 

174 "title": "Body_create_upload_files_uploadfiles__post", 

175 "required": ["files"], 

176 "type": "object", 

177 "properties": { 

178 "files": { 

179 "title": "Files", 

180 "type": "array", 

181 "items": {"type": "string", "format": "binary"}, 

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

183 } 

184 }, 

185 }, 

186 "HTTPValidationError": { 

187 "title": "HTTPValidationError", 

188 "type": "object", 

189 "properties": { 

190 "detail": { 

191 "title": "Detail", 

192 "type": "array", 

193 "items": { 

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

195 }, 

196 } 

197 }, 

198 }, 

199 "ValidationError": { 

200 "title": "ValidationError", 

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

202 "type": "object", 

203 "properties": { 

204 "loc": { 

205 "title": "Location", 

206 "type": "array", 

207 "items": { 

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

209 }, 

210 }, 

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

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

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

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

215 }, 

216 }, 

217 } 

218 }, 

219 } 

220 )