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

39 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7from tests.utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

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

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

15 ], 

16) 

17def get_client(request: pytest.FixtureRequest): 1abdc

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

19 

20 client = TestClient(mod.app) 1abc

21 return client 1abc

22 

23 

24def test_cookie_param_model(client: TestClient): 1abdc

25 with client as c: 1efg

26 c.cookies.set("session_id", "123") 1efg

27 c.cookies.set("fatebook_tracker", "456") 1efg

28 c.cookies.set("googall_tracker", "789") 1efg

29 response = c.get("/items/") 1efg

30 assert response.status_code == 200 1efg

31 assert response.json() == { 1efg

32 "session_id": "123", 

33 "fatebook_tracker": "456", 

34 "googall_tracker": "789", 

35 } 

36 

37 

38def test_cookie_param_model_defaults(client: TestClient): 1abdc

39 with client as c: 1klm

40 c.cookies.set("session_id", "123") 1klm

41 response = c.get("/items/") 1klm

42 assert response.status_code == 200 1klm

43 assert response.json() == { 1klm

44 "session_id": "123", 

45 "fatebook_tracker": None, 

46 "googall_tracker": None, 

47 } 

48 

49 

50def test_cookie_param_model_invalid(client: TestClient): 1abdc

51 response = client.get("/items/") 1nop

52 assert response.status_code == 422 1nop

53 assert response.json() == snapshot( 1nop

54 { 

55 "detail": [ 

56 { 

57 "type": "missing", 

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

59 "msg": "Field required", 

60 "input": {}, 

61 } 

62 ] 

63 } 

64 ) 

65 

66 

67def test_cookie_param_model_extra(client: TestClient): 1abdc

68 with client as c: 1hij

69 c.cookies.set("session_id", "123") 1hij

70 c.cookies.set("extra", "track-me-here-too") 1hij

71 response = c.get("/items/") 1hij

72 assert response.status_code == 200 1hij

73 assert response.json() == snapshot( 1hij

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

75 ) 

76 

77 

78def test_openapi_schema(client: TestClient): 1abdc

79 response = client.get("/openapi.json") 1qrs

80 assert response.status_code == 200, response.text 1qrs

81 assert response.json() == snapshot( 1qrs

82 { 

83 "openapi": "3.1.0", 

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

85 "paths": { 

86 "/items/": { 

87 "get": { 

88 "summary": "Read Items", 

89 "operationId": "read_items_items__get", 

90 "parameters": [ 

91 { 

92 "name": "session_id", 

93 "in": "cookie", 

94 "required": True, 

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

96 }, 

97 { 

98 "name": "fatebook_tracker", 

99 "in": "cookie", 

100 "required": False, 

101 "schema": { 

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

103 "title": "Fatebook Tracker", 

104 }, 

105 }, 

106 { 

107 "name": "googall_tracker", 

108 "in": "cookie", 

109 "required": False, 

110 "schema": { 

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

112 "title": "Googall Tracker", 

113 }, 

114 }, 

115 ], 

116 "responses": { 

117 "200": { 

118 "description": "Successful Response", 

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

120 }, 

121 "422": { 

122 "description": "Validation Error", 

123 "content": { 

124 "application/json": { 

125 "schema": { 

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

127 } 

128 } 

129 }, 

130 }, 

131 }, 

132 } 

133 } 

134 }, 

135 "components": { 

136 "schemas": { 

137 "HTTPValidationError": { 

138 "properties": { 

139 "detail": { 

140 "items": { 

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

142 }, 

143 "type": "array", 

144 "title": "Detail", 

145 } 

146 }, 

147 "type": "object", 

148 "title": "HTTPValidationError", 

149 }, 

150 "ValidationError": { 

151 "properties": { 

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

153 "input": {"title": "Input"}, 

154 "loc": { 

155 "items": { 

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

157 }, 

158 "type": "array", 

159 "title": "Location", 

160 }, 

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

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

163 }, 

164 "type": "object", 

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

166 "title": "ValidationError", 

167 }, 

168 } 

169 }, 

170 } 

171 )