text: from copy import deepcopy
  from unittest import TestCase
  from unittest.mock import patch, Mock
  
  from pony.orm import db_session, rollback
  from vk_api.bot_longpoll import VkBotMessageEvent
  
  
  from bot import Bot
  from chatbot import settings
  from chatbot.generate_ticket import generate_ticket
  
  
  def isolate_db(test_func):
      def wrapper(*args, **kwargs):
          with db_session:
              test_func(*args, **kwargs)
              rollback()
      return wrapper
  
  
  class Test1(TestCase):
      RAW_EVENT = {
          'type': 'message_new',
          'object': {'date': 1613066709, 'from_id': 411756322, 'id': 98, 'out': 0, 'peer_id': 411756322,
                     'text': 'hi bot', 'conversation_message_id': 83, 'fwd_messages': [], 'important': False,
                     'random_id': 0, 'attachments': [], 'is_hidden': False},
          'group_id': 202152694, 'event_id': '7546c9ba003b5f66ad030219ce15db33df7ad170'}
  
      def test_ok(self):
          count = 5
          obj = {'a': 1}
          events = [obj] * count  # [obj, obj, ...]
          long_poller_mock = Mock(return_value=events)
          long_poller_listen_mock = Mock()
          long_poller_listen_mock.listen = long_poller_mock
  
          with patch('bot.VkApi'):
              with patch('bot.VkBotLongPoll', return_value=long_poller_listen_mock):
                  bot = Bot('', '')
                  bot.on_event = Mock()
                  bot.send_image = Mock()
                  bot.run()
  
                  bot.on_event.assert_called()
                  bot.on_event.assert_any_call(obj)
                  assert bot.on_event.call_count == count
  
      INPUTS = [
          'Hi',
          'when',
          'where?',
          'register me',
          'Alex',
          'My address is email@email',
          'email@email.ru',
      ]
      EXPECTED_OUTPUTS = [
          settings.DEFAULT_ANSWER,
          settings.INTENTS[0]['answer'],
          settings.INTENTS[1]['answer'],
          settings.SCENARIOS['registration']['steps']['step1']['text'],
          settings.SCENARIOS['registration']['steps']['step2']['text'],
          settings.SCENARIOS['registration']['steps']['step2']['failure_text'],
          settings.SCENARIOS['registration']['steps']['step3']['text'].format(name='Alex', email='email@email.ru'),
      ]
  
      # @isolate_db
      def test_run_ok(self):
          send_mock = Mock()
          api_mock = Mock()
          api_mock.messages.send = send_mock
  
          events = []
          for input_text in self.INPUTS:
              event = deepcopy(self.RAW_EVENT)
              event['object']['text'] = input_text
              events.append(VkBotMessageEvent(event))
  
          long_poller_mock = Mock()
          long_poller_mock.listen = Mock(return_value=events)
  
          with patch('bot.VkBotLongPoll', return_value=long_poller_mock):
              bot = Bot('', '')
              bot.api = api_mock
              bot.send_image = Mock()
              bot.run()
  
          assert send_mock.call_count == len(self.INPUTS)
  
          real_outputs = []
          for call in send_mock.call_args_list:
              args, kwargs = call
              real_outputs.append(kwargs['message'])
          assert real_outputs == self.EXPECTED_OUTPUTS
  
      def test_image_generation(self):
          with open('100.jpeg', 'rb') as avatar_file:
              avatar_mock = Mock()
              avatar_mock.content = avatar_file.read()
  
          with patch('requests.get', return_value=avatar_mock):
              ticket_file = generate_ticket('Valerii', 'email@email.ru')
  
          with open('ticket_example.png', 'rb') as expected:
              expected_bytes = expected.read()
  
          assert ticket_file.read() == expected_bytes
  