|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +# |
| 4 | +# A library that provides a Python interface to the Telegram Bot API |
| 5 | +# Copyright (C) 2015-2016 |
| 6 | +# Leandro Toledo de Souza <devs@python-telegram-bot.org> |
| 7 | +# |
| 8 | +# This program is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation, either version 3 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with this program. If not, see [http://www.gnu.org/licenses/]. |
| 20 | +""" |
| 21 | +This module contains a object that represents Tests for ConversationHandler |
| 22 | +""" |
| 23 | +import logging |
| 24 | +import sys |
| 25 | +from time import sleep |
| 26 | + |
| 27 | + |
| 28 | +if sys.version_info[0:2] == (2, 6): |
| 29 | + import unittest2 as unittest |
| 30 | +else: |
| 31 | + import unittest |
| 32 | + |
| 33 | +try: |
| 34 | + # python2 |
| 35 | + from urllib2 import urlopen, Request, HTTPError |
| 36 | +except ImportError: |
| 37 | + # python3 |
| 38 | + from urllib.request import Request, urlopen |
| 39 | + from urllib.error import HTTPError |
| 40 | + |
| 41 | +sys.path.append('.') |
| 42 | + |
| 43 | +from telegram import Update, Message, TelegramError, User, Chat, Bot |
| 44 | +from telegram.utils.request import stop_con_pool |
| 45 | +from telegram.ext import * |
| 46 | +from tests.base import BaseTest |
| 47 | +from tests.test_updater import MockBot |
| 48 | + |
| 49 | +# Enable logging |
| 50 | +root = logging.getLogger() |
| 51 | +root.setLevel(logging.DEBUG) |
| 52 | + |
| 53 | +ch = logging.StreamHandler(sys.stdout) |
| 54 | +ch.setLevel(logging.WARN) |
| 55 | +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s ' '- %(message)s') |
| 56 | +ch.setFormatter(formatter) |
| 57 | +root.addHandler(ch) |
| 58 | + |
| 59 | + |
| 60 | +class ConversationHandlerTest(BaseTest, unittest.TestCase): |
| 61 | + """ |
| 62 | + This object represents the tests for the conversation handler. |
| 63 | + """ |
| 64 | + |
| 65 | + # State definitions |
| 66 | + # At first we're thirsty. Then we brew coffee, we drink it |
| 67 | + # and then we can start coding! |
| 68 | + END, THIRSTY, BREWING, DRINKING, CODING = range(-1, 4) |
| 69 | + |
| 70 | + # Test related |
| 71 | + def setUp(self): |
| 72 | + self.updater = None |
| 73 | + self.current_state = dict() |
| 74 | + self.entry_points =[CommandHandler('start', self.start)] |
| 75 | + self.states = {self.THIRSTY: [CommandHandler('brew', self.brew), |
| 76 | + CommandHandler('wait', self.start)], |
| 77 | + self.BREWING: [CommandHandler('pourCoffee', self.drink)], |
| 78 | + self.DRINKING: [CommandHandler('startCoding', self.code), |
| 79 | + CommandHandler('drinkMore', self.drink)], |
| 80 | + self.CODING: [CommandHandler('keepCoding', self.code), |
| 81 | + CommandHandler('gettingThirsty', self.start), |
| 82 | + CommandHandler('drinkMore', self.drink)], |
| 83 | + } |
| 84 | + self.fallbacks = [CommandHandler('eat', self.start)] |
| 85 | + |
| 86 | + def _setup_updater(self, *args, **kwargs): |
| 87 | + stop_con_pool() |
| 88 | + bot = MockBot(*args, **kwargs) |
| 89 | + self.updater = Updater(workers=2, bot=bot) |
| 90 | + |
| 91 | + def tearDown(self): |
| 92 | + if self.updater is not None: |
| 93 | + self.updater.stop() |
| 94 | + stop_con_pool() |
| 95 | + |
| 96 | + def reset(self): |
| 97 | + self.current_state = dict() |
| 98 | + |
| 99 | + # State handlers |
| 100 | + def _set_state(self, update, state): |
| 101 | + self.current_state[update.message.from_user.id]= state |
| 102 | + return state |
| 103 | + |
| 104 | + def _get_state(self, user_id): |
| 105 | + return self.current_state[user_id] |
| 106 | + |
| 107 | + # Actions |
| 108 | + def start(self, bot, update): |
| 109 | + return self._set_state(update, self.THIRSTY) |
| 110 | + |
| 111 | + def brew(self, bot, update): |
| 112 | + return self._set_state(update, self.BREWING) |
| 113 | + |
| 114 | + def drink(self, bot, update): |
| 115 | + return self._set_state(update, self.DRINKING) |
| 116 | + |
| 117 | + def code(self, bot, update): |
| 118 | + return self._set_state(update, self.CODING) |
| 119 | + |
| 120 | + # Tests |
| 121 | + def test_addConversationHandler(self): |
| 122 | + self._setup_updater('', messages=0) |
| 123 | + d = self.updater.dispatcher |
| 124 | + user = User(first_name="Misses Test", id=123) |
| 125 | + second_user = User(first_name="Mister Test", id=124) |
| 126 | + |
| 127 | + handler = ConversationHandler(entry_points=self.entry_points, |
| 128 | + states=self.states, |
| 129 | + fallbacks=self.fallbacks) |
| 130 | + d.add_handler(handler) |
| 131 | + queue = self.updater.start_polling(0.01) |
| 132 | + |
| 133 | + # User one, starts the state machine. |
| 134 | + message = Message(0, user, None, None, text="/start") |
| 135 | + queue.put(Update(update_id=0, message=message)) |
| 136 | + sleep(.1) |
| 137 | + self.assertTrue(self.current_state[user.id] == self.THIRSTY) |
| 138 | + |
| 139 | + # The user is thirsty and wants to brew coffee. |
| 140 | + message = Message(0, user, None, None, text="/brew") |
| 141 | + queue.put(Update(update_id=0, message=message)) |
| 142 | + sleep(.1) |
| 143 | + self.assertTrue(self.current_state[user.id] == self.BREWING) |
| 144 | + |
| 145 | + # Lets see if an invalid command makes sure, no state is changed. |
| 146 | + message = Message(0, user, None, None, text="/nothing") |
| 147 | + queue.put(Update(update_id=0, message=message)) |
| 148 | + sleep(.1) |
| 149 | + self.assertTrue(self.current_state[user.id] == self.BREWING) |
| 150 | + |
| 151 | + # Lets see if the state machine still works by pouring coffee. |
| 152 | + message = Message(0, user, None, None, text="/pourCoffee") |
| 153 | + queue.put(Update(update_id=0, message=message)) |
| 154 | + sleep(.1) |
| 155 | + self.assertTrue(self.current_state[user.id] == self.DRINKING) |
| 156 | + |
| 157 | + # Let's now verify that for another user, who did not start yet, |
| 158 | + # the state has not been changed. |
| 159 | + message = Message(0, second_user, None, None, text="/brew") |
| 160 | + queue.put(Update(update_id=0, message=message)) |
| 161 | + sleep(.1) |
| 162 | + self.assertRaises(KeyError, self._get_state, userid=second_user.id) |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == '__main__': |
| 166 | + unittest.main() |
0 commit comments