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

42 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from dirty_equals import IsDict 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from docs_src.request_files.tutorial001 import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9file_required = { 1abcde

10 "detail": [ 

11 { 

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

13 "msg": "field required", 

14 "type": "value_error.missing", 

15 } 

16 ] 

17} 

18 

19 

20def test_post_form_no_body(): 1abcde

21 response = client.post("/files/") 1abcde

22 assert response.status_code == 422, response.text 1abcde

23 assert response.json() == IsDict( 1abcde

24 { 

25 "detail": [ 

26 { 

27 "type": "missing", 

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

29 "msg": "Field required", 

30 "input": None, 

31 } 

32 ] 

33 } 

34 ) | IsDict( 

35 # TODO: remove when deprecating Pydantic v1 

36 { 

37 "detail": [ 

38 { 

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

40 "msg": "field required", 

41 "type": "value_error.missing", 

42 } 

43 ] 

44 } 

45 ) 

46 

47 

48def test_post_body_json(): 1abcde

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

50 assert response.status_code == 422, response.text 1abcde

51 assert response.json() == IsDict( 1abcde

52 { 

53 "detail": [ 

54 { 

55 "type": "missing", 

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

57 "msg": "Field required", 

58 "input": None, 

59 } 

60 ] 

61 } 

62 ) | IsDict( 

63 # TODO: remove when deprecating Pydantic v1 

64 { 

65 "detail": [ 

66 { 

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

68 "msg": "field required", 

69 "type": "value_error.missing", 

70 } 

71 ] 

72 } 

73 ) 

74 

75 

76def test_post_file(tmp_path): 1abcde

77 path = tmp_path / "test.txt" 1abcde

78 path.write_bytes(b"<file content>") 1abcde

79 

80 client = TestClient(app) 1abcde

81 with path.open("rb") as file: 1abcde

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

83 assert response.status_code == 200, response.text 1abcde

84 assert response.json() == {"file_size": 14} 1abcde

85 

86 

87def test_post_large_file(tmp_path): 1abcde

88 default_pydantic_max_size = 2**16 1abcde

89 path = tmp_path / "test.txt" 1abcde

90 path.write_bytes(b"x" * (default_pydantic_max_size + 1)) 1abcde

91 

92 client = TestClient(app) 1abcde

93 with path.open("rb") as file: 1abcde

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

95 assert response.status_code == 200, response.text 1abcde

96 assert response.json() == {"file_size": default_pydantic_max_size + 1} 1abcde

97 

98 

99def test_post_upload_file(tmp_path): 1abcde

100 path = tmp_path / "test.txt" 1abcde

101 path.write_bytes(b"<file content>") 1abcde

102 

103 client = TestClient(app) 1abcde

104 with path.open("rb") as file: 1abcde

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

106 assert response.status_code == 200, response.text 1abcde

107 assert response.json() == {"filename": "test.txt"} 1abcde

108 

109 

110def test_openapi_schema(): 1abcde

111 response = client.get("/openapi.json") 1abcde

112 assert response.status_code == 200, response.text 1abcde

113 assert response.json() == { 1abcde

114 "openapi": "3.1.0", 

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

116 "paths": { 

117 "/files/": { 

118 "post": { 

119 "responses": { 

120 "200": { 

121 "description": "Successful Response", 

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

123 }, 

124 "422": { 

125 "description": "Validation Error", 

126 "content": { 

127 "application/json": { 

128 "schema": { 

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

130 } 

131 } 

132 }, 

133 }, 

134 }, 

135 "summary": "Create File", 

136 "operationId": "create_file_files__post", 

137 "requestBody": { 

138 "content": { 

139 "multipart/form-data": { 

140 "schema": { 

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

142 } 

143 } 

144 }, 

145 "required": True, 

146 }, 

147 } 

148 }, 

149 "/uploadfile/": { 

150 "post": { 

151 "responses": { 

152 "200": { 

153 "description": "Successful Response", 

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

155 }, 

156 "422": { 

157 "description": "Validation Error", 

158 "content": { 

159 "application/json": { 

160 "schema": { 

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

162 } 

163 } 

164 }, 

165 }, 

166 }, 

167 "summary": "Create Upload File", 

168 "operationId": "create_upload_file_uploadfile__post", 

169 "requestBody": { 

170 "content": { 

171 "multipart/form-data": { 

172 "schema": { 

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

174 } 

175 } 

176 }, 

177 "required": True, 

178 }, 

179 } 

180 }, 

181 }, 

182 "components": { 

183 "schemas": { 

184 "Body_create_upload_file_uploadfile__post": { 

185 "title": "Body_create_upload_file_uploadfile__post", 

186 "required": ["file"], 

187 "type": "object", 

188 "properties": { 

189 "file": {"title": "File", "type": "string", "format": "binary"} 

190 }, 

191 }, 

192 "Body_create_file_files__post": { 

193 "title": "Body_create_file_files__post", 

194 "required": ["file"], 

195 "type": "object", 

196 "properties": { 

197 "file": {"title": "File", "type": "string", "format": "binary"} 

198 }, 

199 }, 

200 "ValidationError": { 

201 "title": "ValidationError", 

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

203 "type": "object", 

204 "properties": { 

205 "loc": { 

206 "title": "Location", 

207 "type": "array", 

208 "items": { 

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

210 }, 

211 }, 

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

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

214 }, 

215 }, 

216 "HTTPValidationError": { 

217 "title": "HTTPValidationError", 

218 "type": "object", 

219 "properties": { 

220 "detail": { 

221 "title": "Detail", 

222 "type": "array", 

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

224 } 

225 }, 

226 }, 

227 } 

228 }, 

229 }