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

32 statements  

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

1from fastapi.testclient import TestClient 1abcde

2 

3from docs_src.request_files.tutorial003 import app 1abcde

4 

5client = TestClient(app) 1abcde

6 

7 

8def test_post_files(tmp_path): 1abcde

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

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

11 path2 = tmp_path / "test2.txt" 1abcde

12 path2.write_bytes(b"<file content2>") 1abcde

13 

14 client = TestClient(app) 1abcde

15 with path.open("rb") as file, path2.open("rb") as file2: 1abcde

16 response = client.post( 1abcde

17 "/files/", 

18 files=( 

19 ("files", ("test.txt", file)), 

20 ("files", ("test2.txt", file2)), 

21 ), 

22 ) 

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

24 assert response.json() == {"file_sizes": [14, 15]} 1abcde

25 

26 

27def test_post_upload_file(tmp_path): 1abcde

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

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

30 path2 = tmp_path / "test2.txt" 1abcde

31 path2.write_bytes(b"<file content2>") 1abcde

32 

33 client = TestClient(app) 1abcde

34 with path.open("rb") as file, path2.open("rb") as file2: 1abcde

35 response = client.post( 1abcde

36 "/uploadfiles/", 

37 files=( 

38 ("files", ("test.txt", file)), 

39 ("files", ("test2.txt", file2)), 

40 ), 

41 ) 

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

43 assert response.json() == {"filenames": ["test.txt", "test2.txt"]} 1abcde

44 

45 

46def test_get_root(): 1abcde

47 client = TestClient(app) 1abcde

48 response = client.get("/") 1abcde

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

50 assert b"<form" in response.content 1abcde

51 

52 

53def test_openapi_schema(): 1abcde

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

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

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

57 "openapi": "3.1.0", 

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

59 "paths": { 

60 "/files/": { 

61 "post": { 

62 "summary": "Create Files", 

63 "operationId": "create_files_files__post", 

64 "requestBody": { 

65 "content": { 

66 "multipart/form-data": { 

67 "schema": { 

68 "$ref": "#/components/schemas/Body_create_files_files__post" 

69 } 

70 } 

71 }, 

72 "required": True, 

73 }, 

74 "responses": { 

75 "200": { 

76 "description": "Successful Response", 

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

78 }, 

79 "422": { 

80 "description": "Validation Error", 

81 "content": { 

82 "application/json": { 

83 "schema": { 

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

85 } 

86 } 

87 }, 

88 }, 

89 }, 

90 } 

91 }, 

92 "/uploadfiles/": { 

93 "post": { 

94 "summary": "Create Upload Files", 

95 "operationId": "create_upload_files_uploadfiles__post", 

96 "requestBody": { 

97 "content": { 

98 "multipart/form-data": { 

99 "schema": { 

100 "$ref": "#/components/schemas/Body_create_upload_files_uploadfiles__post" 

101 } 

102 } 

103 }, 

104 "required": True, 

105 }, 

106 "responses": { 

107 "200": { 

108 "description": "Successful Response", 

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

110 }, 

111 "422": { 

112 "description": "Validation Error", 

113 "content": { 

114 "application/json": { 

115 "schema": { 

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

117 } 

118 } 

119 }, 

120 }, 

121 }, 

122 } 

123 }, 

124 "/": { 

125 "get": { 

126 "summary": "Main", 

127 "operationId": "main__get", 

128 "responses": { 

129 "200": { 

130 "description": "Successful Response", 

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

132 } 

133 }, 

134 } 

135 }, 

136 }, 

137 "components": { 

138 "schemas": { 

139 "Body_create_files_files__post": { 

140 "title": "Body_create_files_files__post", 

141 "required": ["files"], 

142 "type": "object", 

143 "properties": { 

144 "files": { 

145 "title": "Files", 

146 "type": "array", 

147 "items": {"type": "string", "format": "binary"}, 

148 "description": "Multiple files as bytes", 

149 } 

150 }, 

151 }, 

152 "Body_create_upload_files_uploadfiles__post": { 

153 "title": "Body_create_upload_files_uploadfiles__post", 

154 "required": ["files"], 

155 "type": "object", 

156 "properties": { 

157 "files": { 

158 "title": "Files", 

159 "type": "array", 

160 "items": {"type": "string", "format": "binary"}, 

161 "description": "Multiple files as UploadFile", 

162 } 

163 }, 

164 }, 

165 "HTTPValidationError": { 

166 "title": "HTTPValidationError", 

167 "type": "object", 

168 "properties": { 

169 "detail": { 

170 "title": "Detail", 

171 "type": "array", 

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

173 } 

174 }, 

175 }, 

176 "ValidationError": { 

177 "title": "ValidationError", 

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

179 "type": "object", 

180 "properties": { 

181 "loc": { 

182 "title": "Location", 

183 "type": "array", 

184 "items": { 

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

186 }, 

187 }, 

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

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

190 }, 

191 }, 

192 } 

193 }, 

194 }