async def test_error_handler_command_invoke_error(self): """Should call `handle_api_error` or `handle_unexpected_error` depending on original error.""" cog = ErrorHandler(self.bot) cog.handle_api_error = AsyncMock() cog.handle_unexpected_error = AsyncMock() test_cases = ( { "args": (self.ctx, errors.CommandInvokeError(ResponseCodeError(AsyncMock()))), "expect_mock_call": cog.handle_api_error }, { "args": (self.ctx, errors.CommandInvokeError(TypeError)), "expect_mock_call": cog.handle_unexpected_error }, { "args": (self.ctx, errors.CommandInvokeError(LockedResourceError("abc", "test"))), "expect_mock_call": "send" }, { "args": (self.ctx, errors.CommandInvokeError(InvalidInfractedUser(self.ctx.author))), "expect_mock_call": "send" } ) for case in test_cases: with self.subTest(args=case["args"], expect_mock_call=case["expect_mock_call"]): self.ctx.send.reset_mock() self.assertIsNone(await cog.on_command_error(*case["args"])) if case["expect_mock_call"] == "send": self.ctx.send.assert_awaited_once() else: case["expect_mock_call"].assert_awaited_once_with( self.ctx, case["args"][1].original )
async def test_error_handler_check_failure(self): """Should await `ErrorHandler.handle_check_failure` when error is `CheckFailure`.""" self.ctx.reset_mock() cog = ErrorHandler(self.bot) cog.handle_check_failure = AsyncMock() error = errors.CheckFailure() self.assertIsNone(await cog.on_command_error(self.ctx, error)) cog.handle_check_failure.assert_awaited_once_with(self.ctx, error)
async def test_error_handler_user_input_error(self): """Should await `ErrorHandler.handle_user_input_error` when error is `UserInputError`.""" self.ctx.reset_mock() cog = ErrorHandler(self.bot) cog.handle_user_input_error = AsyncMock() error = errors.UserInputError() self.assertIsNone(await cog.on_command_error(self.ctx, error)) cog.handle_user_input_error.assert_awaited_once_with(self.ctx, error)
async def test_error_handler_two_other_errors(self): """Should call `handle_unexpected_error` if error is `MaxConcurrencyReached` or `ExtensionError`.""" cog = ErrorHandler(self.bot) cog.handle_unexpected_error = AsyncMock() errs = (errors.MaxConcurrencyReached(1, MagicMock()), errors.ExtensionError(name="foo")) for err in errs: with self.subTest(error=err): cog.handle_unexpected_error.reset_mock() self.assertIsNone(await cog.on_command_error(self.ctx, err)) cog.handle_unexpected_error.assert_awaited_once_with( self.ctx, err)
async def test_error_handler_command_not_found_error_invoked_by_handler(self): """Should do nothing when error is `CommandNotFound` and have attribute `invoked_from_error_handler`.""" ctx = MockContext(bot=self.bot, invoked_from_error_handler=True) cog = ErrorHandler(self.bot) cog.try_silence = AsyncMock() cog.try_get_tag = AsyncMock() error = errors.CommandNotFound() self.assertIsNone(await cog.on_command_error(ctx, error)) cog.try_silence.assert_not_awaited() cog.try_get_tag.assert_not_awaited() self.ctx.send.assert_not_awaited()
async def test_error_handler_command_on_cooldown(self): """Should send error with `ctx.send` when error is `CommandOnCooldown`.""" self.ctx.reset_mock() cog = ErrorHandler(self.bot) error = errors.CommandOnCooldown(10, 9) self.assertIsNone(await cog.on_command_error(self.ctx, error)) self.ctx.send.assert_awaited_once_with(error)
async def test_error_handler_already_handled(self): """Should not do anything when error is already handled by local error handler.""" self.ctx.reset_mock() cog = ErrorHandler(self.bot) error = errors.CommandError() error.handled = "foo" self.assertIsNone(await cog.on_command_error(self.ctx, error)) self.ctx.send.assert_not_awaited()
async def test_get_help_command_no_command_specified(self): """Should return coroutine of help command.""" self.ctx.command = None result = ErrorHandler.get_help_command(self.ctx) expected = self.ctx.send_help() self.assertEqual(result.__qualname__, expected.__qualname__) self.assertEqual(result.cr_frame.f_locals, expected.cr_frame.f_locals) # Await coroutines to avoid warnings await result await expected
async def test_error_handler_conversion_error(self): """Should call `handle_api_error` or `handle_unexpected_error` depending on original error.""" cog = ErrorHandler(self.bot) cog.handle_api_error = AsyncMock() cog.handle_unexpected_error = AsyncMock() cases = ( { "error": errors.ConversionError(AsyncMock(), ResponseCodeError(AsyncMock())), "mock_function_to_call": cog.handle_api_error }, { "error": errors.ConversionError(AsyncMock(), TypeError), "mock_function_to_call": cog.handle_unexpected_error } ) for case in cases: with self.subTest(**case): self.assertIsNone(await cog.on_command_error(self.ctx, case["error"])) case["mock_function_to_call"].assert_awaited_once_with(self.ctx, case["error"].original)
async def test_error_handler_command_not_found_error_not_invoked_by_handler(self): """Should try first (un)silence channel, when fail, try to get tag.""" error = errors.CommandNotFound() test_cases = ( { "try_silence_return": True, "called_try_get_tag": False }, { "try_silence_return": False, "called_try_get_tag": False }, { "try_silence_return": False, "called_try_get_tag": True } ) cog = ErrorHandler(self.bot) cog.try_silence = AsyncMock() cog.try_get_tag = AsyncMock() for case in test_cases: with self.subTest(try_silence_return=case["try_silence_return"], try_get_tag=case["called_try_get_tag"]): self.ctx.reset_mock() cog.try_silence.reset_mock(return_value=True) cog.try_get_tag.reset_mock() cog.try_silence.return_value = case["try_silence_return"] self.ctx.channel.id = 1234 self.assertIsNone(await cog.on_command_error(self.ctx, error)) if case["try_silence_return"]: cog.try_get_tag.assert_not_awaited() cog.try_silence.assert_awaited_once() else: cog.try_silence.assert_awaited_once() cog.try_get_tag.assert_awaited_once() self.ctx.send.assert_not_awaited()
def setUp(self): self.bot = MockBot() self.ctx = MockContext(bot=self.bot) self.cog = ErrorHandler(self.bot)
def setUp(self): self.bot = MockBot() self.ctx = MockContext() self.tag = Tags(self.bot) self.cog = ErrorHandler(self.bot) self.bot.get_command.return_value = self.tag.get_command
def setUp(self): self.bot = MockBot() self.silence = Silence(self.bot) self.bot.get_command.return_value = self.silence.silence self.ctx = MockContext(bot=self.bot) self.cog = ErrorHandler(self.bot)
async def test_error_handler_other_errors(self, log_mock): """Should `log.debug` other errors.""" cog = ErrorHandler(self.bot) error = errors.DisabledCommand() # Use this just as a other error self.assertIsNone(await cog.on_command_error(self.ctx, error)) log_mock.debug.assert_called_once()