are_you_sure

Untitled

Dec 7th, 2021
695
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import unittest
  2. from unittest.mock import patch
  3.  
  4. import warnings
  5. from telegram.utils import request as tg_utils_request
  6. from telegram.utils.request import Request
  7. from telegram.utils.deprecate import TelegramDeprecationWarning
  8. from telegram.ext import Updater
  9. from telegram import User as tg_User
  10. from telegram import Chat as tg_Chat
  11. from ptbtest import ChatGenerator
  12. from ptbtest import Mockbot
  13. from ptbtest import UserGenerator
  14. from ptbtest import CallbackQueryGenerator
  15. from subprocess import run as subprocess_run
  16.  
  17. import config
  18. import keyboards
  19. from top_level_funcs import end_conversation
  20. import conv_handlers
  21.  
  22.  
  23. # # # file postconfig.py  # # #
  24. from telegram import Bot
  25. ...
  26. bot = Bot(token='some_token')
  27. # # # end file postconfig.py  # # #
  28.  
  29.  
  30. """
  31. This is an example to show how the ptbtest suite can be used.
  32. https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot2.py
  33. """
  34.  
  35. warnings.filterwarnings("ignore", category=TelegramDeprecationWarning)
  36.  
  37.  
  38. # noinspection PyPep8Naming
  39. class Mockbot(Mockbot):
  40.  
  41.     def __init__(self, request: Request = None, *args, **kwargs):
  42.         super().__init__(*args, **kwargs)
  43.         self.request = request or tg_utils_request.Request(connect_timeout=3, read_timeout=3, con_pool_size=16)
  44.         self._current_keyboard = None
  45.         self._current_markup = None
  46.         self.end_conversation = end_conversation
  47.  
  48.     @Mockbot.message
  49.     def CopyMessage(
  50.             self,
  51.             chat_id: int,
  52.             from_chat_id: int,
  53.             message_id: int,
  54.             caption: str = None,
  55.             parse_mode: str = None,
  56.             caption_entities: list = None,
  57.             disable_notification: bool = None,
  58.             reply_to_message_id: int = None,
  59.             allow_sending_without_reply: bool = None,
  60.             reply_markup=None,
  61.             timeout: float = None,
  62.             api_kwargs: dict = None,
  63.     ):
  64.         data = {"chat_id": chat_id}
  65.         if caption:
  66.             # data['caption'] = caption
  67.             data['text'] = caption
  68.         if parse_mode:
  69.             data["parse_mode"] = parse_mode
  70.         if caption_entities:
  71.             data["caption_entities"] = caption_entities
  72.  
  73.         return data
  74.  
  75.     @Mockbot.message
  76.     def SendMediaGroup(
  77.             self,
  78.             chat_id,
  79.             media,
  80.             disable_notification=None,
  81.             reply_to_message_id=None,
  82.             timeout=20,
  83.             api_kwargs=None,
  84.             allow_sending_without_reply=None
  85.     ):
  86.         data = {"chat_id": chat_id, "media": media}
  87.  
  88.         return data
  89.  
  90.     @property
  91.     def current_keyboard(self):
  92.         return self.sent_messages[-1]['reply_markup']['keyboard']
  93.  
  94.     @property
  95.     def current_markup(self):
  96.         return self.sent_messages[-1]['reply_markup']
  97.  
  98.     def sent_answer(self, is_answer: str,
  99.                     index: int = -1,
  100.                     key: str = 'text',
  101.                     _lower: bool = True,
  102.                     _strip: bool = True,
  103.                     _startswith: bool = True):
  104.         bot_text = self.sent_messages[index][key]
  105.         if key == 'text':
  106.             if _lower:
  107.                 bot_text = bot_text.lower()
  108.             if _strip:
  109.                 bot_text = bot_text.strip()
  110.             if _startswith:
  111.                 return bot_text.startswith(is_answer)
  112.         return bot_text
  113.  
  114.     send_media_group = SendMediaGroup
  115.     copy_message = CopyMessage
  116.  
  117.  
  118. class User:
  119.  
  120.     def __init__(self, user: tg_User, chat: tg_Chat, bot: Mockbot, user_data: dict, cqg: CallbackQueryGenerator):
  121.         self.user = user
  122.         self.chat = chat
  123.         self.bot = bot
  124.         self.user_data = user_data
  125.         self.mg = self.bot.mg
  126.         self.cqg = cqg
  127.  
  128.     def send_text(self, text: str, parse_mode: str = "HTML", message_id: int = None) -> None:
  129.         update = self.mg.get_message(user=self.user, chat=self.chat, text=text, parse_mode=parse_mode, id=message_id)
  130.         self.bot.insertUpdate(update=update)
  131.  
  132.  
  133. bot = Mockbot()
  134.  
  135.  
  136. @patch("postconfig.bot", bot)
  137. class TestConversation(unittest.TestCase):  # IsolatedAsyncioTestCase - for async
  138.     def setUp(self):  # name method should be exactly "setUp"
  139.         # self.bot = Mockbot()
  140.         self.bot = bot
  141.         self.mg = self.bot.mg
  142.         self.updater = Updater(bot=self.bot)
  143.         self.dispatcher = self.updater.dispatcher
  144.         self.dispatcher.add_handler(conv_handlers.registration)
  145.         self.dispatcher.add_handler(conv_handlers.partner)
  146.         self.dispatcher.add_handler(conv_handlers.create_post)
  147.         self.chat_generator = ChatGenerator()
  148.         self.user_generator = UserGenerator()
  149.         self.cqg = CallbackQueryGenerator()
  150.         self.botmock = patch("postconfig.bot", self.bot)
  151.         self.botmock.return_value = self.bot
  152.         self.addCleanup(self.updater.stop)
  153.  
  154.     @patch("postconfig.bot", bot)
  155.     def test_reg_steps(self, user: User):
  156.         self.botmock = patch("postconfig.bot", self.bot)
  157.         self.botmock.return_value = self.bot
  158.         user.send_text(text="/reg")
  159.         self.assertTrue(self.bot.sent_answer(is_answer='отличное решение!'))
  160.         user.send_text(text="Поехали")
  161.         self.assertTrue(self.bot.sent_answer(is_answer='шаг 1 из 7'))
  162.         user.send_text(text="David")
  163.         self.assertTrue(self.bot.sent_answer(is_answer='шаг 2 из 7'))
  164.         user.send_text(text="Общаться")
  165.         self.assertTrue(self.bot.sent_answer(is_answer='шаг 3 из 7'))
  166.         user.send_text(text="Я парень")
  167.         self.assertTrue(self.bot.sent_answer(is_answer='шаг 4 из 7'))
  168.         user.send_text(text="33")
  169.         self.assertTrue(self.bot.sent_answer(is_answer='шаг 5 из 7'))
  170.         user.send_text(text="Пропустить")
  171.         self.assertTrue(self.bot.sent_answer(is_answer='шаг 6 из 7'))
  172.         user.send_text(text="Готово")
  173.         self.assertTrue(self.bot.sent_answer(is_answer='шаг 7 из 7'))
  174.         user.send_text(text="Я бот из тестов")
  175.         self.assertTrue(self.bot.sent_answer(is_answer='так будет', index=-2))  # -2 because sent 2 messages
  176.         user.send_text(text="Готово")
  177.         self.assertTrue(self.bot.sent_answer(is_answer='поздравляю'))
  178.  
  179.     @patch("postconfig.bot", bot)
  180.     def test_reg_conv(self, user: User):
  181.         # self.updater.start_polling()
  182.         self.botmock = patch("postconfig.bot", self.bot)
  183.         self.botmock.return_value = self.bot
  184.         self.test_reg_steps(user=user)
  185.  
  186.     @patch("postconfig.bot", bot)
  187.     def test_conversations(self):
  188.         self.botmock = patch("postconfig.bot", self.bot)
  189.         self.botmock.return_value = self.bot
  190.         self.updater.start_polling()  # returns update_queue obj
  191.         user = User(user := self.user_generator.get_user(id=177381168),
  192.                     chat=self.chat_generator.get_chat(type="group"),
  193.                     bot=self.bot,
  194.                     user_data=self.dispatcher.user_data[user.id],
  195.                     cqg=self.cqg)
  196.         self.test_reg_conv(user=user)
  197.  
  198.  
  199. if __name__ == '__main__':
  200.     unittest.main()
RAW Paste Data