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

43 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.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7 

8@pytest.fixture( 1abdc

9 name="client", 

10 params=[ 

11 "tutorial001_py310", 

12 "tutorial001_an_py310", 

13 ], 

14) 

15def get_client(request: pytest.FixtureRequest): 1abdc

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

17 

18 client = TestClient(mod.app) 1abc

19 return client 1abc

20 

21 

22def test_post_form_no_body(client: TestClient): 1abdc

23 response = client.post("/files/") 1nop

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

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

26 "detail": [ 

27 { 

28 "type": "missing", 

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

30 "msg": "Field required", 

31 "input": None, 

32 } 

33 ] 

34 } 

35 

36 

37def test_post_body_json(client: TestClient): 1abdc

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

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

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

41 "detail": [ 

42 { 

43 "type": "missing", 

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

45 "msg": "Field required", 

46 "input": None, 

47 } 

48 ] 

49 } 

50 

51 

52def test_post_file(tmp_path, client: TestClient): 1abdc

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

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

55 

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

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

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

59 assert response.json() == {"file_size": 14} 1hij

60 

61 

62def test_post_large_file(tmp_path, client: TestClient): 1abdc

63 default_pydantic_max_size = 2**16 1efg

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

65 path.write_bytes(b"x" * (default_pydantic_max_size + 1)) 1efg

66 

67 with path.open("rb") as file: 1efg

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

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

70 assert response.json() == {"file_size": default_pydantic_max_size + 1} 1efg

71 

72 

73def test_post_upload_file(tmp_path, client: TestClient): 1abdc

74 path = tmp_path / "test.txt" 1klm

75 path.write_bytes(b"<file content>") 1klm

76 

77 with path.open("rb") as file: 1klm

78 response = client.post("/uploadfile/", files={"file": file}) 1klm

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

80 assert response.json() == {"filename": "test.txt"} 1klm

81 

82 

83def test_openapi_schema(client: TestClient): 1abdc

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

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

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

87 { 

88 "openapi": "3.1.0", 

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

90 "paths": { 

91 "/files/": { 

92 "post": { 

93 "responses": { 

94 "200": { 

95 "description": "Successful Response", 

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

97 }, 

98 "422": { 

99 "description": "Validation Error", 

100 "content": { 

101 "application/json": { 

102 "schema": { 

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

104 } 

105 } 

106 }, 

107 }, 

108 }, 

109 "summary": "Create File", 

110 "operationId": "create_file_files__post", 

111 "requestBody": { 

112 "content": { 

113 "multipart/form-data": { 

114 "schema": { 

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

116 } 

117 } 

118 }, 

119 "required": True, 

120 }, 

121 } 

122 }, 

123 "/uploadfile/": { 

124 "post": { 

125 "responses": { 

126 "200": { 

127 "description": "Successful Response", 

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

129 }, 

130 "422": { 

131 "description": "Validation Error", 

132 "content": { 

133 "application/json": { 

134 "schema": { 

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

136 } 

137 } 

138 }, 

139 }, 

140 }, 

141 "summary": "Create Upload File", 

142 "operationId": "create_upload_file_uploadfile__post", 

143 "requestBody": { 

144 "content": { 

145 "multipart/form-data": { 

146 "schema": { 

147 "$ref": "#/components/schemas/Body_create_upload_file_uploadfile__post" 

148 } 

149 } 

150 }, 

151 "required": True, 

152 }, 

153 } 

154 }, 

155 }, 

156 "components": { 

157 "schemas": { 

158 "Body_create_upload_file_uploadfile__post": { 

159 "title": "Body_create_upload_file_uploadfile__post", 

160 "required": ["file"], 

161 "type": "object", 

162 "properties": { 

163 "file": { 

164 "title": "File", 

165 "type": "string", 

166 "format": "binary", 

167 } 

168 }, 

169 }, 

170 "Body_create_file_files__post": { 

171 "title": "Body_create_file_files__post", 

172 "required": ["file"], 

173 "type": "object", 

174 "properties": { 

175 "file": { 

176 "title": "File", 

177 "type": "string", 

178 "format": "binary", 

179 } 

180 }, 

181 }, 

182 "ValidationError": { 

183 "title": "ValidationError", 

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

185 "type": "object", 

186 "properties": { 

187 "loc": { 

188 "title": "Location", 

189 "type": "array", 

190 "items": { 

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

192 }, 

193 }, 

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

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

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

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

198 }, 

199 }, 

200 "HTTPValidationError": { 

201 "title": "HTTPValidationError", 

202 "type": "object", 

203 "properties": { 

204 "detail": { 

205 "title": "Detail", 

206 "type": "array", 

207 "items": { 

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

209 }, 

210 } 

211 }, 

212 }, 

213 } 

214 }, 

215 } 

216 )