Coverage for tests/test_tutorial/test_path_operation_configurations/test_tutorial005.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from fastapi.testclient import TestClient 1abcdef

5 

6from ...utils import needs_py39, needs_py310, needs_pydanticv1, needs_pydanticv2 1abcdef

7 

8 

9@pytest.fixture( 1abcdef

10 name="client", 

11 params=[ 

12 "tutorial005", 

13 pytest.param("tutorial005_py39", marks=needs_py39), 

14 pytest.param("tutorial005_py310", marks=needs_py310), 

15 ], 

16) 

17def get_client(request: pytest.FixtureRequest): 1abcdef

18 mod = importlib.import_module( 1abcdef

19 f"docs_src.path_operation_configuration.{request.param}" 

20 ) 

21 

22 client = TestClient(mod.app) 1abcdef

23 return client 1abcdef

24 

25 

26def test_query_params_str_validations(client: TestClient): 1abcdef

27 response = client.post("/items/", json={"name": "Foo", "price": 42}) 1ghijkl

28 assert response.status_code == 200, response.text 1ghijkl

29 assert response.json() == { 1ghijkl

30 "name": "Foo", 

31 "price": 42, 

32 "description": None, 

33 "tax": None, 

34 "tags": [], 

35 } 

36 

37 

38@needs_pydanticv2 1abcdef

39def test_openapi_schema(client: TestClient): 1abcdef

40 response = client.get("/openapi.json") 1mnopqr

41 assert response.status_code == 200, response.text 1mnopqr

42 assert response.json() == { 1mnopqr

43 "openapi": "3.1.0", 

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

45 "paths": { 

46 "/items/": { 

47 "post": { 

48 "responses": { 

49 "200": { 

50 "description": "The created item", 

51 "content": { 

52 "application/json": { 

53 "schema": {"$ref": "#/components/schemas/Item"} 

54 } 

55 }, 

56 }, 

57 "422": { 

58 "description": "Validation Error", 

59 "content": { 

60 "application/json": { 

61 "schema": { 

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

63 } 

64 } 

65 }, 

66 }, 

67 }, 

68 "summary": "Create an item", 

69 "description": "Create an item with all the information:\n\n- **name**: each item must have a name\n- **description**: a long description\n- **price**: required\n- **tax**: if the item doesn't have tax, you can omit this\n- **tags**: a set of unique tag strings for this item", 

70 "operationId": "create_item_items__post", 

71 "requestBody": { 

72 "content": { 

73 "application/json": { 

74 "schema": {"$ref": "#/components/schemas/Item"} 

75 } 

76 }, 

77 "required": True, 

78 }, 

79 } 

80 } 

81 }, 

82 "components": { 

83 "schemas": { 

84 "Item": { 

85 "title": "Item", 

86 "required": ["name", "price"], 

87 "type": "object", 

88 "properties": { 

89 "name": {"title": "Name", "type": "string"}, 

90 "description": { 

91 "title": "Description", 

92 "anyOf": [{"type": "string"}, {"type": "null"}], 

93 }, 

94 "price": {"title": "Price", "type": "number"}, 

95 "tax": { 

96 "title": "Tax", 

97 "anyOf": [{"type": "number"}, {"type": "null"}], 

98 }, 

99 "tags": { 

100 "title": "Tags", 

101 "uniqueItems": True, 

102 "type": "array", 

103 "items": {"type": "string"}, 

104 "default": [], 

105 }, 

106 }, 

107 }, 

108 "ValidationError": { 

109 "title": "ValidationError", 

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

111 "type": "object", 

112 "properties": { 

113 "loc": { 

114 "title": "Location", 

115 "type": "array", 

116 "items": { 

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

118 }, 

119 }, 

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

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

122 }, 

123 }, 

124 "HTTPValidationError": { 

125 "title": "HTTPValidationError", 

126 "type": "object", 

127 "properties": { 

128 "detail": { 

129 "title": "Detail", 

130 "type": "array", 

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

132 } 

133 }, 

134 }, 

135 } 

136 }, 

137 } 

138 

139 

140# TODO: remove when deprecating Pydantic v1 

141@needs_pydanticv1 1abcdef

142def test_openapi_schema_pv1(client: TestClient): 1abcdef

143 response = client.get("/openapi.json") 1stuvwx

144 assert response.status_code == 200, response.text 1stuvwx

145 assert response.json() == { 1stuvwx

146 "openapi": "3.1.0", 

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

148 "paths": { 

149 "/items/": { 

150 "post": { 

151 "responses": { 

152 "200": { 

153 "description": "The created item", 

154 "content": { 

155 "application/json": { 

156 "schema": {"$ref": "#/components/schemas/Item"} 

157 } 

158 }, 

159 }, 

160 "422": { 

161 "description": "Validation Error", 

162 "content": { 

163 "application/json": { 

164 "schema": { 

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

166 } 

167 } 

168 }, 

169 }, 

170 }, 

171 "summary": "Create an item", 

172 "description": "Create an item with all the information:\n\n- **name**: each item must have a name\n- **description**: a long description\n- **price**: required\n- **tax**: if the item doesn't have tax, you can omit this\n- **tags**: a set of unique tag strings for this item", 

173 "operationId": "create_item_items__post", 

174 "requestBody": { 

175 "content": { 

176 "application/json": { 

177 "schema": {"$ref": "#/components/schemas/Item"} 

178 } 

179 }, 

180 "required": True, 

181 }, 

182 } 

183 } 

184 }, 

185 "components": { 

186 "schemas": { 

187 "Item": { 

188 "title": "Item", 

189 "required": ["name", "price"], 

190 "type": "object", 

191 "properties": { 

192 "name": {"title": "Name", "type": "string"}, 

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

194 "price": {"title": "Price", "type": "number"}, 

195 "tax": {"title": "Tax", "type": "number"}, 

196 "tags": { 

197 "title": "Tags", 

198 "uniqueItems": True, 

199 "type": "array", 

200 "items": {"type": "string"}, 

201 "default": [], 

202 }, 

203 }, 

204 }, 

205 "ValidationError": { 

206 "title": "ValidationError", 

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

208 "type": "object", 

209 "properties": { 

210 "loc": { 

211 "title": "Location", 

212 "type": "array", 

213 "items": { 

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

215 }, 

216 }, 

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

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

219 }, 

220 }, 

221 "HTTPValidationError": { 

222 "title": "HTTPValidationError", 

223 "type": "object", 

224 "properties": { 

225 "detail": { 

226 "title": "Detail", 

227 "type": "array", 

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

229 } 

230 }, 

231 }, 

232 } 

233 }, 

234 }