Coverage for tests/test_tutorial/test_cookie_param_models/test_tutorial001.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-01-13 13:38 +0000

1import importlib 1abcde

2 

3import pytest 1abcde

4from dirty_equals import IsDict 1abcde

5from fastapi.testclient import TestClient 1abcde

6from inline_snapshot import snapshot 1abcde

7 

8from tests.utils import needs_py39, needs_py310 1abcde

9 

10 

11@pytest.fixture( 1abcde

12 name="client", 

13 params=[ 

14 "tutorial001", 

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

16 "tutorial001_an", 

17 pytest.param("tutorial001_an_py39", marks=needs_py39), 

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

19 ], 

20) 

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

22 mod = importlib.import_module(f"docs_src.cookie_param_models.{request.param}") 1abcde

23 

24 client = TestClient(mod.app) 1abcde

25 return client 1abcde

26 

27 

28def test_cookie_param_model(client: TestClient): 1abcde

29 with client as c: 1fghij

30 c.cookies.set("session_id", "123") 1fghij

31 c.cookies.set("fatebook_tracker", "456") 1fghij

32 c.cookies.set("googall_tracker", "789") 1fghij

33 response = c.get("/items/") 1fghij

34 assert response.status_code == 200 1fghij

35 assert response.json() == { 1fghij

36 "session_id": "123", 

37 "fatebook_tracker": "456", 

38 "googall_tracker": "789", 

39 } 

40 

41 

42def test_cookie_param_model_defaults(client: TestClient): 1abcde

43 with client as c: 1pqrst

44 c.cookies.set("session_id", "123") 1pqrst

45 response = c.get("/items/") 1pqrst

46 assert response.status_code == 200 1pqrst

47 assert response.json() == { 1pqrst

48 "session_id": "123", 

49 "fatebook_tracker": None, 

50 "googall_tracker": None, 

51 } 

52 

53 

54def test_cookie_param_model_invalid(client: TestClient): 1abcde

55 response = client.get("/items/") 1uvwxy

56 assert response.status_code == 422 1uvwxy

57 assert response.json() == snapshot( 1uvwxy

58 IsDict( 

59 { 

60 "detail": [ 

61 { 

62 "type": "missing", 

63 "loc": ["cookie", "session_id"], 

64 "msg": "Field required", 

65 "input": {}, 

66 } 

67 ] 

68 } 

69 ) 

70 | IsDict( 

71 # TODO: remove when deprecating Pydantic v1 

72 { 

73 "detail": [ 

74 { 

75 "type": "value_error.missing", 

76 "loc": ["cookie", "session_id"], 

77 "msg": "field required", 

78 } 

79 ] 

80 } 

81 ) 

82 ) 

83 

84 

85def test_cookie_param_model_extra(client: TestClient): 1abcde

86 with client as c: 1klmno

87 c.cookies.set("session_id", "123") 1klmno

88 c.cookies.set("extra", "track-me-here-too") 1klmno

89 response = c.get("/items/") 1klmno

90 assert response.status_code == 200 1klmno

91 assert response.json() == snapshot( 1klmno

92 {"session_id": "123", "fatebook_tracker": None, "googall_tracker": None} 

93 ) 

94 

95 

96def test_openapi_schema(client: TestClient): 1abcde

97 response = client.get("/openapi.json") 1zABCD

98 assert response.status_code == 200, response.text 1zABCD

99 assert response.json() == snapshot( 1zABCD

100 { 

101 "openapi": "3.1.0", 

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

103 "paths": { 

104 "/items/": { 

105 "get": { 

106 "summary": "Read Items", 

107 "operationId": "read_items_items__get", 

108 "parameters": [ 

109 { 

110 "name": "session_id", 

111 "in": "cookie", 

112 "required": True, 

113 "schema": {"type": "string", "title": "Session Id"}, 

114 }, 

115 { 

116 "name": "fatebook_tracker", 

117 "in": "cookie", 

118 "required": False, 

119 "schema": IsDict( 

120 { 

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

122 "title": "Fatebook Tracker", 

123 } 

124 ) 

125 | IsDict( 

126 # TODO: remove when deprecating Pydantic v1 

127 { 

128 "type": "string", 

129 "title": "Fatebook Tracker", 

130 } 

131 ), 

132 }, 

133 { 

134 "name": "googall_tracker", 

135 "in": "cookie", 

136 "required": False, 

137 "schema": IsDict( 

138 { 

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

140 "title": "Googall Tracker", 

141 } 

142 ) 

143 | IsDict( 

144 # TODO: remove when deprecating Pydantic v1 

145 { 

146 "type": "string", 

147 "title": "Googall Tracker", 

148 } 

149 ), 

150 }, 

151 ], 

152 "responses": { 

153 "200": { 

154 "description": "Successful Response", 

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

156 }, 

157 "422": { 

158 "description": "Validation Error", 

159 "content": { 

160 "application/json": { 

161 "schema": { 

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

163 } 

164 } 

165 }, 

166 }, 

167 }, 

168 } 

169 } 

170 }, 

171 "components": { 

172 "schemas": { 

173 "HTTPValidationError": { 

174 "properties": { 

175 "detail": { 

176 "items": { 

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

178 }, 

179 "type": "array", 

180 "title": "Detail", 

181 } 

182 }, 

183 "type": "object", 

184 "title": "HTTPValidationError", 

185 }, 

186 "ValidationError": { 

187 "properties": { 

188 "loc": { 

189 "items": { 

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

191 }, 

192 "type": "array", 

193 "title": "Location", 

194 }, 

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

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

197 }, 

198 "type": "object", 

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

200 "title": "ValidationError", 

201 }, 

202 } 

203 }, 

204 } 

205 )