Пример #1
0
    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
                    )
Пример #2
0
    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)
Пример #3
0
    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)