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

20 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.dependencies.tutorial001_an_py310 import app 1abc

11 

12 client = TestClient(app) 1abc

13 return client 1abc

14 

15 

16@needs_py310 1deabc

17@pytest.mark.parametrize( 1deabc

18 "path,expected_status,expected_response", 

19 [ 

20 ("/items", 200, {"q": None, "skip": 0, "limit": 100}), 

21 ("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}), 

22 ("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}), 

23 ("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}), 

24 ("/users", 200, {"q": None, "skip": 0, "limit": 100}), 

25 ], 

26) 

27def test_get(path, expected_status, expected_response, client: TestClient): 1deabc

28 response = client.get(path) 1abc

29 assert response.status_code == expected_status 1abc

30 assert response.json() == expected_response 1abc

31 

32 

33@needs_py310 1deabc

34def test_openapi_schema(client: TestClient): 1deabc

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

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

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

38 "openapi": "3.1.0", 

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

40 "paths": { 

41 "/items/": { 

42 "get": { 

43 "responses": { 

44 "200": { 

45 "description": "Successful Response", 

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

47 }, 

48 "422": { 

49 "description": "Validation Error", 

50 "content": { 

51 "application/json": { 

52 "schema": { 

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

54 } 

55 } 

56 }, 

57 }, 

58 }, 

59 "summary": "Read Items", 

60 "operationId": "read_items_items__get", 

61 "parameters": [ 

62 { 

63 "required": False, 

64 "schema": IsDict( 

65 { 

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

67 "title": "Q", 

68 } 

69 ) 

70 | IsDict( 

71 # TODO: remove when deprecating Pydantic v1 

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

73 ), 

74 "name": "q", 

75 "in": "query", 

76 }, 

77 { 

78 "required": False, 

79 "schema": { 

80 "title": "Skip", 

81 "type": "integer", 

82 "default": 0, 

83 }, 

84 "name": "skip", 

85 "in": "query", 

86 }, 

87 { 

88 "required": False, 

89 "schema": { 

90 "title": "Limit", 

91 "type": "integer", 

92 "default": 100, 

93 }, 

94 "name": "limit", 

95 "in": "query", 

96 }, 

97 ], 

98 } 

99 }, 

100 "/users/": { 

101 "get": { 

102 "responses": { 

103 "200": { 

104 "description": "Successful Response", 

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

106 }, 

107 "422": { 

108 "description": "Validation Error", 

109 "content": { 

110 "application/json": { 

111 "schema": { 

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

113 } 

114 } 

115 }, 

116 }, 

117 }, 

118 "summary": "Read Users", 

119 "operationId": "read_users_users__get", 

120 "parameters": [ 

121 { 

122 "required": False, 

123 "schema": IsDict( 

124 { 

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

126 "title": "Q", 

127 } 

128 ) 

129 | IsDict( 

130 # TODO: remove when deprecating Pydantic v1 

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

132 ), 

133 "name": "q", 

134 "in": "query", 

135 }, 

136 { 

137 "required": False, 

138 "schema": { 

139 "title": "Skip", 

140 "type": "integer", 

141 "default": 0, 

142 }, 

143 "name": "skip", 

144 "in": "query", 

145 }, 

146 { 

147 "required": False, 

148 "schema": { 

149 "title": "Limit", 

150 "type": "integer", 

151 "default": 100, 

152 }, 

153 "name": "limit", 

154 "in": "query", 

155 }, 

156 ], 

157 } 

158 }, 

159 }, 

160 "components": { 

161 "schemas": { 

162 "ValidationError": { 

163 "title": "ValidationError", 

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

165 "type": "object", 

166 "properties": { 

167 "loc": { 

168 "title": "Location", 

169 "type": "array", 

170 "items": { 

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

172 }, 

173 }, 

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

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

176 }, 

177 }, 

178 "HTTPValidationError": { 

179 "title": "HTTPValidationError", 

180 "type": "object", 

181 "properties": { 

182 "detail": { 

183 "title": "Detail", 

184 "type": "array", 

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

186 } 

187 }, 

188 }, 

189 } 

190 }, 

191 }