aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar wookie184 <[email protected]>2024-05-21 12:38:22 +0100
committerGravatar GitHub <[email protected]>2024-05-21 12:38:22 +0100
commit82bbeabe8b72e5945a94a245d48700d0c9965159 (patch)
treef073c922d247d592c76c694383f8e8b64763f96d /tests
parentFix typo (diff)
parentMerge pull request #3005 from python-discord/fix/source-command-for-tags (diff)
Merge branch 'main' into reply-to-eval
Diffstat (limited to 'tests')
-rw-r--r--tests/bot/exts/backend/sync/test_cog.py7
-rw-r--r--tests/bot/exts/backend/test_error_handler.py10
-rw-r--r--tests/bot/exts/moderation/infraction/test_infractions.py20
-rw-r--r--tests/bot/exts/recruitment/talentpool/test_review.py40
4 files changed, 61 insertions, 16 deletions
diff --git a/tests/bot/exts/backend/sync/test_cog.py b/tests/bot/exts/backend/sync/test_cog.py
index bf117b478..6d7356bf2 100644
--- a/tests/bot/exts/backend/sync/test_cog.py
+++ b/tests/bot/exts/backend/sync/test_cog.py
@@ -1,3 +1,4 @@
+import types
import unittest
import unittest.mock
from unittest import mock
@@ -86,8 +87,10 @@ class SyncCogTests(SyncCogTestCase):
mock_create_task.assert_not_called()
else:
mock_create_task.assert_called_once()
- self.assertIsInstance(mock_create_task.call_args[0][0], type(self.cog.sync()))
-
+ create_task_arg = mock_create_task.call_args[0][0]
+ self.assertIsInstance(create_task_arg, types.CoroutineType)
+ self.assertEqual(create_task_arg.__qualname__, self.cog.sync.__qualname__)
+ create_task_arg.close()
async def test_sync_cog_sync_guild(self):
"""Roles and users should be synced only if a guild is successfully retrieved."""
diff --git a/tests/bot/exts/backend/test_error_handler.py b/tests/bot/exts/backend/test_error_handler.py
index dbc62270b..85dc33999 100644
--- a/tests/bot/exts/backend/test_error_handler.py
+++ b/tests/bot/exts/backend/test_error_handler.py
@@ -495,26 +495,26 @@ class IndividualErrorHandlerTests(unittest.IsolatedAsyncioTestCase):
else:
log_mock.debug.assert_called_once()
- @patch("bot.exts.backend.error_handler.push_scope")
+ @patch("bot.exts.backend.error_handler.new_scope")
@patch("bot.exts.backend.error_handler.log")
- async def test_handle_unexpected_error(self, log_mock, push_scope_mock):
+ async def test_handle_unexpected_error(self, log_mock, new_scope_mock):
"""Should `ctx.send` this error, error log this and sent to Sentry."""
for case in (None, MockGuild()):
with self.subTest(guild=case):
self.ctx.reset_mock()
log_mock.reset_mock()
- push_scope_mock.reset_mock()
+ new_scope_mock.reset_mock()
scope_mock = Mock()
# Mock `with push_scope_mock() as scope:`
- push_scope_mock.return_value.__enter__.return_value = scope_mock
+ new_scope_mock.return_value.__enter__.return_value = scope_mock
self.ctx.guild = case
await self.cog.handle_unexpected_error(self.ctx, errors.CommandError())
self.ctx.send.assert_awaited_once()
log_mock.error.assert_called_once()
- push_scope_mock.assert_called_once()
+ new_scope_mock.assert_called_once()
set_tag_calls = [
call("command", self.ctx.command.qualified_name),
diff --git a/tests/bot/exts/moderation/infraction/test_infractions.py b/tests/bot/exts/moderation/infraction/test_infractions.py
index 26ba770dc..f257bec7d 100644
--- a/tests/bot/exts/moderation/infraction/test_infractions.py
+++ b/tests/bot/exts/moderation/infraction/test_infractions.py
@@ -37,7 +37,9 @@ class TruncationTests(unittest.IsolatedAsyncioTestCase):
self.cog.mod_log.ignore = Mock()
self.ctx.guild.ban = AsyncMock()
- await self.cog.apply_ban(self.ctx, self.target, "foo bar" * 3000)
+ infraction_reason = "foo bar" * 3000
+
+ await self.cog.apply_ban(self.ctx, self.target, infraction_reason)
self.cog.apply_infraction.assert_awaited_once_with(
self.ctx, {"foo": "bar", "purge": ""}, self.target, ANY
)
@@ -46,10 +48,14 @@ class TruncationTests(unittest.IsolatedAsyncioTestCase):
await action()
self.ctx.guild.ban.assert_awaited_once_with(
self.target,
- reason=textwrap.shorten("foo bar" * 3000, 512, placeholder="..."),
+ reason=textwrap.shorten(infraction_reason, 512, placeholder="..."),
delete_message_days=0
)
+ # Assert that the reason sent to the database isn't truncated.
+ post_infraction_mock.assert_awaited_once()
+ self.assertEqual(post_infraction_mock.call_args.args[3], infraction_reason)
+
@patch("bot.exts.moderation.infraction._utils.post_infraction")
async def test_apply_kick_reason_truncation(self, post_infraction_mock):
"""Should truncate reason for `Member.kick`."""
@@ -59,14 +65,20 @@ class TruncationTests(unittest.IsolatedAsyncioTestCase):
self.cog.mod_log.ignore = Mock()
self.target.kick = AsyncMock()
- await self.cog.apply_kick(self.ctx, self.target, "foo bar" * 3000)
+ infraction_reason = "foo bar" * 3000
+
+ await self.cog.apply_kick(self.ctx, self.target, infraction_reason)
self.cog.apply_infraction.assert_awaited_once_with(
self.ctx, {"foo": "bar"}, self.target, ANY
)
action = self.cog.apply_infraction.call_args.args[-1]
await action()
- self.target.kick.assert_awaited_once_with(reason=textwrap.shorten("foo bar" * 3000, 512, placeholder="..."))
+ self.target.kick.assert_awaited_once_with(reason=textwrap.shorten(infraction_reason, 512, placeholder="..."))
+
+ # Assert that the reason sent to the database isn't truncated.
+ post_infraction_mock.assert_awaited_once()
+ self.assertEqual(post_infraction_mock.call_args.args[3], infraction_reason)
@patch("bot.exts.moderation.infraction.infractions.constants.Roles.voice_verified", new=123456)
diff --git a/tests/bot/exts/recruitment/talentpool/test_review.py b/tests/bot/exts/recruitment/talentpool/test_review.py
index 25622e91f..8ec384bb2 100644
--- a/tests/bot/exts/recruitment/talentpool/test_review.py
+++ b/tests/bot/exts/recruitment/talentpool/test_review.py
@@ -3,7 +3,7 @@ from datetime import UTC, datetime, timedelta
from unittest.mock import AsyncMock, Mock, patch
from bot.exts.recruitment.talentpool import _review
-from tests.helpers import MockBot, MockMember, MockMessage, MockTextChannel
+from tests.helpers import MockBot, MockMember, MockMessage, MockReaction, MockTextChannel
class AsyncIterator:
@@ -63,8 +63,10 @@ class ReviewerTests(unittest.IsolatedAsyncioTestCase):
"""Tests for the `is_ready_for_review` function."""
too_recent = datetime.now(UTC) - timedelta(hours=1)
not_too_recent = datetime.now(UTC) - timedelta(days=7)
+ ticket_reaction = MockReaction(users=[self.bot_user], emoji="\N{TICKET}")
+
cases = (
- # Only one review, and not too recent, so ready.
+ # Only one active review, and not too recent, so ready.
(
[
MockMessage(author=self.bot_user, content="wookie for Helper!", created_at=not_too_recent),
@@ -75,7 +77,7 @@ class ReviewerTests(unittest.IsolatedAsyncioTestCase):
True,
),
- # Three reviews, so not ready.
+ # Three active reviews, so not ready.
(
[
MockMessage(author=self.bot_user, content="Chrisjl for Helper!", created_at=not_too_recent),
@@ -86,7 +88,7 @@ class ReviewerTests(unittest.IsolatedAsyncioTestCase):
False,
),
- # Only one review, but too recent, so not ready.
+ # Only one active review, but too recent, so not ready.
(
[
MockMessage(author=self.bot_user, content="Chrisjl for Helper!", created_at=too_recent),
@@ -95,7 +97,7 @@ class ReviewerTests(unittest.IsolatedAsyncioTestCase):
False,
),
- # Only two reviews, and not too recent, so ready.
+ # Only two active reviews, and not too recent, so ready.
(
[
MockMessage(author=self.bot_user, content="Not a review", created_at=too_recent),
@@ -107,6 +109,34 @@ class ReviewerTests(unittest.IsolatedAsyncioTestCase):
True,
),
+ # Over the active threshold, but below the total threshold
+ (
+ [
+ MockMessage(
+ author=self.bot_user,
+ content="joe for Helper!",
+ created_at=not_too_recent,
+ reactions=[ticket_reaction]
+ )
+ ] * 6,
+ not_too_recent.timestamp(),
+ True
+ ),
+
+ # Over the total threshold
+ (
+ [
+ MockMessage(
+ author=self.bot_user,
+ content="joe for Helper!",
+ created_at=not_too_recent,
+ reactions=[ticket_reaction]
+ )
+ ] * 11,
+ not_too_recent.timestamp(),
+ False
+ ),
+
# No messages, so ready.
([], None, True),
)