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

41 statements  

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

1from pathlib import Path 1deabc

2 

3import pytest 1deabc

4from dirty_equals import IsDict 1deabc

5from fastapi.testclient import TestClient 1deabc

6 

7from ...utils import needs_py310 1deabc

8 

9 

10@pytest.fixture(name="client") 1deabc

11def get_client(): 1deabc

12 from docs_src.request_files.tutorial001_02_py310 import app 1abc

13 

14 client = TestClient(app) 1abc

15 return client 1abc

16 

17 

18@needs_py310 1deabc

19def test_post_form_no_body(client: TestClient): 1deabc

20 response = client.post("/files/") 1abc

21 assert response.status_code == 200, response.text 1abc

22 assert response.json() == {"message": "No file sent"} 1abc

23 

24 

25@needs_py310 1deabc

26def test_post_uploadfile_no_body(client: TestClient): 1deabc

27 response = client.post("/uploadfile/") 1abc

28 assert response.status_code == 200, response.text 1abc

29 assert response.json() == {"message": "No upload file sent"} 1abc

30 

31 

32@needs_py310 1deabc

33def test_post_file(tmp_path: Path, client: TestClient): 1deabc

34 path = tmp_path / "test.txt" 1abc

35 path.write_bytes(b"<file content>") 1abc

36 

37 with path.open("rb") as file: 1abc

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

39 assert response.status_code == 200, response.text 1abc

40 assert response.json() == {"file_size": 14} 1abc

41 

42 

43@needs_py310 1deabc

44def test_post_upload_file(tmp_path: Path, client: TestClient): 1deabc

45 path = tmp_path / "test.txt" 1abc

46 path.write_bytes(b"<file content>") 1abc

47 

48 with path.open("rb") as file: 1abc

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

50 assert response.status_code == 200, response.text 1abc

51 assert response.json() == {"filename": "test.txt"} 1abc

52 

53 

54@needs_py310 1deabc

55def test_openapi_schema(client: TestClient): 1deabc

56 response = client.get("/openapi.json") 1abc

57 assert response.status_code == 200, response.text 1abc

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

59 "openapi": "3.1.0", 

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

61 "paths": { 

62 "/files/": { 

63 "post": { 

64 "summary": "Create File", 

65 "operationId": "create_file_files__post", 

66 "requestBody": { 

67 "content": { 

68 "multipart/form-data": { 

69 "schema": IsDict( 

70 { 

71 "allOf": [ 

72 { 

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

74 } 

75 ], 

76 "title": "Body", 

77 } 

78 ) 

79 | IsDict( 

80 # TODO: remove when deprecating Pydantic v1 

81 { 

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

83 } 

84 ) 

85 } 

86 } 

87 }, 

88 "responses": { 

89 "200": { 

90 "description": "Successful Response", 

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

92 }, 

93 "422": { 

94 "description": "Validation Error", 

95 "content": { 

96 "application/json": { 

97 "schema": { 

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

99 } 

100 } 

101 }, 

102 }, 

103 }, 

104 } 

105 }, 

106 "/uploadfile/": { 

107 "post": { 

108 "summary": "Create Upload File", 

109 "operationId": "create_upload_file_uploadfile__post", 

110 "requestBody": { 

111 "content": { 

112 "multipart/form-data": { 

113 "schema": IsDict( 

114 { 

115 "allOf": [ 

116 { 

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

118 } 

119 ], 

120 "title": "Body", 

121 } 

122 ) 

123 | IsDict( 

124 # TODO: remove when deprecating Pydantic v1 

125 { 

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

127 } 

128 ) 

129 } 

130 } 

131 }, 

132 "responses": { 

133 "200": { 

134 "description": "Successful Response", 

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

136 }, 

137 "422": { 

138 "description": "Validation Error", 

139 "content": { 

140 "application/json": { 

141 "schema": { 

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

143 } 

144 } 

145 }, 

146 }, 

147 }, 

148 } 

149 }, 

150 }, 

151 "components": { 

152 "schemas": { 

153 "Body_create_file_files__post": { 

154 "title": "Body_create_file_files__post", 

155 "type": "object", 

156 "properties": { 

157 "file": IsDict( 

158 { 

159 "title": "File", 

160 "anyOf": [ 

161 {"type": "string", "format": "binary"}, 

162 {"type": "null"}, 

163 ], 

164 } 

165 ) 

166 | IsDict( 

167 # TODO: remove when deprecating Pydantic v1 

168 {"title": "File", "type": "string", "format": "binary"} 

169 ) 

170 }, 

171 }, 

172 "Body_create_upload_file_uploadfile__post": { 

173 "title": "Body_create_upload_file_uploadfile__post", 

174 "type": "object", 

175 "properties": { 

176 "file": IsDict( 

177 { 

178 "title": "File", 

179 "anyOf": [ 

180 {"type": "string", "format": "binary"}, 

181 {"type": "null"}, 

182 ], 

183 } 

184 ) 

185 | IsDict( 

186 # TODO: remove when deprecating Pydantic v1 

187 {"title": "File", "type": "string", "format": "binary"} 

188 ) 

189 }, 

190 }, 

191 "HTTPValidationError": { 

192 "title": "HTTPValidationError", 

193 "type": "object", 

194 "properties": { 

195 "detail": { 

196 "title": "Detail", 

197 "type": "array", 

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

199 } 

200 }, 

201 }, 

202 "ValidationError": { 

203 "title": "ValidationError", 

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

205 "type": "object", 

206 "properties": { 

207 "loc": { 

208 "title": "Location", 

209 "type": "array", 

210 "items": { 

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

212 }, 

213 }, 

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

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

216 }, 

217 }, 

218 } 

219 }, 

220 }