Coverage for tests/test_tutorial/test_query_params/test_tutorial006_py310.py: 100%

24 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.query_params.tutorial006_py310 import app 1abc

11 

12 c = TestClient(app) 1abc

13 return c 1abc

14 

15 

16@needs_py310 1deabc

17def test_foo_needy_very(client: TestClient): 1deabc

18 response = client.get("/items/foo?needy=very") 1abc

19 assert response.status_code == 200 1abc

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

21 "item_id": "foo", 

22 "needy": "very", 

23 "skip": 0, 

24 "limit": None, 

25 } 

26 

27 

28@needs_py310 1deabc

29def test_foo_no_needy(client: TestClient): 1deabc

30 response = client.get("/items/foo?skip=a&limit=b") 1abc

31 assert response.status_code == 422 1abc

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

33 { 

34 "detail": [ 

35 { 

36 "type": "missing", 

37 "loc": ["query", "needy"], 

38 "msg": "Field required", 

39 "input": None, 

40 }, 

41 { 

42 "type": "int_parsing", 

43 "loc": ["query", "skip"], 

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

45 "input": "a", 

46 }, 

47 { 

48 "type": "int_parsing", 

49 "loc": ["query", "limit"], 

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

51 "input": "b", 

52 }, 

53 ] 

54 } 

55 ) | IsDict( 

56 # TODO: remove when deprecating Pydantic v1 

57 { 

58 "detail": [ 

59 { 

60 "loc": ["query", "needy"], 

61 "msg": "field required", 

62 "type": "value_error.missing", 

63 }, 

64 { 

65 "loc": ["query", "skip"], 

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

67 "type": "type_error.integer", 

68 }, 

69 { 

70 "loc": ["query", "limit"], 

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

72 "type": "type_error.integer", 

73 }, 

74 ] 

75 } 

76 ) 

77 

78 

79@needs_py310 1deabc

80def test_openapi_schema(client: TestClient): 1deabc

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

82 assert response.status_code == 200 1abc

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

84 "openapi": "3.1.0", 

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

86 "paths": { 

87 "/items/{item_id}": { 

88 "get": { 

89 "responses": { 

90 "200": { 

91 "description": "Successful Response", 

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

93 }, 

94 "422": { 

95 "description": "Validation Error", 

96 "content": { 

97 "application/json": { 

98 "schema": { 

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

100 } 

101 } 

102 }, 

103 }, 

104 }, 

105 "summary": "Read User Item", 

106 "operationId": "read_user_item_items__item_id__get", 

107 "parameters": [ 

108 { 

109 "required": True, 

110 "schema": {"title": "Item Id", "type": "string"}, 

111 "name": "item_id", 

112 "in": "path", 

113 }, 

114 { 

115 "required": True, 

116 "schema": {"title": "Needy", "type": "string"}, 

117 "name": "needy", 

118 "in": "query", 

119 }, 

120 { 

121 "required": False, 

122 "schema": { 

123 "title": "Skip", 

124 "type": "integer", 

125 "default": 0, 

126 }, 

127 "name": "skip", 

128 "in": "query", 

129 }, 

130 { 

131 "required": False, 

132 "schema": IsDict( 

133 { 

134 "anyOf": [{"type": "integer"}, {"type": "null"}], 

135 "title": "Limit", 

136 } 

137 ) 

138 | IsDict( 

139 # TODO: remove when deprecating Pydantic v1 

140 {"title": "Limit", "type": "integer"} 

141 ), 

142 "name": "limit", 

143 "in": "query", 

144 }, 

145 ], 

146 } 

147 } 

148 }, 

149 "components": { 

150 "schemas": { 

151 "ValidationError": { 

152 "title": "ValidationError", 

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

154 "type": "object", 

155 "properties": { 

156 "loc": { 

157 "title": "Location", 

158 "type": "array", 

159 "items": { 

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

161 }, 

162 }, 

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

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

165 }, 

166 }, 

167 "HTTPValidationError": { 

168 "title": "HTTPValidationError", 

169 "type": "object", 

170 "properties": { 

171 "detail": { 

172 "title": "Detail", 

173 "type": "array", 

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

175 } 

176 }, 

177 }, 

178 } 

179 }, 

180 }