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

42 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from fastapi import FastAPI 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7from ...utils import needs_py39 1abcdef

8 

9 

10@pytest.fixture( 1abcdef

11 name="app", 

12 params=[ 

13 "tutorial003", 

14 "tutorial003_an", 

15 pytest.param("tutorial003_py39", marks=needs_py39), 

16 pytest.param("tutorial003_an_py39", marks=needs_py39), 

17 ], 

18) 

19def get_app(request: pytest.FixtureRequest): 1abcdef

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

21 

22 return mod.app 1abcdef

23 

24 

25@pytest.fixture(name="client") 1abcdef

26def get_client(app: FastAPI): 1abcdef

27 client = TestClient(app) 1abcdef

28 return client 1abcdef

29 

30 

31def test_post_files(tmp_path, app: FastAPI): 1abcdef

32 path = tmp_path / "test.txt" 1ghijkl

33 path.write_bytes(b"<file content>") 1ghijkl

34 path2 = tmp_path / "test2.txt" 1ghijkl

35 path2.write_bytes(b"<file content2>") 1ghijkl

36 

37 client = TestClient(app) 1ghijkl

38 with path.open("rb") as file, path2.open("rb") as file2: 1ghijkl

39 response = client.post( 1ghijkl

40 "/files/", 

41 files=( 

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

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

44 ), 

45 ) 

46 assert response.status_code == 200, response.text 1ghijkl

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

48 

49 

50def test_post_upload_file(tmp_path, app: FastAPI): 1abcdef

51 path = tmp_path / "test.txt" 1mnopqr

52 path.write_bytes(b"<file content>") 1mnopqr

53 path2 = tmp_path / "test2.txt" 1mnopqr

54 path2.write_bytes(b"<file content2>") 1mnopqr

55 

56 client = TestClient(app) 1mnopqr

57 with path.open("rb") as file, path2.open("rb") as file2: 1mnopqr

58 response = client.post( 1mnopqr

59 "/uploadfiles/", 

60 files=( 

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

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

63 ), 

64 ) 

65 assert response.status_code == 200, response.text 1mnopqr

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

67 

68 

69def test_get_root(app: FastAPI): 1abcdef

70 client = TestClient(app) 1stuvwx

71 response = client.get("/") 1stuvwx

72 assert response.status_code == 200, response.text 1stuvwx

73 assert b"<form" in response.content 1stuvwx

74 

75 

76def test_openapi_schema(client: TestClient): 1abcdef

77 response = client.get("/openapi.json") 1yzABCD

78 assert response.status_code == 200, response.text 1yzABCD

79 assert response.json() == { 1yzABCD

80 "openapi": "3.1.0", 

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

82 "paths": { 

83 "/files/": { 

84 "post": { 

85 "summary": "Create Files", 

86 "operationId": "create_files_files__post", 

87 "requestBody": { 

88 "content": { 

89 "multipart/form-data": { 

90 "schema": { 

91 "$ref": "#/components/schemas/Body_create_files_files__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 "/uploadfiles/": { 

116 "post": { 

117 "summary": "Create Upload Files", 

118 "operationId": "create_upload_files_uploadfiles__post", 

119 "requestBody": { 

120 "content": { 

121 "multipart/form-data": { 

122 "schema": { 

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

124 } 

125 } 

126 }, 

127 "required": True, 

128 }, 

129 "responses": { 

130 "200": { 

131 "description": "Successful Response", 

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

133 }, 

134 "422": { 

135 "description": "Validation Error", 

136 "content": { 

137 "application/json": { 

138 "schema": { 

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

140 } 

141 } 

142 }, 

143 }, 

144 }, 

145 } 

146 }, 

147 "/": { 

148 "get": { 

149 "summary": "Main", 

150 "operationId": "main__get", 

151 "responses": { 

152 "200": { 

153 "description": "Successful Response", 

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

155 } 

156 }, 

157 } 

158 }, 

159 }, 

160 "components": { 

161 "schemas": { 

162 "Body_create_files_files__post": { 

163 "title": "Body_create_files_files__post", 

164 "required": ["files"], 

165 "type": "object", 

166 "properties": { 

167 "files": { 

168 "title": "Files", 

169 "type": "array", 

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

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

172 } 

173 }, 

174 }, 

175 "Body_create_upload_files_uploadfiles__post": { 

176 "title": "Body_create_upload_files_uploadfiles__post", 

177 "required": ["files"], 

178 "type": "object", 

179 "properties": { 

180 "files": { 

181 "title": "Files", 

182 "type": "array", 

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

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

185 } 

186 }, 

187 }, 

188 "HTTPValidationError": { 

189 "title": "HTTPValidationError", 

190 "type": "object", 

191 "properties": { 

192 "detail": { 

193 "title": "Detail", 

194 "type": "array", 

195 "items": {"$ref": "#/components/schemas/ValidationError"}, 

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 }, 

214 }, 

215 } 

216 }, 

217 }