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

19 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7from ...utils import needs_py310 1abcd

8 

9 

10@pytest.fixture( 1abcd

11 name="client", 

12 params=[ 

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

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

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

16 pytest.param("tutorial003_an_py310", marks=needs_py310), 

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

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

19 ], 

20) 

21def get_client(request: pytest.FixtureRequest): 1abcd

22 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQb

23 

24 client = TestClient(mod.app) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQb

25 return client 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQb

26 

27 

28@pytest.mark.parametrize( 1abcd

29 "path,expected_status,expected_response", 

30 [ 

31 ( 

32 "/items", 

33 200, 

34 { 

35 "items": [ 

36 {"item_name": "Foo"}, 

37 {"item_name": "Bar"}, 

38 {"item_name": "Baz"}, 

39 ] 

40 }, 

41 ), 

42 ( 

43 "/items?q=foo", 

44 200, 

45 { 

46 "items": [ 

47 {"item_name": "Foo"}, 

48 {"item_name": "Bar"}, 

49 {"item_name": "Baz"}, 

50 ], 

51 "q": "foo", 

52 }, 

53 ), 

54 ( 

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

56 200, 

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

58 ), 

59 ( 

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

61 200, 

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

63 ), 

64 ( 

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

66 200, 

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

68 ), 

69 ( 

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

71 200, 

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

73 ), 

74 ], 

75) 

76def test_get(path, expected_status, expected_response, client: TestClient): 1abcd

77 response = client.get(path) 2RbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!c#c

78 assert response.status_code == expected_status 2RbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!c#c

79 assert response.json() == expected_response 2RbSbTbUbVbWbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcecfcgchcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!c#c

80 

81 

82def test_openapi_schema(client: TestClient): 1abcd

83 response = client.get("/openapi.json") 2$c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c

84 assert response.status_code == 200, response.text 2$c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c

85 assert response.json() == snapshot( 2$c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c

86 { 

87 "openapi": "3.1.0", 

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

89 "paths": { 

90 "/items/": { 

91 "get": { 

92 "responses": { 

93 "200": { 

94 "description": "Successful Response", 

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

96 }, 

97 "422": { 

98 "description": "Validation Error", 

99 "content": { 

100 "application/json": { 

101 "schema": { 

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

103 } 

104 } 

105 }, 

106 }, 

107 }, 

108 "summary": "Read Items", 

109 "operationId": "read_items_items__get", 

110 "parameters": [ 

111 { 

112 "required": False, 

113 "schema": { 

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

115 "title": "Q", 

116 }, 

117 "name": "q", 

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": { 

133 "title": "Limit", 

134 "type": "integer", 

135 "default": 100, 

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 "input": {"title": "Input"}, 

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

162 }, 

163 }, 

164 "HTTPValidationError": { 

165 "title": "HTTPValidationError", 

166 "type": "object", 

167 "properties": { 

168 "detail": { 

169 "title": "Detail", 

170 "type": "array", 

171 "items": { 

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

173 }, 

174 } 

175 }, 

176 }, 

177 } 

178 }, 

179 } 

180 )