def test_less_than_one_worker_warning(self, dp, recwarn):
     Dispatcher(dp.bot,
                dp.update_queue,
                job_queue=dp.job_queue,
                workers=0,
                use_context=True)
     assert len(recwarn) == 1
     assert (
         str(recwarn[0].message) ==
         'Asynchronous callbacks can not be processed without at least one worker thread.'
     )
Esempio n. 2
0
    def test_construction_with_bad_persistence(self, caplog, bot):
        class my_per:
            def __init__(self):
                self.store_user_data = False
                self.store_chat_data = False
                self.store_bot_data = False

        with pytest.raises(
                TypeError,
                match=
                'persistence must be based on telegram.ext.BasePersistence'):
            Dispatcher(bot, None, persistence=my_per())
Esempio n. 3
0
    def test_custom_context_init(self, bot):
        cc = ContextTypes(
            context=CustomContext,
            user_data=int,
            chat_data=float,
            bot_data=complex,
        )

        dispatcher = Dispatcher(bot, Queue(), context_types=cc)

        assert isinstance(dispatcher.user_data[1], int)
        assert isinstance(dispatcher.chat_data[1], float)
        assert isinstance(dispatcher.bot_data, complex)
Esempio n. 4
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

            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"]
Esempio n. 5
0
    def test_custom_context_handler_callback(self, bot):
        def callback(_, 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_handler(MessageHandler(Filters.all, callback))

        dispatcher.process_update(self.message_update)
        sleep(0.1)
        assert self.received == (CustomContext, float, complex, int)
 def test_non_context_deprecation(self, dp):
     with pytest.warns(TelegramDeprecationWarning):
         Dispatcher(dp.bot, dp.update_queue, job_queue=dp.job_queue, workers=0,
                    use_context=False)