Skip to content

Commit 875c6d2

Browse files
committed
Fix bug in test causing channel edit to not be recognised
1 parent dd444fe commit 875c6d2

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

tests/bot/exts/moderation/test_slowmode.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import datetime
23
from unittest import mock
34

@@ -168,40 +169,41 @@ async def test_reschedule_slowmodes(self) -> None:
168169
self.cog._reschedule.assert_called()
169170
self.cog.scheduler.schedule_at.assert_not_called()
170171

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()
171179

172180
@mock.patch("bot.exts.moderation.slowmode.datetime", wraps=datetime.datetime)
173181
async def test_reschedules_slowmodes(self, mock_datetime) -> None:
174182
"""Slowmodes are loaded from cache at cog reload and scheduled to be reverted."""
175183
mock_datetime.now.return_value = datetime.datetime(2025, 6, 2, 12, 0, 0, tzinfo=datetime.UTC)
176184
mock_now = datetime.datetime(2025, 6, 2, 12, 0, 0, tzinfo=datetime.UTC)
177-
self.cog._reschedule = mock.AsyncMock(wraps=self.cog._reschedule)
178185

179-
channels = []
186+
channels = {}
180187
slowmodes = (
181188
(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
183190
)
184191

185192
for channel_id, expiration_datetime, delay in slowmodes:
186193
channel = MockTextChannel(slowmode_delay=delay, id=channel_id)
187-
channels.append(channel)
188-
194+
channels[channel_id] = channel
189195
await self.cog.slowmode_expiration_cache.set(channel_id, expiration_datetime)
190196
await self.cog.original_slowmode_cache.set(channel_id, delay)
191197

198+
self.bot.get_channel = mock.MagicMock(side_effect=lambda channel_id: channels.get(channel_id))
192199
await self.cog.cog_unload()
193200
await self.cog.cog_load()
201+
for channel_id in channels:
202+
self.assertIn(channel_id, self.cog.scheduler)
194203

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()
205207

206208
@mock.patch("bot.exts.moderation.slowmode.has_any_role")
207209
@mock.patch("bot.exts.moderation.slowmode.MODERATION_ROLES", new=(1, 2, 3))

0 commit comments

Comments
 (0)