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

20 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1eabcd

2from dirty_equals import IsDict 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

8@pytest.fixture(name="client") 1eabcd

9def get_client(): 1eabcd

10 from docs_src.dependencies.tutorial004_an_py39 import app 1abcd

11 

12 client = TestClient(app) 1abcd

13 return client 1abcd

14 

15 

16@needs_py39 1eabcd

17@pytest.mark.parametrize( 1eabcd

18 "path,expected_status,expected_response", 

19 [ 

20 ( 

21 "/items", 

22 200, 

23 { 

24 "items": [ 

25 {"item_name": "Foo"}, 

26 {"item_name": "Bar"}, 

27 {"item_name": "Baz"}, 

28 ] 

29 }, 

30 ), 

31 ( 

32 "/items?q=foo", 

33 200, 

34 { 

35 "items": [ 

36 {"item_name": "Foo"}, 

37 {"item_name": "Bar"}, 

38 {"item_name": "Baz"}, 

39 ], 

40 "q": "foo", 

41 }, 

42 ), 

43 ( 

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

45 200, 

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

47 ), 

48 ( 

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

50 200, 

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

52 ), 

53 ( 

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

55 200, 

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

57 ), 

58 ( 

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

60 200, 

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

62 ), 

63 ], 

64) 

65def test_get(path, expected_status, expected_response, client: TestClient): 1eabcd

66 response = client.get(path) 1abcd

67 assert response.status_code == expected_status 1abcd

68 assert response.json() == expected_response 1abcd

69 

70 

71@needs_py39 1eabcd

72def test_openapi_schema(client: TestClient): 1eabcd

73 response = client.get("/openapi.json") 1abcd

74 assert response.status_code == 200, response.text 1abcd

75 assert response.json() == { 1abcd

76 "openapi": "3.1.0", 

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

78 "paths": { 

79 "/items/": { 

80 "get": { 

81 "responses": { 

82 "200": { 

83 "description": "Successful Response", 

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

85 }, 

86 "422": { 

87 "description": "Validation Error", 

88 "content": { 

89 "application/json": { 

90 "schema": { 

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

92 } 

93 } 

94 }, 

95 }, 

96 }, 

97 "summary": "Read Items", 

98 "operationId": "read_items_items__get", 

99 "parameters": [ 

100 { 

101 "required": False, 

102 "schema": IsDict( 

103 { 

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

105 "title": "Q", 

106 } 

107 ) 

108 | IsDict( 

109 # TODO: remove when deprecating Pydantic v1 

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

111 ), 

112 "name": "q", 

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

128 "title": "Limit", 

129 "type": "integer", 

130 "default": 100, 

131 }, 

132 "name": "limit", 

133 "in": "query", 

134 }, 

135 ], 

136 } 

137 } 

138 }, 

139 "components": { 

140 "schemas": { 

141 "ValidationError": { 

142 "title": "ValidationError", 

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

144 "type": "object", 

145 "properties": { 

146 "loc": { 

147 "title": "Location", 

148 "type": "array", 

149 "items": { 

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

151 }, 

152 }, 

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

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

155 }, 

156 }, 

157 "HTTPValidationError": { 

158 "title": "HTTPValidationError", 

159 "type": "object", 

160 "properties": { 

161 "detail": { 

162 "title": "Detail", 

163 "type": "array", 

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

165 } 

166 }, 

167 }, 

168 } 

169 }, 

170 }