diff options
author | 2024-05-23 16:59:33 +0100 | |
---|---|---|
committer | 2024-05-23 16:59:33 +0100 | |
commit | 09cd19394dbfcb135261f6c49278e0e4fc4fe3a7 (patch) | |
tree | 98a4a5cf1b02886783179282a041d74f504c7da4 /tests | |
parent | Adjust truncation for 11 lines (diff) | |
parent | Support events that span from one year to another (#3065) (diff) |
Merge branch 'main' into vivek/fix-eval-truncation
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bot/exts/backend/test_error_handler.py | 10 | ||||
-rw-r--r-- | tests/bot/exts/recruitment/talentpool/test_review.py | 40 | ||||
-rw-r--r-- | tests/bot/exts/utils/snekbox/test_snekbox.py | 15 | ||||
-rw-r--r-- | tests/helpers.py | 20 |
4 files changed, 56 insertions, 29 deletions
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/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), ) diff --git a/tests/bot/exts/utils/snekbox/test_snekbox.py b/tests/bot/exts/utils/snekbox/test_snekbox.py index e13ee3c71..3595d9a67 100644 --- a/tests/bot/exts/utils/snekbox/test_snekbox.py +++ b/tests/bot/exts/utils/snekbox/test_snekbox.py @@ -292,7 +292,6 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): async def test_send_job(self): """Test the send_job function.""" ctx = MockContext() - ctx.message = MockMessage() ctx.send = AsyncMock() ctx.author = MockUser(mention="@LemonLemonishBeard#0042") @@ -311,7 +310,7 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): ctx.send.assert_called_once() self.assertEqual( ctx.send.call_args.args[0], - "@LemonLemonishBeard#0042 :warning: Your 3.12 eval job has completed " + ":warning: Your 3.12 eval job has completed " "with return code 0.\n\n```\n[No output]\n```" ) allowed_mentions = ctx.send.call_args.kwargs["allowed_mentions"] @@ -325,9 +324,7 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): async def test_send_job_with_paste_link(self): """Test the send_job function with a too long output that generate a paste link.""" ctx = MockContext() - ctx.message = MockMessage() ctx.send = AsyncMock() - ctx.author.mention = "@LemonLemonishBeard#0042" eval_result = EvalResult("Way too long beard", 0) self.cog.post_job = AsyncMock(return_value=eval_result) @@ -343,7 +340,7 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): ctx.send.assert_called_once() self.assertEqual( ctx.send.call_args.args[0], - "@LemonLemonishBeard#0042 :white_check_mark: Your 3.12 eval job " + ":white_check_mark: Your 3.12 eval job " "has completed with return code 0." "\n\n```\nWay too long beard\n```\nFull output: lookatmybeard.com" ) @@ -354,9 +351,7 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): async def test_send_job_with_non_zero_eval(self): """Test the send_job function with a code returning a non-zero code.""" ctx = MockContext() - ctx.message = MockMessage() ctx.send = AsyncMock() - ctx.author.mention = "@LemonLemonishBeard#0042" eval_result = EvalResult("ERROR", 127) self.cog.post_job = AsyncMock(return_value=eval_result) @@ -372,7 +367,7 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): ctx.send.assert_called_once() self.assertEqual( ctx.send.call_args.args[0], - "@LemonLemonishBeard#0042 :x: Your 3.12 eval job has completed with return code 127." + ":x: Your 3.12 eval job has completed with return code 127." "\n\n```\nERROR\n```" ) @@ -382,9 +377,7 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): async def test_send_job_with_disallowed_file_ext(self): """Test send_job with disallowed file extensions.""" ctx = MockContext() - ctx.message = MockMessage() ctx.send = AsyncMock() - ctx.author.mention = "@user#7700" files = [ FileAttachment("test.disallowed2", b"test"), @@ -407,7 +400,7 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): ctx.send.assert_called_once() res = ctx.send.call_args.args[0] self.assertTrue( - res.startswith("@user#7700 :white_check_mark: Your 3.12 eval job has completed with return code 0.") + res.startswith(":white_check_mark: Your 3.12 eval job has completed with return code 0.") ) self.assertIn("Files with disallowed extensions can't be uploaded: **.disallowed, .disallowed2, ...**", res) diff --git a/tests/helpers.py b/tests/helpers.py index 580848c25..c51a82a9d 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -500,10 +500,12 @@ class MockContext(CustomMockMixin, unittest.mock.MagicMock): super().__init__(**kwargs) self.me = kwargs.get("me", MockMember()) self.bot = kwargs.get("bot", MockBot()) - self.guild = kwargs.get("guild", MockGuild()) - self.author = kwargs.get("author", MockMember()) - self.channel = kwargs.get("channel", MockTextChannel()) - self.message = kwargs.get("message", MockMessage()) + + self.message = kwargs.get("message", MockMessage(guild=self.guild)) + self.author = kwargs.get("author", self.message.author) + self.channel = kwargs.get("channel", self.message.channel) + self.guild = kwargs.get("guild", self.channel.guild) + self.invoked_from_error_handler = kwargs.get("invoked_from_error_handler", False) @@ -519,10 +521,12 @@ class MockInteraction(CustomMockMixin, unittest.mock.MagicMock): super().__init__(**kwargs) self.me = kwargs.get("me", MockMember()) self.client = kwargs.get("client", MockBot()) - self.guild = kwargs.get("guild", MockGuild()) - self.user = kwargs.get("user", MockMember()) - self.channel = kwargs.get("channel", MockTextChannel()) - self.message = kwargs.get("message", MockMessage()) + + self.message = kwargs.get("message", MockMessage(guild=self.guild)) + self.user = kwargs.get("user", self.message.author) + self.channel = kwargs.get("channel", self.message.channel) + self.guild = kwargs.get("guild", self.channel.guild) + self.invoked_from_error_handler = kwargs.get("invoked_from_error_handler", False) |