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

20 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.query_params.tutorial006 import app 1abcde

9 

10 c = TestClient(app) 1abcde

11 return c 1abcde

12 

13 

14def test_foo_needy_very(client: TestClient): 1abcde

15 response = client.get("/items/foo?needy=very") 1abcde

16 assert response.status_code == 200 1abcde

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

18 "item_id": "foo", 

19 "needy": "very", 

20 "skip": 0, 

21 "limit": None, 

22 } 

23 

24 

25def test_foo_no_needy(client: TestClient): 1abcde

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

27 assert response.status_code == 422 1abcde

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

29 { 

30 "detail": [ 

31 { 

32 "type": "missing", 

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

34 "msg": "Field required", 

35 "input": None, 

36 }, 

37 { 

38 "type": "int_parsing", 

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

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

41 "input": "a", 

42 }, 

43 { 

44 "type": "int_parsing", 

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

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

47 "input": "b", 

48 }, 

49 ] 

50 } 

51 ) | IsDict( 

52 # TODO: remove when deprecating Pydantic v1 

53 { 

54 "detail": [ 

55 { 

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

57 "msg": "field required", 

58 "type": "value_error.missing", 

59 }, 

60 { 

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

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

63 "type": "type_error.integer", 

64 }, 

65 { 

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

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

68 "type": "type_error.integer", 

69 }, 

70 ] 

71 } 

72 ) 

73 

74 

75def test_openapi_schema(client: TestClient): 1abcde

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

77 assert response.status_code == 200 1abcde

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

79 "openapi": "3.1.0", 

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

81 "paths": { 

82 "/items/{item_id}": { 

83 "get": { 

84 "responses": { 

85 "200": { 

86 "description": "Successful Response", 

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

88 }, 

89 "422": { 

90 "description": "Validation Error", 

91 "content": { 

92 "application/json": { 

93 "schema": { 

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

95 } 

96 } 

97 }, 

98 }, 

99 }, 

100 "summary": "Read User Item", 

101 "operationId": "read_user_item_items__item_id__get", 

102 "parameters": [ 

103 { 

104 "required": True, 

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

106 "name": "item_id", 

107 "in": "path", 

108 }, 

109 { 

110 "required": True, 

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

112 "name": "needy", 

113 "in": "query", 

114 }, 

115 { 

116 "required": False, 

117 "schema": { 

118 "title": "Skip", 

119 "type": "integer", 

120 "default": 0, 

121 }, 

122 "name": "skip", 

123 "in": "query", 

124 }, 

125 { 

126 "required": False, 

127 "schema": IsDict( 

128 { 

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

130 "title": "Limit", 

131 } 

132 ) 

133 | IsDict( 

134 # TODO: remove when deprecating Pydantic v1 

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

136 ), 

137 "name": "limit", 

138 "in": "query", 

139 }, 

140 ], 

141 } 

142 } 

143 }, 

144 "components": { 

145 "schemas": { 

146 "ValidationError": { 

147 "title": "ValidationError", 

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

149 "type": "object", 

150 "properties": { 

151 "loc": { 

152 "title": "Location", 

153 "type": "array", 

154 "items": { 

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

156 }, 

157 }, 

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

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

160 }, 

161 }, 

162 "HTTPValidationError": { 

163 "title": "HTTPValidationError", 

164 "type": "object", 

165 "properties": { 

166 "detail": { 

167 "title": "Detail", 

168 "type": "array", 

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

170 } 

171 }, 

172 }, 

173 } 

174 }, 

175 }