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-02-12 18:15 +0000

1import importlib 1abdc

2from pathlib import Path 1abdc

3 

4import pytest 1abdc

5from fastapi.testclient import TestClient 1abdc

6from inline_snapshot import snapshot 1abdc

7 

8from ...utils import needs_py310 1abdc

9 

10 

11@pytest.fixture( 1abdc

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): 1abdc

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

20 

21 client = TestClient(mod.app) 1abc

22 return client 1abc

23 

24 

25def test_post_form_no_body(client: TestClient): 1abdc

26 response = client.post("/files/") 1klm

27 assert response.status_code == 200, response.text 1klm

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

29 

30 

31def test_post_uploadfile_no_body(client: TestClient): 1abdc

32 response = client.post("/uploadfile/") 1nop

33 assert response.status_code == 200, response.text 1nop

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

35 

36 

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

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

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

40 

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

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

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

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

45 

46 

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

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

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

50 

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

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

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

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

55 

56 

57def test_openapi_schema(client: TestClient): 1abdc

58 response = client.get("/openapi.json") 1qrs

59 assert response.status_code == 200, response.text 1qrs

60 assert response.json() == snapshot( 1qrs

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 {"type": "string", "format": "binary"}, 

138 {"type": "null"}, 

139 ], 

140 } 

141 }, 

142 }, 

143 "Body_create_upload_file_uploadfile__post": { 

144 "title": "Body_create_upload_file_uploadfile__post", 

145 "type": "object", 

146 "properties": { 

147 "file": { 

148 "title": "File", 

149 "anyOf": [ 

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

151 {"type": "null"}, 

152 ], 

153 } 

154 }, 

155 }, 

156 "HTTPValidationError": { 

157 "title": "HTTPValidationError", 

158 "type": "object", 

159 "properties": { 

160 "detail": { 

161 "title": "Detail", 

162 "type": "array", 

163 "items": { 

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

165 }, 

166 } 

167 }, 

168 }, 

169 "ValidationError": { 

170 "title": "ValidationError", 

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

172 "type": "object", 

173 "properties": { 

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

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

176 "loc": { 

177 "title": "Location", 

178 "type": "array", 

179 "items": { 

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

181 }, 

182 }, 

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

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

185 }, 

186 }, 

187 } 

188 }, 

189 } 

190 )