Coverage for tests / test_tutorial / test_json_base64_bytes / test_tutorial001.py: 100%

26 statements  

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

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7from tests.utils import needs_py310 1abcd

8 

9 

10@pytest.fixture( 1abcd

11 name="client", 

12 params=[pytest.param("tutorial001_py310", marks=needs_py310)], 

13) 

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

15 mod = importlib.import_module(f"docs_src.json_base64_bytes.{request.param}") 1efghijklmnopqr

16 

17 client = TestClient(mod.app) 1efghijklmnopqr

18 return client 1efghijklmnopqr

19 

20 

21def test_post_data(client: TestClient): 1abcd

22 response = client.post( 1stuv

23 "/data", 

24 json={ 

25 "description": "A file", 

26 "data": "SGVsbG8sIFdvcmxkIQ==", 

27 }, 

28 ) 

29 assert response.status_code == 200, response.text 1stuv

30 assert response.json() == {"description": "A file", "content": "Hello, World!"} 1stuv

31 

32 

33def test_get_data(client: TestClient): 1abcd

34 response = client.get("/data") 1wxyz

35 assert response.status_code == 200, response.text 1wxyz

36 assert response.json() == {"description": "A plumbus", "data": "aGVsbG8="} 1wxyz

37 

38 

39def test_post_data_in_out(client: TestClient): 1abcd

40 response = client.post( 1ABCD

41 "/data-in-out", 

42 json={ 

43 "description": "A plumbus", 

44 "data": "SGVsbG8sIFdvcmxkIQ==", 

45 }, 

46 ) 

47 assert response.status_code == 200, response.text 1ABCD

48 assert response.json() == { 1ABCD

49 "description": "A plumbus", 

50 "data": "SGVsbG8sIFdvcmxkIQ==", 

51 } 

52 

53 

54def test_openapi_schema(client: TestClient): 1abcd

55 response = client.get("/openapi.json") 1EFGH

56 assert response.status_code == 200, response.text 1EFGH

57 assert response.json() == snapshot( 1EFGH

58 { 

59 "openapi": "3.1.0", 

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

61 "paths": { 

62 "/data": { 

63 "get": { 

64 "summary": "Get Data", 

65 "operationId": "get_data_data_get", 

66 "responses": { 

67 "200": { 

68 "description": "Successful Response", 

69 "content": { 

70 "application/json": { 

71 "schema": { 

72 "$ref": "#/components/schemas/DataOutput" 

73 } 

74 } 

75 }, 

76 } 

77 }, 

78 }, 

79 "post": { 

80 "summary": "Post Data", 

81 "operationId": "post_data_data_post", 

82 "requestBody": { 

83 "content": { 

84 "application/json": { 

85 "schema": {"$ref": "#/components/schemas/DataInput"} 

86 } 

87 }, 

88 "required": True, 

89 }, 

90 "responses": { 

91 "200": { 

92 "description": "Successful Response", 

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

94 }, 

95 "422": { 

96 "description": "Validation Error", 

97 "content": { 

98 "application/json": { 

99 "schema": { 

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

101 } 

102 } 

103 }, 

104 }, 

105 }, 

106 }, 

107 }, 

108 "/data-in-out": { 

109 "post": { 

110 "summary": "Post Data In Out", 

111 "operationId": "post_data_in_out_data_in_out_post", 

112 "requestBody": { 

113 "content": { 

114 "application/json": { 

115 "schema": { 

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

117 } 

118 } 

119 }, 

120 "required": True, 

121 }, 

122 "responses": { 

123 "200": { 

124 "description": "Successful Response", 

125 "content": { 

126 "application/json": { 

127 "schema": { 

128 "$ref": "#/components/schemas/DataInputOutput" 

129 } 

130 } 

131 }, 

132 }, 

133 "422": { 

134 "description": "Validation Error", 

135 "content": { 

136 "application/json": { 

137 "schema": { 

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

139 } 

140 } 

141 }, 

142 }, 

143 }, 

144 } 

145 }, 

146 }, 

147 "components": { 

148 "schemas": { 

149 "DataInput": { 

150 "properties": { 

151 "description": {"type": "string", "title": "Description"}, 

152 "data": { 

153 "type": "string", 

154 "contentEncoding": "base64", 

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

156 "title": "Data", 

157 }, 

158 }, 

159 "type": "object", 

160 "required": ["description", "data"], 

161 "title": "DataInput", 

162 }, 

163 "DataInputOutput": { 

164 "properties": { 

165 "description": {"type": "string", "title": "Description"}, 

166 "data": { 

167 "type": "string", 

168 "contentEncoding": "base64", 

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

170 "title": "Data", 

171 }, 

172 }, 

173 "type": "object", 

174 "required": ["description", "data"], 

175 "title": "DataInputOutput", 

176 }, 

177 "DataOutput": { 

178 "properties": { 

179 "description": {"type": "string", "title": "Description"}, 

180 "data": { 

181 "type": "string", 

182 "contentEncoding": "base64", 

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

184 "title": "Data", 

185 }, 

186 }, 

187 "type": "object", 

188 "required": ["description", "data"], 

189 "title": "DataOutput", 

190 }, 

191 "HTTPValidationError": { 

192 "properties": { 

193 "detail": { 

194 "items": { 

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

196 }, 

197 "type": "array", 

198 "title": "Detail", 

199 } 

200 }, 

201 "type": "object", 

202 "title": "HTTPValidationError", 

203 }, 

204 "ValidationError": { 

205 "properties": { 

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

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

208 "loc": { 

209 "items": { 

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

211 }, 

212 "type": "array", 

213 "title": "Location", 

214 }, 

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

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

217 }, 

218 "type": "object", 

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

220 "title": "ValidationError", 

221 }, 

222 } 

223 }, 

224 } 

225 )