test_inlinekeyboard.pyΒΆ

 1# ruff: noqa: PT009
 2import unittest
 3
 4from telegram import InlineKeyboardButton, InlineKeyboardMarkup
 5from telegram.ext import CallbackQueryHandler, Updater
 6
 7from ptbtest import CallbackQueryGenerator, ChatGenerator, MessageGenerator, Mockbot
 8
 9"""
10This is an example to show how the ptbtest suite can be used.
11This example follows the timerbot example at:
12https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py
13We will skip the start and help callbacks and focus on the callback query.
14
15"""
16
17
18class TestInlineKeyboard(unittest.TestCase):
19    def setUp(self):
20        # For use within the tests we nee some stuff. Starting with a Mockbot
21        self.bot = Mockbot()
22        # Some generators for users and chats
23        self.cg = ChatGenerator()
24        # And a Messagegenerator, CallbackQueryGenerator and updater (for use with the bot.)
25        self.mg = MessageGenerator(self.bot)
26        self.cqg = CallbackQueryGenerator(self.bot)
27        self.updater = Updater(bot=self.bot)
28
29    def test_callback(self):
30        # first insert the callbackhandler, register it and start polling
31        def button(bot, update):
32            query = update.callback_query
33
34            bot.edit_message_text(
35                text=f"Selected option: {query.data}",
36                chat_id=query.message.chat_id,
37                message_id=query.message.message_id,
38            )
39
40        dp = self.updater.dispatcher
41        dp.add_handler(CallbackQueryHandler(button))
42        self.updater.start_polling()
43
44        # the start callback in this example generates a message that will be edited, so let's mimick that message
45        # for future reference
46        keyboard = [
47            [InlineKeyboardButton("Option 1", callback_data="1"), InlineKeyboardButton("Option 2", callback_data="2")],
48            [InlineKeyboardButton("Option 3", callback_data="3")],
49        ]
50
51        reply_markup = InlineKeyboardMarkup(keyboard)
52        chat = self.cg.get_chat()
53        start_message = self.bot.sendMessage(chat_id=chat.id, text="Please choose:", reply_markup=reply_markup)
54
55        # now let's create some callback query's to send
56        u1 = self.cqg.get_callback_query(message=start_message, data="1")
57        u2 = self.cqg.get_callback_query(message=start_message, data="2")
58        u3 = self.cqg.get_callback_query(message=start_message, data="3")
59
60        # And test them one by one
61        self.bot.insert_update(u1)
62        data = self.bot.sent_messages[-1]
63        self.assertEqual(data["text"], "Selected option: 1")
64        self.assertEqual(data["chat_id"], start_message.chat.id)
65        self.assertEqual(data["message_id"], start_message.message_id)
66        self.bot.insert_update(u2)
67        data = self.bot.sent_messages[-1]
68        self.assertEqual(data["text"], "Selected option: 2")
69        self.assertEqual(data["chat_id"], start_message.chat.id)
70        self.assertEqual(data["message_id"], start_message.message_id)
71        self.bot.insert_update(u3)
72        data = self.bot.sent_messages[-1]
73        self.assertEqual(data["text"], "Selected option: 3")
74        self.assertEqual(data["chat_id"], start_message.chat.id)
75        self.assertEqual(data["message_id"], start_message.message_id)
76
77        # stop polling
78        self.updater.stop()
79
80
81if __name__ == "__main__":
82    unittest.main()