Coverage for tests/utils/context/test_main.py: 99%

137 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1from typing import Any 

2 

3import pytest 

4from fast_depends import ValidationError 

5 

6from faststream import Context, ContextRepo 

7from faststream._internal.utils import apply_types 

8 

9 

10def test_context_getattr(context: ContextRepo) -> None: 

11 a = 1000 

12 context.set_global("key", a) 

13 

14 assert context.key is a 

15 assert context.key2 is None 

16 

17 

18@pytest.mark.asyncio() 

19async def test_context_apply(context: ContextRepo) -> None: 

20 a = 1000 

21 context.set_global("key", a) 

22 

23 @apply_types(context__=context) 

24 async def use(key: Any = Context()) -> Any: 

25 return key is a 

26 

27 assert await use() 

28 

29 

30@pytest.mark.asyncio() 

31async def test_context_ignore(context: ContextRepo) -> None: 

32 a = 3 

33 context.set_global("key", a) 

34 

35 @apply_types(context__=context) 

36 async def use() -> None: 

37 return None 

38 

39 assert await use() is None 

40 

41 

42@pytest.mark.asyncio() 

43async def test_context_apply_multi(context: ContextRepo) -> None: 

44 a = 1001 

45 context.set_global("key_a", a) 

46 

47 b = 1000 

48 context.set_global("key_b", b) 

49 

50 @apply_types(context__=context) 

51 async def use1(key_a: Any = Context()) -> Any: 

52 return key_a is a 

53 

54 assert await use1() 

55 

56 @apply_types(context__=context) 

57 async def use2(key_b: Any = Context()) -> Any: 

58 return key_b is b 

59 

60 assert await use2() 

61 

62 @apply_types(context__=context) 

63 async def use3(key_a: Any = Context(), key_b: Any = Context()) -> Any: 

64 return key_a is a and key_b is b 

65 

66 assert await use3() 

67 

68 

69@pytest.mark.asyncio() 

70async def test_context_overrides(context: ContextRepo) -> None: 

71 a = 1001 

72 context.set_global("test", a) 

73 

74 b = 1000 

75 context.set_global("test", b) 

76 

77 @apply_types(context__=context) 

78 async def use(test: Any = Context()) -> Any: 

79 return test is b 

80 

81 assert await use() 

82 

83 

84@pytest.mark.asyncio() 

85async def test_context_nested_apply(context: ContextRepo) -> None: 

86 a = 1000 

87 context.set_global("key", a) 

88 

89 @apply_types(context__=context) 

90 def use_nested(key: Any = Context()) -> Any: 

91 return key 

92 

93 @apply_types(context__=context) 

94 async def use(key: Any = Context()) -> Any: 

95 return key is use_nested() is a 

96 

97 assert await use() 

98 

99 

100@pytest.mark.asyncio() 

101async def test_reset_global(context: ContextRepo) -> None: 

102 a = 1000 

103 context.set_global("key", a) 

104 context.reset_global("key") 

105 

106 @apply_types(context__=context) 

107 async def use(key: Any = Context()) -> None: ... 

108 

109 with pytest.raises(ValidationError): 

110 await use() 

111 

112 

113@pytest.mark.asyncio() 

114async def test_clear_context(context: ContextRepo) -> None: 

115 a = 1000 

116 context.set_global("key", a) 

117 context.clear() 

118 

119 @apply_types(context__=context) 

120 async def use(key: Any = Context(default=None)) -> Any: 

121 return key is None 

122 

123 assert await use() 

124 

125 

126def test_scope(context: ContextRepo) -> None: 

127 @apply_types(context__=context) 

128 def use(key: Any = Context(), key2: Any = Context()) -> None: 

129 assert key == 1 

130 assert key2 == 1 

131 

132 with context.scope("key", 1), context.scope("key2", 1): 

133 use() 

134 

135 assert context.get("key") is None 

136 assert context.get("key2") is None 

137 

138 

139def test_scopes(context: ContextRepo) -> None: 

140 @apply_types(context__=context) 

141 def use(key: Any = Context(), key2: Any = Context()) -> None: 

142 assert key == 1 

143 assert key2 == 2 

144 

145 with context.scopes((("key", 1), ("key2", 2))): 

146 use() 

147 

148 assert context.get("key") is None 

149 assert context.get("key2") is None 

150 

151 

152def test_scopes_restores_a_repeated_key(context: ContextRepo) -> None: 

153 with context.scopes((("key", "first"), ("key", "second"))): 

154 assert context.get("key") == "second" 

155 

156 assert context.get("key") is None 

157 

158 

159def test_default(context: ContextRepo) -> None: 

160 @apply_types(context__=context) 

161 def use( 

162 key: Any = Context(), 

163 key2: Any = Context(), 

164 key3: Any = Context(default=1), 

165 key4: Any = Context("key.key4", default=1), 

166 key5: Any = Context("key5.key6"), 

167 ) -> None: 

168 assert key == 0 

169 assert key2 is True 

170 assert key3 == 1 

171 assert key4 == 1 

172 assert key5 is False 

173 

174 with ( 

175 context.scope("key", 0), 

176 context.scope("key2", True), 

177 context.scope( 

178 "key5", 

179 {"key6": False}, 

180 ), 

181 ): 

182 use() 

183 

184 

185def test_local_default(context: ContextRepo) -> None: 

186 key = "some-key" 

187 

188 tag = context.set_local(key, "useless") 

189 context.reset_local(key, tag) 

190 

191 assert context.get_local(key, 1) == 1 

192 

193 

194def test_initial(context: ContextRepo) -> None: 

195 @apply_types(context__=context) 

196 def use( 

197 a: Any, 

198 key: Any = Context(initial=list), 

199 ) -> Any: 

200 key.append(a) 

201 return key 

202 

203 assert use(1) == [1] 

204 assert use(2) == [1, 2] 

205 

206 

207@pytest.mark.asyncio() 

208async def test_context_with_custom_object_implementing_comparison( 

209 context: ContextRepo, 

210) -> None: 

211 class User: # ruff: ignore[eq-without-hash] - a user object that defines only `__eq__` 

212 def __init__(self, user_id: int) -> None: 

213 self.user_id = user_id 

214 

215 def __eq__(self, other: object) -> Any: 

216 if not isinstance(other, User): 

217 return NotImplemented 

218 return self.user_id == other.user_id 

219 

220 def __ne__(self, other: object) -> Any: 

221 return not self.__eq__(other) 

222 

223 user2 = User(user_id=2) 

224 user3 = User(user_id=3) 

225 

226 @apply_types(context__=context) 

227 async def use( 

228 key1: Any = Context("user1"), 

229 key2: Any = Context("user2", default=user2), 

230 key3: Any = Context("user3", default=user3), 

231 ) -> Any: 

232 return ( 

233 key1 == User(user_id=1) 

234 and key2 == User(user_id=2) 

235 and key3 == User(user_id=4) 

236 ) 

237 

238 with ( 

239 context.scope("user1", User(user_id=1)), 

240 context.scope("user3", User(user_id=4)), 

241 ): 

242 assert await use()