Coverage for tests/test_tutorial/test_body_fields/test_tutorial001_an_py310.py: 100%

29 statements  

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

1import pytest 1deabc

2from dirty_equals import IsDict 1deabc

3from fastapi.testclient import TestClient 1deabc

4 

5from ...utils import needs_py310 1deabc

6 

7 

8@pytest.fixture(name="client") 1deabc

9def get_client(): 1deabc

10 from docs_src.body_fields.tutorial001_an_py310 import app 1abc

11 

12 client = TestClient(app) 1abc

13 return client 1abc

14 

15 

16@needs_py310 1deabc

17def test_items_5(client: TestClient): 1deabc

18 response = client.put("/items/5", json={"item": {"name": "Foo", "price": 3.0}}) 1abc

19 assert response.status_code == 200 1abc

20 assert response.json() == { 1abc

21 "item_id": 5, 

22 "item": {"name": "Foo", "price": 3.0, "description": None, "tax": None}, 

23 } 

24 

25 

26@needs_py310 1deabc

27def test_items_6(client: TestClient): 1deabc

28 response = client.put( 1abc

29 "/items/6", 

30 json={ 

31 "item": { 

32 "name": "Bar", 

33 "price": 0.2, 

34 "description": "Some bar", 

35 "tax": "5.4", 

36 } 

37 }, 

38 ) 

39 assert response.status_code == 200 1abc

40 assert response.json() == { 1abc

41 "item_id": 6, 

42 "item": { 

43 "name": "Bar", 

44 "price": 0.2, 

45 "description": "Some bar", 

46 "tax": 5.4, 

47 }, 

48 } 

49 

50 

51@needs_py310 1deabc

52def test_invalid_price(client: TestClient): 1deabc

53 response = client.put("/items/5", json={"item": {"name": "Foo", "price": -3.0}}) 1abc

54 assert response.status_code == 422 1abc

55 assert response.json() == IsDict( 1abc

56 { 

57 "detail": [ 

58 { 

59 "type": "greater_than", 

60 "loc": ["body", "item", "price"], 

61 "msg": "Input should be greater than 0", 

62 "input": -3.0, 

63 "ctx": {"gt": 0.0}, 

64 } 

65 ] 

66 } 

67 ) | IsDict( 

68 # TODO: remove when deprecating Pydantic v1 

69 { 

70 "detail": [ 

71 { 

72 "ctx": {"limit_value": 0}, 

73 "loc": ["body", "item", "price"], 

74 "msg": "ensure this value is greater than 0", 

75 "type": "value_error.number.not_gt", 

76 } 

77 ] 

78 } 

79 ) 

80 

81 

82@needs_py310 1deabc

83def test_openapi_schema(client: TestClient): 1deabc

84 response = client.get("/openapi.json") 1abc

85 assert response.status_code == 200, response.text 1abc

86 assert response.json() == { 1abc

87 "openapi": "3.1.0", 

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

89 "paths": { 

90 "/items/{item_id}": { 

91 "put": { 

92 "responses": { 

93 "200": { 

94 "description": "Successful Response", 

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

96 }, 

97 "422": { 

98 "description": "Validation Error", 

99 "content": { 

100 "application/json": { 

101 "schema": { 

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

103 } 

104 } 

105 }, 

106 }, 

107 }, 

108 "summary": "Update Item", 

109 "operationId": "update_item_items__item_id__put", 

110 "parameters": [ 

111 { 

112 "required": True, 

113 "schema": {"title": "Item Id", "type": "integer"}, 

114 "name": "item_id", 

115 "in": "path", 

116 } 

117 ], 

118 "requestBody": { 

119 "content": { 

120 "application/json": { 

121 "schema": { 

122 "$ref": "#/components/schemas/Body_update_item_items__item_id__put" 

123 } 

124 } 

125 }, 

126 "required": True, 

127 }, 

128 } 

129 } 

130 }, 

131 "components": { 

132 "schemas": { 

133 "Item": { 

134 "title": "Item", 

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

136 "type": "object", 

137 "properties": { 

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

139 "description": IsDict( 

140 { 

141 "title": "The description of the item", 

142 "anyOf": [ 

143 {"maxLength": 300, "type": "string"}, 

144 {"type": "null"}, 

145 ], 

146 } 

147 ) 

148 | IsDict( 

149 # TODO: remove when deprecating Pydantic v1 

150 { 

151 "title": "The description of the item", 

152 "maxLength": 300, 

153 "type": "string", 

154 } 

155 ), 

156 "price": { 

157 "title": "Price", 

158 "exclusiveMinimum": 0.0, 

159 "type": "number", 

160 "description": "The price must be greater than zero", 

161 }, 

162 "tax": IsDict( 

163 { 

164 "title": "Tax", 

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

166 } 

167 ) 

168 | IsDict( 

169 # TODO: remove when deprecating Pydantic v1 

170 {"title": "Tax", "type": "number"} 

171 ), 

172 }, 

173 }, 

174 "Body_update_item_items__item_id__put": { 

175 "title": "Body_update_item_items__item_id__put", 

176 "required": ["item"], 

177 "type": "object", 

178 "properties": {"item": {"$ref": "#/components/schemas/Item"}}, 

179 }, 

180 "ValidationError": { 

181 "title": "ValidationError", 

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

183 "type": "object", 

184 "properties": { 

185 "loc": { 

186 "title": "Location", 

187 "type": "array", 

188 "items": { 

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

190 }, 

191 }, 

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

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

194 }, 

195 }, 

196 "HTTPValidationError": { 

197 "title": "HTTPValidationError", 

198 "type": "object", 

199 "properties": { 

200 "detail": { 

201 "title": "Detail", 

202 "type": "array", 

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

204 } 

205 }, 

206 }, 

207 } 

208 }, 

209 }