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

37 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2from pathlib import Path 1abcd

3 

4import pytest 1abcd

5from fastapi.testclient import TestClient 1abcd

6from inline_snapshot import snapshot 1abcd

7 

8from ...utils import needs_py310 1abcd

9 

10 

11@pytest.fixture( 1abcd

12 name="client", 

13 params=[ 

14 pytest.param("tutorial001_02_py310", marks=needs_py310), 

15 pytest.param("tutorial001_02_an_py310", marks=needs_py310), 

16 ], 

17) 

18def get_client(request: pytest.FixtureRequest): 1abcd

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

20 

21 client = TestClient(mod.app) 1uvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

22 return client 1uvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

23 

24 

25def test_post_form_no_body(client: TestClient): 1abcd

26 response = client.post("/files/") 10123456

27 assert response.status_code == 200, response.text 10123456

28 assert response.json() == {"message": "No file sent"} 10123456

29 

30 

31def test_post_uploadfile_no_body(client: TestClient): 1abcd

32 response = client.post("/uploadfile/") 1789!#$%'

33 assert response.status_code == 200, response.text 1789!#$%'

34 assert response.json() == {"message": "No upload file sent"} 1789!#$%'

35 

36 

37def test_post_file(tmp_path: Path, client: TestClient): 1abcd

38 path = tmp_path / "test.txt" 1efghijkl

39 path.write_bytes(b"<file content>") 1efghijkl

40 

41 with path.open("rb") as file: 1efghijkl

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

43 assert response.status_code == 200, response.text 1efghijkl

44 assert response.json() == {"file_size": 14} 1efghijkl

45 

46 

47def test_post_upload_file(tmp_path: Path, client: TestClient): 1abcd

48 path = tmp_path / "test.txt" 1mnopqrst

49 path.write_bytes(b"<file content>") 1mnopqrst

50 

51 with path.open("rb") as file: 1mnopqrst

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

53 assert response.status_code == 200, response.text 1mnopqrst

54 assert response.json() == {"filename": "test.txt"} 1mnopqrst

55 

56 

57def test_openapi_schema(client: TestClient): 1abcd

58 response = client.get("/openapi.json") 1()*+,-./

59 assert response.status_code == 200, response.text 1()*+,-./

60 assert response.json() == snapshot( 1()*+,-./

61 { 

62 "openapi": "3.1.0", 

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

64 "paths": { 

65 "/files/": { 

66 "post": { 

67 "summary": "Create File", 

68 "operationId": "create_file_files__post", 

69 "requestBody": { 

70 "content": { 

71 "multipart/form-data": { 

72 "schema": { 

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

74 } 

75 } 

76 } 

77 }, 

78 "responses": { 

79 "200": { 

80 "description": "Successful Response", 

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

82 }, 

83 "422": { 

84 "description": "Validation Error", 

85 "content": { 

86 "application/json": { 

87 "schema": { 

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

89 } 

90 } 

91 }, 

92 }, 

93 }, 

94 } 

95 }, 

96 "/uploadfile/": { 

97 "post": { 

98 "summary": "Create Upload File", 

99 "operationId": "create_upload_file_uploadfile__post", 

100 "requestBody": { 

101 "content": { 

102 "multipart/form-data": { 

103 "schema": { 

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

105 } 

106 } 

107 } 

108 }, 

109 "responses": { 

110 "200": { 

111 "description": "Successful Response", 

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

113 }, 

114 "422": { 

115 "description": "Validation Error", 

116 "content": { 

117 "application/json": { 

118 "schema": { 

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

120 } 

121 } 

122 }, 

123 }, 

124 }, 

125 } 

126 }, 

127 }, 

128 "components": { 

129 "schemas": { 

130 "Body_create_file_files__post": { 

131 "title": "Body_create_file_files__post", 

132 "type": "object", 

133 "properties": { 

134 "file": { 

135 "title": "File", 

136 "anyOf": [ 

137 { 

138 "type": "string", 

139 "contentMediaType": "application/octet-stream", 

140 }, 

141 {"type": "null"}, 

142 ], 

143 } 

144 }, 

145 }, 

146 "Body_create_upload_file_uploadfile__post": { 

147 "title": "Body_create_upload_file_uploadfile__post", 

148 "type": "object", 

149 "properties": { 

150 "file": { 

151 "title": "File", 

152 "anyOf": [ 

153 { 

154 "type": "string", 

155 "contentMediaType": "application/octet-stream", 

156 }, 

157 {"type": "null"}, 

158 ], 

159 } 

160 }, 

161 }, 

162 "HTTPValidationError": { 

163 "title": "HTTPValidationError", 

164 "type": "object", 

165 "properties": { 

166 "detail": { 

167 "title": "Detail", 

168 "type": "array", 

169 "items": { 

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

171 }, 

172 } 

173 }, 

174 }, 

175 "ValidationError": { 

176 "title": "ValidationError", 

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

178 "type": "object", 

179 "properties": { 

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

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

182 "loc": { 

183 "title": "Location", 

184 "type": "array", 

185 "items": { 

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

187 }, 

188 }, 

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

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

191 }, 

192 }, 

193 } 

194 }, 

195 } 

196 )