Coverage for tests / test_tutorial / test_request_forms_and_files / test_tutorial001.py: 100%

47 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 "tutorial001_py310", 

13 "tutorial001_an_py310", 

14 ], 

15) 

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

17 mod = importlib.import_module(f"docs_src.request_forms_and_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_form_no_body(client: TestClient): 1abdc

29 response = client.post("/files/") 1klm

30 assert response.status_code == 422, response.text 1klm

31 assert response.json() == { 1klm

32 "detail": [ 

33 { 

34 "type": "missing", 

35 "loc": ["body", "file"], 

36 "msg": "Field required", 

37 "input": None, 

38 }, 

39 { 

40 "type": "missing", 

41 "loc": ["body", "fileb"], 

42 "msg": "Field required", 

43 "input": None, 

44 }, 

45 { 

46 "type": "missing", 

47 "loc": ["body", "token"], 

48 "msg": "Field required", 

49 "input": None, 

50 }, 

51 ] 

52 } 

53 

54 

55def test_post_form_no_file(client: TestClient): 1abdc

56 response = client.post("/files/", data={"token": "foo"}) 1nop

57 assert response.status_code == 422, response.text 1nop

58 assert response.json() == { 1nop

59 "detail": [ 

60 { 

61 "type": "missing", 

62 "loc": ["body", "file"], 

63 "msg": "Field required", 

64 "input": None, 

65 }, 

66 { 

67 "type": "missing", 

68 "loc": ["body", "fileb"], 

69 "msg": "Field required", 

70 "input": None, 

71 }, 

72 ] 

73 } 

74 

75 

76def test_post_body_json(client: TestClient): 1abdc

77 response = client.post("/files/", json={"file": "Foo", "token": "Bar"}) 1qrs

78 assert response.status_code == 422, response.text 1qrs

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

80 "detail": [ 

81 { 

82 "type": "missing", 

83 "loc": ["body", "file"], 

84 "msg": "Field required", 

85 "input": None, 

86 }, 

87 { 

88 "type": "missing", 

89 "loc": ["body", "fileb"], 

90 "msg": "Field required", 

91 "input": None, 

92 }, 

93 { 

94 "type": "missing", 

95 "loc": ["body", "token"], 

96 "msg": "Field required", 

97 "input": None, 

98 }, 

99 ] 

100 } 

101 

102 

103def test_post_file_no_token(tmp_path, app: FastAPI): 1abdc

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

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

106 

107 client = TestClient(app) 1hij

108 with path.open("rb") as file: 1hij

109 response = client.post("/files/", files={"file": file}) 1hij

110 assert response.status_code == 422, response.text 1hij

111 assert response.json() == { 1hij

112 "detail": [ 

113 { 

114 "type": "missing", 

115 "loc": ["body", "fileb"], 

116 "msg": "Field required", 

117 "input": None, 

118 }, 

119 { 

120 "type": "missing", 

121 "loc": ["body", "token"], 

122 "msg": "Field required", 

123 "input": None, 

124 }, 

125 ] 

126 } 

127 

128 

129def test_post_files_and_token(tmp_path, app: FastAPI): 1abdc

130 patha = tmp_path / "test.txt" 1efg

131 pathb = tmp_path / "testb.txt" 1efg

132 patha.write_text("<file content>") 1efg

133 pathb.write_text("<file b content>") 1efg

134 

135 client = TestClient(app) 1efg

136 with patha.open("rb") as filea, pathb.open("rb") as fileb: 1efg

137 response = client.post( 1efg

138 "/files/", 

139 data={"token": "foo"}, 

140 files={"file": filea, "fileb": ("testb.txt", fileb, "text/plain")}, 

141 ) 

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

143 assert response.json() == { 1efg

144 "file_size": 14, 

145 "token": "foo", 

146 "fileb_content_type": "text/plain", 

147 } 

148 

149 

150def test_openapi_schema(client: TestClient): 1abdc

151 response = client.get("/openapi.json") 1tuv

152 assert response.status_code == 200, response.text 1tuv

153 assert response.json() == snapshot( 1tuv

154 { 

155 "openapi": "3.1.0", 

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

157 "paths": { 

158 "/files/": { 

159 "post": { 

160 "responses": { 

161 "200": { 

162 "description": "Successful Response", 

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

164 }, 

165 "422": { 

166 "description": "Validation Error", 

167 "content": { 

168 "application/json": { 

169 "schema": { 

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

171 } 

172 } 

173 }, 

174 }, 

175 }, 

176 "summary": "Create File", 

177 "operationId": "create_file_files__post", 

178 "requestBody": { 

179 "content": { 

180 "multipart/form-data": { 

181 "schema": { 

182 "$ref": "#/components/schemas/Body_create_file_files__post" 

183 } 

184 } 

185 }, 

186 "required": True, 

187 }, 

188 } 

189 } 

190 }, 

191 "components": { 

192 "schemas": { 

193 "Body_create_file_files__post": { 

194 "title": "Body_create_file_files__post", 

195 "required": ["file", "fileb", "token"], 

196 "type": "object", 

197 "properties": { 

198 "file": { 

199 "title": "File", 

200 "type": "string", 

201 "format": "binary", 

202 }, 

203 "fileb": { 

204 "title": "Fileb", 

205 "type": "string", 

206 "format": "binary", 

207 }, 

208 "token": {"title": "Token", "type": "string"}, 

209 }, 

210 }, 

211 "ValidationError": { 

212 "title": "ValidationError", 

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

214 "type": "object", 

215 "properties": { 

216 "loc": { 

217 "title": "Location", 

218 "type": "array", 

219 "items": { 

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

221 }, 

222 }, 

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

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

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

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

227 }, 

228 }, 

229 "HTTPValidationError": { 

230 "title": "HTTPValidationError", 

231 "type": "object", 

232 "properties": { 

233 "detail": { 

234 "title": "Detail", 

235 "type": "array", 

236 "items": { 

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

238 }, 

239 } 

240 }, 

241 }, 

242 } 

243 }, 

244 } 

245 )