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

23 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.tutorial001_03_an import app 1abcde

4 

5client = TestClient(app) 1abcde

6 

7 

8def test_post_file(tmp_path): 1abcde

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

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

11 

12 client = TestClient(app) 1abcde

13 with path.open("rb") as file: 1abcde

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

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

16 assert response.json() == {"file_size": 14} 1abcde

17 

18 

19def test_post_upload_file(tmp_path): 1abcde

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

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

22 

23 client = TestClient(app) 1abcde

24 with path.open("rb") as file: 1abcde

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

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

27 assert response.json() == {"filename": "test.txt"} 1abcde

28 

29 

30def test_openapi_schema(): 1abcde

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

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

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

34 "openapi": "3.1.0", 

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

36 "paths": { 

37 "/files/": { 

38 "post": { 

39 "summary": "Create File", 

40 "operationId": "create_file_files__post", 

41 "requestBody": { 

42 "content": { 

43 "multipart/form-data": { 

44 "schema": { 

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

46 } 

47 } 

48 }, 

49 "required": True, 

50 }, 

51 "responses": { 

52 "200": { 

53 "description": "Successful Response", 

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

55 }, 

56 "422": { 

57 "description": "Validation Error", 

58 "content": { 

59 "application/json": { 

60 "schema": { 

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

62 } 

63 } 

64 }, 

65 }, 

66 }, 

67 } 

68 }, 

69 "/uploadfile/": { 

70 "post": { 

71 "summary": "Create Upload File", 

72 "operationId": "create_upload_file_uploadfile__post", 

73 "requestBody": { 

74 "content": { 

75 "multipart/form-data": { 

76 "schema": { 

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

78 } 

79 } 

80 }, 

81 "required": True, 

82 }, 

83 "responses": { 

84 "200": { 

85 "description": "Successful Response", 

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

87 }, 

88 "422": { 

89 "description": "Validation Error", 

90 "content": { 

91 "application/json": { 

92 "schema": { 

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

94 } 

95 } 

96 }, 

97 }, 

98 }, 

99 } 

100 }, 

101 }, 

102 "components": { 

103 "schemas": { 

104 "Body_create_file_files__post": { 

105 "title": "Body_create_file_files__post", 

106 "required": ["file"], 

107 "type": "object", 

108 "properties": { 

109 "file": { 

110 "title": "File", 

111 "type": "string", 

112 "description": "A file read as bytes", 

113 "format": "binary", 

114 } 

115 }, 

116 }, 

117 "Body_create_upload_file_uploadfile__post": { 

118 "title": "Body_create_upload_file_uploadfile__post", 

119 "required": ["file"], 

120 "type": "object", 

121 "properties": { 

122 "file": { 

123 "title": "File", 

124 "type": "string", 

125 "description": "A file read as UploadFile", 

126 "format": "binary", 

127 } 

128 }, 

129 }, 

130 "HTTPValidationError": { 

131 "title": "HTTPValidationError", 

132 "type": "object", 

133 "properties": { 

134 "detail": { 

135 "title": "Detail", 

136 "type": "array", 

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

138 } 

139 }, 

140 }, 

141 "ValidationError": { 

142 "title": "ValidationError", 

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

144 "type": "object", 

145 "properties": { 

146 "loc": { 

147 "title": "Location", 

148 "type": "array", 

149 "items": { 

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

151 }, 

152 }, 

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

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

155 }, 

156 }, 

157 } 

158 }, 

159 }