Esempio n. 1
0
    def test_error_while_saving_chat_data(self, dp, bot):
        increment = []

        class OwnPersistence(BasePersistence):
            def __init__(self):
                super(BasePersistence, self).__init__()
                self.store_user_data = True
                self.store_chat_data = True
                self.store_bot_data = True

            def get_bot_data(self):
                return dict()

            def update_bot_data(self, data):
                raise Exception

            def get_chat_data(self):
                return defaultdict(dict)

            def update_chat_data(self, chat_id, data):
                raise Exception

            def get_user_data(self):
                return defaultdict(dict)

            def update_user_data(self, user_id, data):
                raise Exception

        def start1(b, u):
            pass

        def error(b, u, e):
            increment.append("error")

        # If updating a user_data or chat_data from a persistence object throws an error,
        # the error handler should catch it

        update = Update(1,
                        message=Message(1,
                                        User(1, "Test", False),
                                        None,
                                        Chat(1, "lala"),
                                        text='/start',
                                        entities=[
                                            MessageEntity(
                                                type=MessageEntity.BOT_COMMAND,
                                                offset=0,
                                                length=len('/start'))
                                        ],
                                        bot=bot))
        my_persistence = OwnPersistence()
        dp = Dispatcher(bot, None, persistence=my_persistence)
        dp.add_handler(CommandHandler('start', start1))
        dp.add_error_handler(error)
        dp.process_update(update)
        assert increment == ["error", "error", "error"]
Esempio n. 2
0
    def test_custom_context_error_handler(self, bot):
        def error_handler(_, context):
            self.received = (
                type(context),
                type(context.user_data),
                type(context.chat_data),
                type(context.bot_data),
            )

        dispatcher = Dispatcher(
            bot,
            Queue(),
            context_types=ContextTypes(context=CustomContext,
                                       bot_data=int,
                                       user_data=float,
                                       chat_data=complex),
        )
        dispatcher.add_error_handler(error_handler)
        dispatcher.add_handler(
            MessageHandler(Filters.all, self.callback_raise_error))

        dispatcher.process_update(self.message_update)
        sleep(0.1)
        assert self.received == (CustomContext, float, complex, int)