def test_on_event(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']['message']['text'] = input_text events.append(VkBotMessageEvent(event)) long_poller_mock = Mock() long_poller_mock.listen = Mock(return_value=events) with patch('vk_bot.VkBotLongPoll', return_value=long_poller_mock): bot = VkBot('', '') 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_run(self): count = 5 events = [{}] * count long_poller_mock = Mock(return_value=events) long_poller_listen_mock = Mock() long_poller_listen_mock.listen = long_poller_mock with patch('vk_bot.vk_api.VkApi'): with patch('vk_bot.VkBotLongPoll', return_value=long_poller_listen_mock): bot = VkBot('', '') bot.on_event = Mock() bot.send_image = Mock() bot.run() bot.on_event.assert_called() bot.on_event.assert_any_call({}) assert bot.on_event.call_count == count
import sys import os.path sys.path.append(os.path.join(os.path.dirname(__file__), '..')) DEBUG = False if __name__ == '__main__': from vk_bot import VkBot import subprocess bot = VkBot() migrations = subprocess.Popen([sys.executable, "models.py"]) sender = subprocess.Popen([sys.executable, "sender.py"]) bot.run() sender.kill()