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

49 statements  

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

1import pytest 1eabcd

2from dirty_equals import IsDict 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

8@pytest.fixture(name="client") 1eabcd

9def get_client(): 1eabcd

10 from docs_src.request_files.tutorial001_an_py39 import app 1abcd

11 

12 client = TestClient(app) 1abcd

13 return client 1abcd

14 

15 

16@needs_py39 1eabcd

17def test_post_form_no_body(client: TestClient): 1eabcd

18 response = client.post("/files/") 1abcd

19 assert response.status_code == 422, response.text 1abcd

20 assert response.json() == IsDict( 1abcd

21 { 

22 "detail": [ 

23 { 

24 "type": "missing", 

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

26 "msg": "Field required", 

27 "input": None, 

28 } 

29 ] 

30 } 

31 ) | IsDict( 

32 # TODO: remove when deprecating Pydantic v1 

33 { 

34 "detail": [ 

35 { 

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

37 "msg": "field required", 

38 "type": "value_error.missing", 

39 } 

40 ] 

41 } 

42 ) 

43 

44 

45@needs_py39 1eabcd

46def test_post_body_json(client: TestClient): 1eabcd

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

48 assert response.status_code == 422, response.text 1abcd

49 assert response.json() == IsDict( 1abcd

50 { 

51 "detail": [ 

52 { 

53 "type": "missing", 

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

55 "msg": "Field required", 

56 "input": None, 

57 } 

58 ] 

59 } 

60 ) | IsDict( 

61 # TODO: remove when deprecating Pydantic v1 

62 { 

63 "detail": [ 

64 { 

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

66 "msg": "field required", 

67 "type": "value_error.missing", 

68 } 

69 ] 

70 } 

71 ) 

72 

73 

74@needs_py39 1eabcd

75def test_post_file(tmp_path, client: TestClient): 1eabcd

76 path = tmp_path / "test.txt" 1abcd

77 path.write_bytes(b"<file content>") 1abcd

78 

79 with path.open("rb") as file: 1abcd

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

81 assert response.status_code == 200, response.text 1abcd

82 assert response.json() == {"file_size": 14} 1abcd

83 

84 

85@needs_py39 1eabcd

86def test_post_large_file(tmp_path, client: TestClient): 1eabcd

87 default_pydantic_max_size = 2**16 1abcd

88 path = tmp_path / "test.txt" 1abcd

89 path.write_bytes(b"x" * (default_pydantic_max_size + 1)) 1abcd

90 

91 with path.open("rb") as file: 1abcd

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

93 assert response.status_code == 200, response.text 1abcd

94 assert response.json() == {"file_size": default_pydantic_max_size + 1} 1abcd

95 

96 

97@needs_py39 1eabcd

98def test_post_upload_file(tmp_path, client: TestClient): 1eabcd

99 path = tmp_path / "test.txt" 1abcd

100 path.write_bytes(b"<file content>") 1abcd

101 

102 with path.open("rb") as file: 1abcd

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

104 assert response.status_code == 200, response.text 1abcd

105 assert response.json() == {"filename": "test.txt"} 1abcd

106 

107 

108@needs_py39 1eabcd

109def test_openapi_schema(client: TestClient): 1eabcd

110 response = client.get("/openapi.json") 1abcd

111 assert response.status_code == 200, response.text 1abcd

112 assert response.json() == { 1abcd

113 "openapi": "3.1.0", 

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

115 "paths": { 

116 "/files/": { 

117 "post": { 

118 "responses": { 

119 "200": { 

120 "description": "Successful Response", 

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

122 }, 

123 "422": { 

124 "description": "Validation Error", 

125 "content": { 

126 "application/json": { 

127 "schema": { 

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

129 } 

130 } 

131 }, 

132 }, 

133 }, 

134 "summary": "Create File", 

135 "operationId": "create_file_files__post", 

136 "requestBody": { 

137 "content": { 

138 "multipart/form-data": { 

139 "schema": { 

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

141 } 

142 } 

143 }, 

144 "required": True, 

145 }, 

146 } 

147 }, 

148 "/uploadfile/": { 

149 "post": { 

150 "responses": { 

151 "200": { 

152 "description": "Successful Response", 

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

154 }, 

155 "422": { 

156 "description": "Validation Error", 

157 "content": { 

158 "application/json": { 

159 "schema": { 

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

161 } 

162 } 

163 }, 

164 }, 

165 }, 

166 "summary": "Create Upload File", 

167 "operationId": "create_upload_file_uploadfile__post", 

168 "requestBody": { 

169 "content": { 

170 "multipart/form-data": { 

171 "schema": { 

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

173 } 

174 } 

175 }, 

176 "required": True, 

177 }, 

178 } 

179 }, 

180 }, 

181 "components": { 

182 "schemas": { 

183 "Body_create_upload_file_uploadfile__post": { 

184 "title": "Body_create_upload_file_uploadfile__post", 

185 "required": ["file"], 

186 "type": "object", 

187 "properties": { 

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

189 }, 

190 }, 

191 "Body_create_file_files__post": { 

192 "title": "Body_create_file_files__post", 

193 "required": ["file"], 

194 "type": "object", 

195 "properties": { 

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

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 "HTTPValidationError": { 

216 "title": "HTTPValidationError", 

217 "type": "object", 

218 "properties": { 

219 "detail": { 

220 "title": "Detail", 

221 "type": "array", 

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

223 } 

224 }, 

225 }, 

226 } 

227 }, 

228 }