|
| 1 | +import asyncio |
1 | 2 | import datetime |
2 | 3 | from unittest import mock |
3 | 4 |
|
@@ -168,40 +169,41 @@ async def test_reschedule_slowmodes(self) -> None: |
168 | 169 | self.cog._reschedule.assert_called() |
169 | 170 | self.cog.scheduler.schedule_at.assert_not_called() |
170 | 171 |
|
| 172 | + async def test_reschedule_upon_reload(self) -> None: |
| 173 | + """ Check that method `_reschedule` is called upon cog reload""" |
| 174 | + self.cog._reschedule = mock.AsyncMock(wraps=self.cog._reschedule) |
| 175 | + await self.cog.cog_unload() |
| 176 | + await self.cog.cog_load() |
| 177 | + |
| 178 | + self.cog._reschedule.assert_called() |
171 | 179 |
|
172 | 180 | @mock.patch("bot.exts.moderation.slowmode.datetime", wraps=datetime.datetime) |
173 | 181 | async def test_reschedules_slowmodes(self, mock_datetime) -> None: |
174 | 182 | """Slowmodes are loaded from cache at cog reload and scheduled to be reverted.""" |
175 | 183 | mock_datetime.now.return_value = datetime.datetime(2025, 6, 2, 12, 0, 0, tzinfo=datetime.UTC) |
176 | 184 | mock_now = datetime.datetime(2025, 6, 2, 12, 0, 0, tzinfo=datetime.UTC) |
177 | | - self.cog._reschedule = mock.AsyncMock(wraps=self.cog._reschedule) |
178 | 185 |
|
179 | | - channels = [] |
| 186 | + channels = {} |
180 | 187 | slowmodes = ( |
181 | 188 | (123, (mock_now - datetime.timedelta(10)).timestamp(), 2), # expiration in the past |
182 | | - (456, (mock_now + datetime.timedelta(10)).timestamp(), 4), # expiration in the future |
| 189 | + (456, (mock_now + datetime.timedelta(20)).timestamp(), 4), # expiration in the future |
183 | 190 | ) |
184 | 191 |
|
185 | 192 | for channel_id, expiration_datetime, delay in slowmodes: |
186 | 193 | channel = MockTextChannel(slowmode_delay=delay, id=channel_id) |
187 | | - channels.append(channel) |
188 | | - |
| 194 | + channels[channel_id] = channel |
189 | 195 | await self.cog.slowmode_expiration_cache.set(channel_id, expiration_datetime) |
190 | 196 | await self.cog.original_slowmode_cache.set(channel_id, delay) |
191 | 197 |
|
| 198 | + self.bot.get_channel = mock.MagicMock(side_effect=lambda channel_id: channels.get(channel_id)) |
192 | 199 | await self.cog.cog_unload() |
193 | 200 | await self.cog.cog_load() |
| 201 | + for channel_id in channels: |
| 202 | + self.assertIn(channel_id, self.cog.scheduler) |
194 | 203 |
|
195 | | - # check that _reschedule function was called upon cog reload. |
196 | | - self.cog._reschedule.assert_called() |
197 | | - |
198 | | - # check that a task was created for every cached slowmode. |
199 | | - for channel in channels: |
200 | | - self.assertIn(channel.id, self.cog.scheduler) |
201 | | - |
202 | | - # check that one channel with slowmode expiration in the past was edited immediately. |
203 | | - channels[0].edit.assert_awaited_once_with(slowmode_delay=channels[0].slowmode_delay) |
204 | | - channels[1].edit.assert_not_called() |
| 204 | + await asyncio.sleep(1) # give scheduled task time to execute |
| 205 | + channels[123].edit.assert_awaited_once_with(slowmode_delay=channels[123].slowmode_delay) |
| 206 | + channels[456].edit.assert_not_called() |
205 | 207 |
|
206 | 208 | @mock.patch("bot.exts.moderation.slowmode.has_any_role") |
207 | 209 | @mock.patch("bot.exts.moderation.slowmode.MODERATION_ROLES", new=(1, 2, 3)) |
|
0 commit comments