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

29 statements  

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

1import pytest 1eabcd

2from fastapi.testclient import TestClient 1eabcd

3 

4from ...utils import needs_py39 1eabcd

5 

6 

7@pytest.fixture(name="client") 1eabcd

8def get_client(): 1eabcd

9 from docs_src.request_files.tutorial001_03_an_py39 import app 1abcd

10 

11 client = TestClient(app) 1abcd

12 return client 1abcd

13 

14 

15@needs_py39 1eabcd

16def test_post_file(tmp_path, client: TestClient): 1eabcd

17 path = tmp_path / "test.txt" 1abcd

18 path.write_bytes(b"<file content>") 1abcd

19 

20 with path.open("rb") as file: 1abcd

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

22 assert response.status_code == 200, response.text 1abcd

23 assert response.json() == {"file_size": 14} 1abcd

24 

25 

26@needs_py39 1eabcd

27def test_post_upload_file(tmp_path, client: TestClient): 1eabcd

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

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

30 

31 with path.open("rb") as file: 1abcd

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

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

34 assert response.json() == {"filename": "test.txt"} 1abcd

35 

36 

37@needs_py39 1eabcd

38def test_openapi_schema(client: TestClient): 1eabcd

39 response = client.get("/openapi.json") 1abcd

40 assert response.status_code == 200, response.text 1abcd

41 assert response.json() == { 1abcd

42 "openapi": "3.1.0", 

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

44 "paths": { 

45 "/files/": { 

46 "post": { 

47 "summary": "Create File", 

48 "operationId": "create_file_files__post", 

49 "requestBody": { 

50 "content": { 

51 "multipart/form-data": { 

52 "schema": { 

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

54 } 

55 } 

56 }, 

57 "required": True, 

58 }, 

59 "responses": { 

60 "200": { 

61 "description": "Successful Response", 

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

63 }, 

64 "422": { 

65 "description": "Validation Error", 

66 "content": { 

67 "application/json": { 

68 "schema": { 

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

70 } 

71 } 

72 }, 

73 }, 

74 }, 

75 } 

76 }, 

77 "/uploadfile/": { 

78 "post": { 

79 "summary": "Create Upload File", 

80 "operationId": "create_upload_file_uploadfile__post", 

81 "requestBody": { 

82 "content": { 

83 "multipart/form-data": { 

84 "schema": { 

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

86 } 

87 } 

88 }, 

89 "required": True, 

90 }, 

91 "responses": { 

92 "200": { 

93 "description": "Successful Response", 

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

95 }, 

96 "422": { 

97 "description": "Validation Error", 

98 "content": { 

99 "application/json": { 

100 "schema": { 

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

102 } 

103 } 

104 }, 

105 }, 

106 }, 

107 } 

108 }, 

109 }, 

110 "components": { 

111 "schemas": { 

112 "Body_create_file_files__post": { 

113 "title": "Body_create_file_files__post", 

114 "required": ["file"], 

115 "type": "object", 

116 "properties": { 

117 "file": { 

118 "title": "File", 

119 "type": "string", 

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

121 "format": "binary", 

122 } 

123 }, 

124 }, 

125 "Body_create_upload_file_uploadfile__post": { 

126 "title": "Body_create_upload_file_uploadfile__post", 

127 "required": ["file"], 

128 "type": "object", 

129 "properties": { 

130 "file": { 

131 "title": "File", 

132 "type": "string", 

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

134 "format": "binary", 

135 } 

136 }, 

137 }, 

138 "HTTPValidationError": { 

139 "title": "HTTPValidationError", 

140 "type": "object", 

141 "properties": { 

142 "detail": { 

143 "title": "Detail", 

144 "type": "array", 

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

146 } 

147 }, 

148 }, 

149 "ValidationError": { 

150 "title": "ValidationError", 

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

152 "type": "object", 

153 "properties": { 

154 "loc": { 

155 "title": "Location", 

156 "type": "array", 

157 "items": { 

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

159 }, 

160 }, 

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

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

163 }, 

164 }, 

165 } 

166 }, 

167 }