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

124 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-08 01:48 +0000

1import pytest 

2from fast_depends import ValidationError 

3 

4from faststream import Context, ContextRepo 

5from faststream._internal.utils import apply_types 

6 

7 

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

9 a = 1000 

10 context.set_global("key", a) 

11 

12 assert context.key is a 

13 assert context.key2 is None 

14 

15 

16@pytest.mark.asyncio() 

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

18 a = 1000 

19 context.set_global("key", a) 

20 

21 @apply_types(context__=context) 

22 async def use(key=Context()): 

23 return key is a 

24 

25 assert await use() 

26 

27 

28@pytest.mark.asyncio() 

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

30 a = 3 

31 context.set_global("key", a) 

32 

33 @apply_types(context__=context) 

34 async def use() -> None: 

35 return None 

36 

37 assert await use() is None 

38 

39 

40@pytest.mark.asyncio() 

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

42 a = 1001 

43 context.set_global("key_a", a) 

44 

45 b = 1000 

46 context.set_global("key_b", b) 

47 

48 @apply_types(context__=context) 

49 async def use1(key_a=Context()): 

50 return key_a is a 

51 

52 assert await use1() 

53 

54 @apply_types(context__=context) 

55 async def use2(key_b=Context()): 

56 return key_b is b 

57 

58 assert await use2() 

59 

60 @apply_types(context__=context) 

61 async def use3(key_a=Context(), key_b=Context()): 

62 return key_a is a and key_b is b 

63 

64 assert await use3() 

65 

66 

67@pytest.mark.asyncio() 

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

69 a = 1001 

70 context.set_global("test", a) 

71 

72 b = 1000 

73 context.set_global("test", b) 

74 

75 @apply_types(context__=context) 

76 async def use(test=Context()): 

77 return test is b 

78 

79 assert await use() 

80 

81 

82@pytest.mark.asyncio() 

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

84 a = 1000 

85 context.set_global("key", a) 

86 

87 @apply_types(context__=context) 

88 def use_nested(key=Context()): 

89 return key 

90 

91 @apply_types(context__=context) 

92 async def use(key=Context()): 

93 return key is use_nested() is a 

94 

95 assert await use() 

96 

97 

98@pytest.mark.asyncio() 

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

100 a = 1000 

101 context.set_global("key", a) 

102 context.reset_global("key") 

103 

104 @apply_types(context__=context) 

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

106 

107 with pytest.raises(ValidationError): 

108 await use() 

109 

110 

111@pytest.mark.asyncio() 

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

113 a = 1000 

114 context.set_global("key", a) 

115 context.clear() 

116 

117 @apply_types(context__=context) 

118 async def use(key=Context(default=None)): 

119 return key is None 

120 

121 assert await use() 

122 

123 

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

125 @apply_types(context__=context) 

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

127 assert key == 1 

128 assert key2 == 1 

129 

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

131 use() 

132 

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

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

135 

136 

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

138 @apply_types(context__=context) 

139 def use( 

140 key=Context(), 

141 key2=Context(), 

142 key3=Context(default=1), 

143 key4=Context("key.key4", default=1), 

144 key5=Context("key5.key6"), 

145 ) -> None: 

146 assert key == 0 

147 assert key2 is True 

148 assert key3 == 1 

149 assert key4 == 1 

150 assert key5 is False 

151 

152 with ( 

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

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

155 context.scope( 

156 "key5", 

157 {"key6": False}, 

158 ), 

159 ): 

160 use() 

161 

162 

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

164 key = "some-key" 

165 

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

167 context.reset_local(key, tag) 

168 

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

170 

171 

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

173 @apply_types(context__=context) 

174 def use( 

175 a, 

176 key=Context(initial=list), 

177 ): 

178 key.append(a) 

179 return key 

180 

181 assert use(1) == [1] 

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

183 

184 

185@pytest.mark.asyncio() 

186async def test_context_with_custom_object_implementing_comparison( 

187 context: ContextRepo, 

188) -> None: 

189 class User: 

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

191 self.user_id = user_id 

192 

193 def __eq__(self, other): 

194 if not isinstance(other, User): 

195 return NotImplemented 

196 return self.user_id == other.user_id 

197 

198 def __ne__(self, other): 

199 return not self.__eq__(other) 

200 

201 user2 = User(user_id=2) 

202 user3 = User(user_id=3) 

203 

204 @apply_types(context__=context) 

205 async def use( 

206 key1=Context("user1"), 

207 key2=Context("user2", default=user2), 

208 key3=Context("user3", default=user3), 

209 ): 

210 return ( 

211 key1 == User(user_id=1) 

212 and key2 == User(user_id=2) 

213 and key3 == User(user_id=4) 

214 ) 

215 

216 with ( 

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

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

219 ): 

220 assert await use()