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

18 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.path_operation_configuration.tutorial005 import app 1abcde

4 

5from ...utils import needs_pydanticv1, needs_pydanticv2 1abcde

6 

7client = TestClient(app) 1abcde

8 

9 

10def test_query_params_str_validations(): 1abcde

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

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

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

14 "name": "Foo", 

15 "price": 42, 

16 "description": None, 

17 "tax": None, 

18 "tags": [], 

19 } 

20 

21 

22@needs_pydanticv2 1abcde

23def test_openapi_schema(): 1abcde

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

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

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

27 "openapi": "3.1.0", 

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

29 "paths": { 

30 "/items/": { 

31 "post": { 

32 "responses": { 

33 "200": { 

34 "description": "The created item", 

35 "content": { 

36 "application/json": { 

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

38 } 

39 }, 

40 }, 

41 "422": { 

42 "description": "Validation Error", 

43 "content": { 

44 "application/json": { 

45 "schema": { 

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

47 } 

48 } 

49 }, 

50 }, 

51 }, 

52 "summary": "Create an item", 

53 "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", 

54 "operationId": "create_item_items__post", 

55 "requestBody": { 

56 "content": { 

57 "application/json": { 

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

59 } 

60 }, 

61 "required": True, 

62 }, 

63 } 

64 } 

65 }, 

66 "components": { 

67 "schemas": { 

68 "Item": { 

69 "title": "Item", 

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

71 "type": "object", 

72 "properties": { 

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

74 "description": { 

75 "title": "Description", 

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

77 }, 

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

79 "tax": { 

80 "title": "Tax", 

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

82 }, 

83 "tags": { 

84 "title": "Tags", 

85 "uniqueItems": True, 

86 "type": "array", 

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

88 "default": [], 

89 }, 

90 }, 

91 }, 

92 "ValidationError": { 

93 "title": "ValidationError", 

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

95 "type": "object", 

96 "properties": { 

97 "loc": { 

98 "title": "Location", 

99 "type": "array", 

100 "items": { 

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

102 }, 

103 }, 

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

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

106 }, 

107 }, 

108 "HTTPValidationError": { 

109 "title": "HTTPValidationError", 

110 "type": "object", 

111 "properties": { 

112 "detail": { 

113 "title": "Detail", 

114 "type": "array", 

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

116 } 

117 }, 

118 }, 

119 } 

120 }, 

121 } 

122 

123 

124# TODO: remove when deprecating Pydantic v1 

125@needs_pydanticv1 1abcde

126def test_openapi_schema_pv1(): 1abcde

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

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

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

130 "openapi": "3.1.0", 

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

132 "paths": { 

133 "/items/": { 

134 "post": { 

135 "responses": { 

136 "200": { 

137 "description": "The created item", 

138 "content": { 

139 "application/json": { 

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

141 } 

142 }, 

143 }, 

144 "422": { 

145 "description": "Validation Error", 

146 "content": { 

147 "application/json": { 

148 "schema": { 

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

150 } 

151 } 

152 }, 

153 }, 

154 }, 

155 "summary": "Create an item", 

156 "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", 

157 "operationId": "create_item_items__post", 

158 "requestBody": { 

159 "content": { 

160 "application/json": { 

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

162 } 

163 }, 

164 "required": True, 

165 }, 

166 } 

167 } 

168 }, 

169 "components": { 

170 "schemas": { 

171 "Item": { 

172 "title": "Item", 

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

174 "type": "object", 

175 "properties": { 

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

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

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

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

180 "tags": { 

181 "title": "Tags", 

182 "uniqueItems": True, 

183 "type": "array", 

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

185 "default": [], 

186 }, 

187 }, 

188 }, 

189 "ValidationError": { 

190 "title": "ValidationError", 

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

192 "type": "object", 

193 "properties": { 

194 "loc": { 

195 "title": "Location", 

196 "type": "array", 

197 "items": { 

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

199 }, 

200 }, 

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

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

203 }, 

204 }, 

205 "HTTPValidationError": { 

206 "title": "HTTPValidationError", 

207 "type": "object", 

208 "properties": { 

209 "detail": { 

210 "title": "Detail", 

211 "type": "array", 

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

213 } 

214 }, 

215 }, 

216 } 

217 }, 

218 }