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

19 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7from ...utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

13 pytest.param("tutorial001_py310", marks=needs_py310), 

14 pytest.param("tutorial001_an_py310", marks=needs_py310), 

15 pytest.param("tutorial001_02_an_py310", marks=needs_py310), 

16 ], 

17) 

18def get_client(request: pytest.FixtureRequest): 1abdc

19 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1abc

20 

21 client = TestClient(mod.app) 1abc

22 return client 1abc

23 

24 

25@pytest.mark.parametrize( 1abdc

26 "path,expected_status,expected_response", 

27 [ 

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

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

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

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

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

33 ], 

34) 

35def test_get(path, expected_status, expected_response, client: TestClient): 1abdc

36 response = client.get(path) 1efg

37 assert response.status_code == expected_status 1efg

38 assert response.json() == expected_response 1efg

39 

40 

41def test_openapi_schema(client: TestClient): 1abdc

42 response = client.get("/openapi.json") 1hij

43 assert response.status_code == 200, response.text 1hij

44 assert response.json() == snapshot( 1hij

45 { 

46 "openapi": "3.1.0", 

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

48 "paths": { 

49 "/items/": { 

50 "get": { 

51 "responses": { 

52 "200": { 

53 "description": "Successful Response", 

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

55 }, 

56 "422": { 

57 "description": "Validation Error", 

58 "content": { 

59 "application/json": { 

60 "schema": { 

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

62 } 

63 } 

64 }, 

65 }, 

66 }, 

67 "summary": "Read Items", 

68 "operationId": "read_items_items__get", 

69 "parameters": [ 

70 { 

71 "required": False, 

72 "schema": { 

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

74 "title": "Q", 

75 }, 

76 "name": "q", 

77 "in": "query", 

78 }, 

79 { 

80 "required": False, 

81 "schema": { 

82 "title": "Skip", 

83 "type": "integer", 

84 "default": 0, 

85 }, 

86 "name": "skip", 

87 "in": "query", 

88 }, 

89 { 

90 "required": False, 

91 "schema": { 

92 "title": "Limit", 

93 "type": "integer", 

94 "default": 100, 

95 }, 

96 "name": "limit", 

97 "in": "query", 

98 }, 

99 ], 

100 } 

101 }, 

102 "/users/": { 

103 "get": { 

104 "responses": { 

105 "200": { 

106 "description": "Successful Response", 

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

108 }, 

109 "422": { 

110 "description": "Validation Error", 

111 "content": { 

112 "application/json": { 

113 "schema": { 

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

115 } 

116 } 

117 }, 

118 }, 

119 }, 

120 "summary": "Read Users", 

121 "operationId": "read_users_users__get", 

122 "parameters": [ 

123 { 

124 "required": False, 

125 "schema": { 

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

127 "title": "Q", 

128 }, 

129 "name": "q", 

130 "in": "query", 

131 }, 

132 { 

133 "required": False, 

134 "schema": { 

135 "title": "Skip", 

136 "type": "integer", 

137 "default": 0, 

138 }, 

139 "name": "skip", 

140 "in": "query", 

141 }, 

142 { 

143 "required": False, 

144 "schema": { 

145 "title": "Limit", 

146 "type": "integer", 

147 "default": 100, 

148 }, 

149 "name": "limit", 

150 "in": "query", 

151 }, 

152 ], 

153 } 

154 }, 

155 }, 

156 "components": { 

157 "schemas": { 

158 "ValidationError": { 

159 "title": "ValidationError", 

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

161 "type": "object", 

162 "properties": { 

163 "loc": { 

164 "title": "Location", 

165 "type": "array", 

166 "items": { 

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

168 }, 

169 }, 

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

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

172 "input": {"title": "Input"}, 

173 "ctx": {"title": "Context", "type": "object"}, 

174 }, 

175 }, 

176 "HTTPValidationError": { 

177 "title": "HTTPValidationError", 

178 "type": "object", 

179 "properties": { 

180 "detail": { 

181 "title": "Detail", 

182 "type": "array", 

183 "items": { 

184 "$ref": "#/components/schemas/ValidationError" 

185 }, 

186 } 

187 }, 

188 }, 

189 } 

190 }, 

191 } 

192 )