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

14 statements  

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

1import pytest 1abcde

2from dirty_equals import IsDict 1abcde

3from fastapi.testclient import TestClient 1abcde

4 

5from docs_src.dependencies.tutorial004 import app 1abcde

6 

7client = TestClient(app) 1abcde

8 

9 

10@pytest.mark.parametrize( 1abcde

11 "path,expected_status,expected_response", 

12 [ 

13 ( 

14 "/items", 

15 200, 

16 { 

17 "items": [ 

18 {"item_name": "Foo"}, 

19 {"item_name": "Bar"}, 

20 {"item_name": "Baz"}, 

21 ] 

22 }, 

23 ), 

24 ( 

25 "/items?q=foo", 

26 200, 

27 { 

28 "items": [ 

29 {"item_name": "Foo"}, 

30 {"item_name": "Bar"}, 

31 {"item_name": "Baz"}, 

32 ], 

33 "q": "foo", 

34 }, 

35 ), 

36 ( 

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

38 200, 

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

40 ), 

41 ( 

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

43 200, 

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

45 ), 

46 ( 

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

48 200, 

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

50 ), 

51 ( 

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

53 200, 

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

55 ), 

56 ], 

57) 

58def test_get(path, expected_status, expected_response): 1abcde

59 response = client.get(path) 1abcde

60 assert response.status_code == expected_status 1abcde

61 assert response.json() == expected_response 1abcde

62 

63 

64def test_openapi_schema(): 1abcde

65 response = client.get("/openapi.json") 1abcde

66 assert response.status_code == 200, response.text 1abcde

67 assert response.json() == { 1abcde

68 "openapi": "3.1.0", 

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

70 "paths": { 

71 "/items/": { 

72 "get": { 

73 "responses": { 

74 "200": { 

75 "description": "Successful Response", 

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

77 }, 

78 "422": { 

79 "description": "Validation Error", 

80 "content": { 

81 "application/json": { 

82 "schema": { 

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

84 } 

85 } 

86 }, 

87 }, 

88 }, 

89 "summary": "Read Items", 

90 "operationId": "read_items_items__get", 

91 "parameters": [ 

92 { 

93 "required": False, 

94 "schema": IsDict( 

95 { 

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

97 "title": "Q", 

98 } 

99 ) 

100 | IsDict( 

101 # TODO: remove when deprecating Pydantic v1 

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

103 ), 

104 "name": "q", 

105 "in": "query", 

106 }, 

107 { 

108 "required": False, 

109 "schema": { 

110 "title": "Skip", 

111 "type": "integer", 

112 "default": 0, 

113 }, 

114 "name": "skip", 

115 "in": "query", 

116 }, 

117 { 

118 "required": False, 

119 "schema": { 

120 "title": "Limit", 

121 "type": "integer", 

122 "default": 100, 

123 }, 

124 "name": "limit", 

125 "in": "query", 

126 }, 

127 ], 

128 } 

129 } 

130 }, 

131 "components": { 

132 "schemas": { 

133 "ValidationError": { 

134 "title": "ValidationError", 

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

136 "type": "object", 

137 "properties": { 

138 "loc": { 

139 "title": "Location", 

140 "type": "array", 

141 "items": { 

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

143 }, 

144 }, 

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

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

147 }, 

148 }, 

149 "HTTPValidationError": { 

150 "title": "HTTPValidationError", 

151 "type": "object", 

152 "properties": { 

153 "detail": { 

154 "title": "Detail", 

155 "type": "array", 

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

157 } 

158 }, 

159 }, 

160 } 

161 }, 

162 }