diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/bot/exts/backend/test_error_handler.py | 20 | ||||
| -rw-r--r-- | tests/helpers.py | 31 | 
2 files changed, 40 insertions, 11 deletions
diff --git a/tests/bot/exts/backend/test_error_handler.py b/tests/bot/exts/backend/test_error_handler.py index adb0252a5..0ba2fcf11 100644 --- a/tests/bot/exts/backend/test_error_handler.py +++ b/tests/bot/exts/backend/test_error_handler.py @@ -47,7 +47,7 @@ class ErrorHandlerTests(unittest.IsolatedAsyncioTestCase):          )          self.cog.try_silence = AsyncMock()          self.cog.try_get_tag = AsyncMock() -        self.cog.try_run_eval = AsyncMock(return_value=False) +        self.cog.try_run_fixed_codeblock = AsyncMock(return_value=False)          for case in test_cases:              with self.subTest(try_silence_return=case["try_silence_return"], try_get_tag=case["called_try_get_tag"]): @@ -75,7 +75,7 @@ class ErrorHandlerTests(unittest.IsolatedAsyncioTestCase):          self.cog.try_silence = AsyncMock()          self.cog.try_get_tag = AsyncMock() -        self.cog.try_run_eval = AsyncMock() +        self.cog.try_run_fixed_codeblock = AsyncMock()          error = errors.CommandNotFound() @@ -83,7 +83,7 @@ class ErrorHandlerTests(unittest.IsolatedAsyncioTestCase):          self.cog.try_silence.assert_not_awaited()          self.cog.try_get_tag.assert_not_awaited() -        self.cog.try_run_eval.assert_not_awaited() +        self.cog.try_run_fixed_codeblock.assert_not_awaited()          self.ctx.send.assert_not_awaited()      async def test_error_handler_user_input_error(self): @@ -334,13 +334,13 @@ class TryGetTagTests(unittest.IsolatedAsyncioTestCase):          self.ctx = MockContext()          self.tag = Tags(self.bot)          self.cog = error_handler.ErrorHandler(self.bot) -        self.bot.get_command.return_value = self.tag.get_command +        self.bot.get_cog.return_value = self.tag      async def test_try_get_tag_get_command(self):          """Should call `Bot.get_command` with `tags get` argument.""" -        self.bot.get_command.reset_mock() +        self.bot.get_cog.reset_mock()          await self.cog.try_get_tag(self.ctx) -        self.bot.get_command.assert_called_once_with("tags get") +        self.bot.get_cog.assert_called_once_with("Tags")      async def test_try_get_tag_invoked_from_error_handler(self):          """`self.ctx` should have `invoked_from_error_handler` `True`.""" @@ -350,14 +350,14 @@ class TryGetTagTests(unittest.IsolatedAsyncioTestCase):      async def test_try_get_tag_no_permissions(self):          """Test how to handle checks failing.""" -        self.tag.get_command.can_run = AsyncMock(return_value=False) +        self.bot.can_run = AsyncMock(return_value=False)          self.ctx.invoked_with = "foo"          self.assertIsNone(await self.cog.try_get_tag(self.ctx))      async def test_try_get_tag_command_error(self):          """Should call `on_command_error` when `CommandError` raised."""          err = errors.CommandError() -        self.tag.get_command.can_run = AsyncMock(side_effect=err) +        self.bot.can_run = AsyncMock(side_effect=err)          self.cog.on_command_error = AsyncMock()          self.assertIsNone(await self.cog.try_get_tag(self.ctx))          self.cog.on_command_error.assert_awaited_once_with(self.ctx, err) @@ -365,7 +365,7 @@ class TryGetTagTests(unittest.IsolatedAsyncioTestCase):      async def test_dont_call_suggestion_tag_sent(self):          """Should never call command suggestion if tag is already sent."""          self.ctx.message = MagicMock(content="foo") -        self.ctx.invoke = AsyncMock(return_value=True) +        self.tag.get_command_ctx = AsyncMock(return_value=True)          self.cog.send_command_suggestion = AsyncMock()          await self.cog.try_get_tag(self.ctx) @@ -385,7 +385,7 @@ class TryGetTagTests(unittest.IsolatedAsyncioTestCase):      async def test_call_suggestion(self):          """Should call command suggestion if user is not a mod."""          self.ctx.invoked_with = "foo" -        self.ctx.invoke = AsyncMock(return_value=False) +        self.tag.get_command_ctx = AsyncMock(return_value=False)          self.cog.send_command_suggestion = AsyncMock()          await self.cog.try_get_tag(self.ctx) diff --git a/tests/helpers.py b/tests/helpers.py index 4b980ac21..1a71f210a 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -222,7 +222,7 @@ class MockRole(CustomMockMixin, unittest.mock.Mock, ColourMixin, HashableMixin):  # Create a Member instance to get a realistic Mock of `discord.Member` -member_data = {'user': 'lemon', 'roles': [1]} +member_data = {'user': 'lemon', 'roles': [1], 'flags': 2}  state_mock = unittest.mock.MagicMock()  member_instance = discord.Member(data=member_data, guild=guild_instance, state=state_mock) @@ -479,6 +479,25 @@ class MockContext(CustomMockMixin, unittest.mock.MagicMock):          self.invoked_from_error_handler = kwargs.get('invoked_from_error_handler', False) +class MockInteraction(CustomMockMixin, unittest.mock.MagicMock): +    """ +    A MagicMock subclass to mock Interaction objects. + +    Instances of this class will follow the specifications of `discord.Interaction` +    instances. For more information, see the `MockGuild` docstring. +    """ + +    def __init__(self, **kwargs) -> None: +        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.invoked_from_error_handler = kwargs.get('invoked_from_error_handler', False) + +  attachment_instance = discord.Attachment(data=unittest.mock.MagicMock(id=1), state=unittest.mock.MagicMock()) @@ -530,6 +549,16 @@ class MockMessage(CustomMockMixin, unittest.mock.MagicMock):          self.channel = kwargs.get('channel', MockTextChannel()) +class MockInteractionMessage(MockMessage): +    """ +    A MagicMock subclass to mock InteractionMessage objects. + +    Instances of this class will follow the specifications of `discord.InteractionMessage` instances. For more +    information, see the `MockGuild` docstring. +    """ +    pass + +  emoji_data = {'require_colons': True, 'managed': True, 'id': 1, 'name': 'hyperlemon'}  emoji_instance = discord.Emoji(guild=MockGuild(), state=unittest.mock.MagicMock(), data=emoji_data)  |