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

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from dirty_equals import IsDict 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7from ...utils import needs_py39, needs_py310 1abcdef

8 

9 

10@pytest.fixture( 1abcdef

11 name="client", 

12 params=[ 

13 "tutorial004", 

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

15 "tutorial004_an", 

16 pytest.param("tutorial004_an_py39", marks=needs_py39), 

17 pytest.param("tutorial004_an_py310", marks=needs_py310), 

18 ], 

19) 

20def get_client(request: pytest.FixtureRequest): 1abcdef

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

22 

23 client = TestClient(mod.app) 1abcdef

24 return client 1abcdef

25 

26 

27@pytest.mark.parametrize( 1abcdef

28 "path,expected_status,expected_response", 

29 [ 

30 ( 

31 "/items", 

32 200, 

33 { 

34 "items": [ 

35 {"item_name": "Foo"}, 

36 {"item_name": "Bar"}, 

37 {"item_name": "Baz"}, 

38 ] 

39 }, 

40 ), 

41 ( 

42 "/items?q=foo", 

43 200, 

44 { 

45 "items": [ 

46 {"item_name": "Foo"}, 

47 {"item_name": "Bar"}, 

48 {"item_name": "Baz"}, 

49 ], 

50 "q": "foo", 

51 }, 

52 ), 

53 ( 

54 "/items?q=foo&skip=1", 

55 200, 

56 {"items": [{"item_name": "Bar"}, {"item_name": "Baz"}], "q": "foo"}, 

57 ), 

58 ( 

59 "/items?q=bar&limit=2", 

60 200, 

61 {"items": [{"item_name": "Foo"}, {"item_name": "Bar"}], "q": "bar"}, 

62 ), 

63 ( 

64 "/items?q=bar&skip=1&limit=1", 

65 200, 

66 {"items": [{"item_name": "Bar"}], "q": "bar"}, 

67 ), 

68 ( 

69 "/items?limit=1&q=bar&skip=1", 

70 200, 

71 {"items": [{"item_name": "Bar"}], "q": "bar"}, 

72 ), 

73 ], 

74) 

75def test_get(path, expected_status, expected_response, client: TestClient): 1abcdef

76 response = client.get(path) 1ghijkl

77 assert response.status_code == expected_status 1ghijkl

78 assert response.json() == expected_response 1ghijkl

79 

80 

81def test_openapi_schema(client: TestClient): 1abcdef

82 response = client.get("/openapi.json") 1mnopqr

83 assert response.status_code == 200, response.text 1mnopqr

84 assert response.json() == { 1mnopqr

85 "openapi": "3.1.0", 

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

87 "paths": { 

88 "/items/": { 

89 "get": { 

90 "responses": { 

91 "200": { 

92 "description": "Successful Response", 

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

94 }, 

95 "422": { 

96 "description": "Validation Error", 

97 "content": { 

98 "application/json": { 

99 "schema": { 

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

101 } 

102 } 

103 }, 

104 }, 

105 }, 

106 "summary": "Read Items", 

107 "operationId": "read_items_items__get", 

108 "parameters": [ 

109 { 

110 "required": False, 

111 "schema": IsDict( 

112 { 

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

114 "title": "Q", 

115 } 

116 ) 

117 | IsDict( 

118 # TODO: remove when deprecating Pydantic v1 

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

120 ), 

121 "name": "q", 

122 "in": "query", 

123 }, 

124 { 

125 "required": False, 

126 "schema": { 

127 "title": "Skip", 

128 "type": "integer", 

129 "default": 0, 

130 }, 

131 "name": "skip", 

132 "in": "query", 

133 }, 

134 { 

135 "required": False, 

136 "schema": { 

137 "title": "Limit", 

138 "type": "integer", 

139 "default": 100, 

140 }, 

141 "name": "limit", 

142 "in": "query", 

143 }, 

144 ], 

145 } 

146 } 

147 }, 

148 "components": { 

149 "schemas": { 

150 "ValidationError": { 

151 "title": "ValidationError", 

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

153 "type": "object", 

154 "properties": { 

155 "loc": { 

156 "title": "Location", 

157 "type": "array", 

158 "items": { 

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

160 }, 

161 }, 

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

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

164 }, 

165 }, 

166 "HTTPValidationError": { 

167 "title": "HTTPValidationError", 

168 "type": "object", 

169 "properties": { 

170 "detail": { 

171 "title": "Detail", 

172 "type": "array", 

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

174 } 

175 }, 

176 }, 

177 } 

178 }, 

179 }