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

41 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_an import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9def test_post_form_no_body(): 1abcde

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

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

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

13 { 

14 "detail": [ 

15 { 

16 "type": "missing", 

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

18 "msg": "Field required", 

19 "input": None, 

20 } 

21 ] 

22 } 

23 ) | IsDict( 

24 # TODO: remove when deprecating Pydantic v1 

25 { 

26 "detail": [ 

27 { 

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

29 "msg": "field required", 

30 "type": "value_error.missing", 

31 } 

32 ] 

33 } 

34 ) 

35 

36 

37def test_post_body_json(): 1abcde

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

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

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

41 { 

42 "detail": [ 

43 { 

44 "type": "missing", 

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

46 "msg": "Field required", 

47 "input": None, 

48 } 

49 ] 

50 } 

51 ) | IsDict( 

52 # TODO: remove when deprecating Pydantic v1 

53 { 

54 "detail": [ 

55 { 

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

57 "msg": "field required", 

58 "type": "value_error.missing", 

59 } 

60 ] 

61 } 

62 ) 

63 

64 

65def test_post_file(tmp_path): 1abcde

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

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

68 

69 client = TestClient(app) 1abcde

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

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

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

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

74 

75 

76def test_post_large_file(tmp_path): 1abcde

77 default_pydantic_max_size = 2**16 1abcde

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

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

80 

81 client = TestClient(app) 1abcde

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

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

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

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

86 

87 

88def test_post_upload_file(tmp_path): 1abcde

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

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

91 

92 client = TestClient(app) 1abcde

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

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

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

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

97 

98 

99def test_openapi_schema(): 1abcde

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

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

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

103 "openapi": "3.1.0", 

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

105 "paths": { 

106 "/files/": { 

107 "post": { 

108 "responses": { 

109 "200": { 

110 "description": "Successful Response", 

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

112 }, 

113 "422": { 

114 "description": "Validation Error", 

115 "content": { 

116 "application/json": { 

117 "schema": { 

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

119 } 

120 } 

121 }, 

122 }, 

123 }, 

124 "summary": "Create File", 

125 "operationId": "create_file_files__post", 

126 "requestBody": { 

127 "content": { 

128 "multipart/form-data": { 

129 "schema": { 

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

131 } 

132 } 

133 }, 

134 "required": True, 

135 }, 

136 } 

137 }, 

138 "/uploadfile/": { 

139 "post": { 

140 "responses": { 

141 "200": { 

142 "description": "Successful Response", 

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

144 }, 

145 "422": { 

146 "description": "Validation Error", 

147 "content": { 

148 "application/json": { 

149 "schema": { 

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

151 } 

152 } 

153 }, 

154 }, 

155 }, 

156 "summary": "Create Upload File", 

157 "operationId": "create_upload_file_uploadfile__post", 

158 "requestBody": { 

159 "content": { 

160 "multipart/form-data": { 

161 "schema": { 

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

163 } 

164 } 

165 }, 

166 "required": True, 

167 }, 

168 } 

169 }, 

170 }, 

171 "components": { 

172 "schemas": { 

173 "Body_create_upload_file_uploadfile__post": { 

174 "title": "Body_create_upload_file_uploadfile__post", 

175 "required": ["file"], 

176 "type": "object", 

177 "properties": { 

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

179 }, 

180 }, 

181 "Body_create_file_files__post": { 

182 "title": "Body_create_file_files__post", 

183 "required": ["file"], 

184 "type": "object", 

185 "properties": { 

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

187 }, 

188 }, 

189 "ValidationError": { 

190 "title": "ValidationError", 

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

192 "type": "object", 

193 "properties": { 

194 "loc": { 

195 "title": "Location", 

196 "type": "array", 

197 "items": { 

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

199 }, 

200 }, 

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

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

203 }, 

204 }, 

205 "HTTPValidationError": { 

206 "title": "HTTPValidationError", 

207 "type": "object", 

208 "properties": { 

209 "detail": { 

210 "title": "Detail", 

211 "type": "array", 

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

213 } 

214 }, 

215 }, 

216 } 

217 }, 

218 }