1# ruff: noqa: PT009
2import unittest
3
4from telegram.ext import CommandHandler, MessageHandler, Updater, filters
5
6from ptbtest import ChatGenerator, MessageGenerator, Mockbot, UserGenerator
7
8"""
9This is an example to show how the ptbtest suite can be used.
10This example follows the echobot example at:
11https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot.py
12
13"""
14
15
16class TestEchobot(unittest.TestCase):
17 def setUp(self):
18 # For use within the tests we nee some stuff. Starting with a Mockbot
19 self.bot = Mockbot()
20 # Some generators for users and chats
21 self.ug = UserGenerator()
22 self.cg = ChatGenerator()
23 # And a Messagegenerator and updater (for use with the bot.)
24 self.mg = MessageGenerator(self.bot)
25 self.updater = Updater(bot=self.bot)
26
27 def test_help(self):
28 # this tests the help handler. So first insert the handler
29 def help_message(update):
30 update.message.reply_text("Help!")
31
32 # Then register the handler with he updater's dispatcher and start polling
33 self.updater.dispatcher.add_handler(CommandHandler("help", help_message))
34 self.updater.start_polling()
35 # We want to simulate a message. Since we don't care wich user sends it we let the MessageGenerator
36 # create random ones
37 update = self.mg.get_message(text="/help")
38 # We insert the update with the bot so the updater can retrieve it.
39 self.bot.insert_update(update)
40 # sent_messages is the list with calls to the bot's outbound actions. Since we hope the message we inserted
41 # only triggered one sendMessage action it's length should be 1.
42 self.assertEqual(len(self.bot.sent_messages), 1)
43 sent = self.bot.sent_messages[0]
44 self.assertEqual(sent["method"], "sendMessage")
45 self.assertEqual(sent["text"], "Help!")
46 # Always stop the updater at the end of a testcase so it won't hang.
47 self.updater.stop()
48
49 def test_start(self):
50 def start(update):
51 update.message.reply_text("Hi!")
52
53 self.updater.dispatcher.add_handler(CommandHandler("start", start))
54 self.updater.start_polling()
55 # Here you can see how we would handle having our own user and chat
56 user = self.ug.get_user(first_name="Test", last_name="The Bot")
57 chat = self.cg.get_chat(user=user)
58 update = self.mg.get_message(user=user, chat=chat, text="/start")
59 self.bot.insert_update(update)
60 self.assertEqual(len(self.bot.sent_messages), 1)
61 sent = self.bot.sent_messages[0]
62 self.assertEqual(sent["method"], "sendMessage")
63 self.assertEqual(sent["text"], "Hi!")
64 self.updater.stop()
65
66 def test_echo(self):
67 def echo(update):
68 update.message.reply_text(update.message.text)
69
70 self.updater.dispatcher.add_handler(MessageHandler(filters.text, echo))
71 self.updater.start_polling()
72 update = self.mg.get_message(text="first message")
73 update2 = self.mg.get_message(text="second message")
74 self.bot.insert_update(update)
75 self.bot.insert_update(update2)
76 self.assertEqual(len(self.bot.sent_messages), 2)
77 sent = self.bot.sent_messages
78 self.assertEqual(sent[0]["method"], "sendMessage")
79 self.assertEqual(sent[0]["text"], "first message")
80 self.assertEqual(sent[1]["text"], "second message")
81 self.updater.stop()
82
83
84if __name__ == "__main__":
85 unittest.main()