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

28 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_multiple_params.tutorial001 import app 1abcde

9 

10 client = TestClient(app) 1abcde

11 return client 1abcde

12 

13 

14def test_post_body_q_bar_content(client: TestClient): 1abcde

15 response = client.put("/items/5?q=bar", json={"name": "Foo", "price": 50.5}) 1abcde

16 assert response.status_code == 200 1abcde

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

18 "item_id": 5, 

19 "item": { 

20 "name": "Foo", 

21 "price": 50.5, 

22 "description": None, 

23 "tax": None, 

24 }, 

25 "q": "bar", 

26 } 

27 

28 

29def test_post_no_body_q_bar(client: TestClient): 1abcde

30 response = client.put("/items/5?q=bar", json=None) 1abcde

31 assert response.status_code == 200 1abcde

32 assert response.json() == {"item_id": 5, "q": "bar"} 1abcde

33 

34 

35def test_post_no_body(client: TestClient): 1abcde

36 response = client.put("/items/5", json=None) 1abcde

37 assert response.status_code == 200 1abcde

38 assert response.json() == {"item_id": 5} 1abcde

39 

40 

41def test_post_id_foo(client: TestClient): 1abcde

42 response = client.put("/items/foo", json=None) 1abcde

43 assert response.status_code == 422 1abcde

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

45 { 

46 "detail": [ 

47 { 

48 "type": "int_parsing", 

49 "loc": ["path", "item_id"], 

50 "msg": "Input should be a valid integer, unable to parse string as an integer", 

51 "input": "foo", 

52 } 

53 ] 

54 } 

55 ) | IsDict( 

56 # TODO: remove when deprecating Pydantic v1 

57 { 

58 "detail": [ 

59 { 

60 "loc": ["path", "item_id"], 

61 "msg": "value is not a valid integer", 

62 "type": "type_error.integer", 

63 } 

64 ] 

65 } 

66 ) 

67 

68 

69def test_openapi_schema(client: TestClient): 1abcde

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

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

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

73 "openapi": "3.1.0", 

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

75 "paths": { 

76 "/items/{item_id}": { 

77 "put": { 

78 "responses": { 

79 "200": { 

80 "description": "Successful Response", 

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

82 }, 

83 "422": { 

84 "description": "Validation Error", 

85 "content": { 

86 "application/json": { 

87 "schema": { 

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

89 } 

90 } 

91 }, 

92 }, 

93 }, 

94 "summary": "Update Item", 

95 "operationId": "update_item_items__item_id__put", 

96 "parameters": [ 

97 { 

98 "required": True, 

99 "schema": { 

100 "title": "The ID of the item to get", 

101 "maximum": 1000.0, 

102 "minimum": 0.0, 

103 "type": "integer", 

104 }, 

105 "name": "item_id", 

106 "in": "path", 

107 }, 

108 { 

109 "required": False, 

110 "schema": IsDict( 

111 { 

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

113 "title": "Q", 

114 } 

115 ) 

116 | IsDict( 

117 # TODO: remove when deprecating Pydantic v1 

118 {"title": "Q", "type": "string"} 

119 ), 

120 "name": "q", 

121 "in": "query", 

122 }, 

123 ], 

124 "requestBody": { 

125 "content": { 

126 "application/json": { 

127 "schema": IsDict( 

128 { 

129 "anyOf": [ 

130 {"$ref": "#/components/schemas/Item"}, 

131 {"type": "null"}, 

132 ], 

133 "title": "Item", 

134 } 

135 ) 

136 | IsDict( 

137 # TODO: remove when deprecating Pydantic v1 

138 {"$ref": "#/components/schemas/Item"} 

139 ) 

140 } 

141 } 

142 }, 

143 } 

144 } 

145 }, 

146 "components": { 

147 "schemas": { 

148 "Item": { 

149 "title": "Item", 

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

151 "type": "object", 

152 "properties": { 

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

154 "description": IsDict( 

155 { 

156 "title": "Description", 

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

158 } 

159 ) 

160 | IsDict( 

161 # TODO: remove when deprecating Pydantic v1 

162 {"title": "Description", "type": "string"} 

163 ), 

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

165 "tax": IsDict( 

166 { 

167 "title": "Tax", 

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

169 } 

170 ) 

171 | IsDict( 

172 # TODO: remove when deprecating Pydantic v1 

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

174 ), 

175 }, 

176 }, 

177 "ValidationError": { 

178 "title": "ValidationError", 

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

180 "type": "object", 

181 "properties": { 

182 "loc": { 

183 "title": "Location", 

184 "type": "array", 

185 "items": { 

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

187 }, 

188 }, 

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

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

191 }, 

192 }, 

193 "HTTPValidationError": { 

194 "title": "HTTPValidationError", 

195 "type": "object", 

196 "properties": { 

197 "detail": { 

198 "title": "Detail", 

199 "type": "array", 

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

201 } 

202 }, 

203 }, 

204 } 

205 }, 

206 }