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

24 statements  

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

1import pytest 1abcde

2from dirty_equals import IsDict 1abcde

3from fastapi.testclient import TestClient 1abcde

4 

5 

6@pytest.fixture(name="client") 1abcde

7def get_client(): 1abcde

8 from docs_src.body_fields.tutorial001_an import app 1abcde

9 

10 client = TestClient(app) 1abcde

11 return client 1abcde

12 

13 

14def test_items_5(client: TestClient): 1abcde

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

16 assert response.status_code == 200 1abcde

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

18 "item_id": 5, 

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

20 } 

21 

22 

23def test_items_6(client: TestClient): 1abcde

24 response = client.put( 1abcde

25 "/items/6", 

26 json={ 

27 "item": { 

28 "name": "Bar", 

29 "price": 0.2, 

30 "description": "Some bar", 

31 "tax": "5.4", 

32 } 

33 }, 

34 ) 

35 assert response.status_code == 200 1abcde

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

37 "item_id": 6, 

38 "item": { 

39 "name": "Bar", 

40 "price": 0.2, 

41 "description": "Some bar", 

42 "tax": 5.4, 

43 }, 

44 } 

45 

46 

47def test_invalid_price(client: TestClient): 1abcde

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

49 assert response.status_code == 422 1abcde

50 assert response.json() == IsDict( 1abcde

51 { 

52 "detail": [ 

53 { 

54 "type": "greater_than", 

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

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

57 "input": -3.0, 

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

59 } 

60 ] 

61 } 

62 ) | IsDict( 

63 # TODO: remove when deprecating Pydantic v1 

64 { 

65 "detail": [ 

66 { 

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

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

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

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

71 } 

72 ] 

73 } 

74 ) 

75 

76 

77def test_openapi_schema(client: TestClient): 1abcde

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

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

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

81 "openapi": "3.1.0", 

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

83 "paths": { 

84 "/items/{item_id}": { 

85 "put": { 

86 "responses": { 

87 "200": { 

88 "description": "Successful Response", 

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

90 }, 

91 "422": { 

92 "description": "Validation Error", 

93 "content": { 

94 "application/json": { 

95 "schema": { 

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

97 } 

98 } 

99 }, 

100 }, 

101 }, 

102 "summary": "Update Item", 

103 "operationId": "update_item_items__item_id__put", 

104 "parameters": [ 

105 { 

106 "required": True, 

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

108 "name": "item_id", 

109 "in": "path", 

110 } 

111 ], 

112 "requestBody": { 

113 "content": { 

114 "application/json": { 

115 "schema": { 

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

117 } 

118 } 

119 }, 

120 "required": True, 

121 }, 

122 } 

123 } 

124 }, 

125 "components": { 

126 "schemas": { 

127 "Item": { 

128 "title": "Item", 

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

130 "type": "object", 

131 "properties": { 

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

133 "description": IsDict( 

134 { 

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

136 "anyOf": [ 

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

138 {"type": "null"}, 

139 ], 

140 } 

141 ) 

142 | IsDict( 

143 # TODO: remove when deprecating Pydantic v1 

144 { 

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

146 "maxLength": 300, 

147 "type": "string", 

148 } 

149 ), 

150 "price": { 

151 "title": "Price", 

152 "exclusiveMinimum": 0.0, 

153 "type": "number", 

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

155 }, 

156 "tax": IsDict( 

157 { 

158 "title": "Tax", 

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

160 } 

161 ) 

162 | IsDict( 

163 # TODO: remove when deprecating Pydantic v1 

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

165 ), 

166 }, 

167 }, 

168 "Body_update_item_items__item_id__put": { 

169 "title": "Body_update_item_items__item_id__put", 

170 "required": ["item"], 

171 "type": "object", 

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

173 }, 

174 "ValidationError": { 

175 "title": "ValidationError", 

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

177 "type": "object", 

178 "properties": { 

179 "loc": { 

180 "title": "Location", 

181 "type": "array", 

182 "items": { 

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

184 }, 

185 }, 

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

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

188 }, 

189 }, 

190 "HTTPValidationError": { 

191 "title": "HTTPValidationError", 

192 "type": "object", 

193 "properties": { 

194 "detail": { 

195 "title": "Detail", 

196 "type": "array", 

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

198 } 

199 }, 

200 }, 

201 } 

202 }, 

203 }