Skip to content

Commit

Permalink
added unittest support
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe Pina authored and fopina committed Jul 23, 2015
1 parent 43a3c71 commit c59ddb7
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ docs/_build/

# PyBuilder
target/

.idea
Empty file added tests/__init__.py
Empty file.
52 changes: 52 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from tgbot import plugintest, TGPluginBase
from twx.botapi import Update


class TestPlugin(TGPluginBase):
def chat(self, bot, message, text):
pass

def list_commands(self):
return [
('echo', self.echo, 'right back at ya')
]

def echo(self, bot, message, text):
reply = text
if not reply:
reply = 'echo'
bot.tg.send_message(message.chat.id, reply, reply_to_message_id=message.message_id)


class TestPluginTest(plugintest.PluginTestCase):
def setUp(self):
self.bot = self.fake_bot('', plugins=[TestPlugin()])

def test_reply(self):
self.bot.process_update(
Update.from_dict({
'update_id': 1,
'message': {
'message_id': 1,
'text': '/echo',
'chat': {
'id': 1,
},
}
})
)
self.assertReplied(self.bot, 'echo')

self.bot.process_update(
Update.from_dict({
'update_id': 1,
'message': {
'message_id': 1,
'text': '/echo test',
'chat': {
'id': 1,
},
}
})
)
self.assertReplied(self.bot, 'test')
44 changes: 23 additions & 21 deletions tgbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def is_expected(self, bot, message):

return False


class TGBot(object):
def __init__(self, token, polling_time=2, plugins=[], no_command=None):
self._token = token
Expand All @@ -108,36 +109,37 @@ def __init__(self, token, polling_time=2, plugins=[], no_command=None):
for cmd in p.list_commands():
if cmd[0] in self.cmds:
raise Exception(
'Duplicate command %s: both in %s and %s' % [
'Duplicate command %s: both in %s and %s' % (
cmd[0],
type(p).__name__,
self.cmds[cmd[0]][2],
])
)
)
self.cmds[cmd[0]] = (cmd[1], cmd[2], type(p).__name__)

def process_update(self, update):
if update.message.text:
if update.message.text.startswith('/'):
spl = update.message.text.find(' ')
if spl < 0:
self.process(update.message.text[1:], '', update.message)
else:
self.process(update.message.text[1:spl], update.message.text[spl + 1:], update.message)
else:
was_expected = False
for p in self._plugins:
was_expected = p.is_expected(self, update.message)
if was_expected:
break

if self._no_cmd is not None and not was_expected:
self._no_cmd.chat(self, update.message, update.message.text)

def run(self):
while True:
ups = self.tg.get_updates(offset=self._last_id).wait()
for up in ups:
if up.message.text:
if up.message.text.startswith('/'):
spl = up.message.text.find(' ')
if spl < 0:
self.process(up.message.text[1:], '', up.message)
else:
self.process(up.message.text[1:spl], up.message.text[spl + 1:], up.message)
else:
was_expected = False
for p in self._plugins:
was_expected = p.is_expected(self, up.message)
if was_expected:
break

if self._no_cmd is not None and not was_expected:
self._no_cmd.chat(self, up.message, up.message.text)

else:
pass
self.process_update(up)
self._last_id = up.update_id + 1

sleep(self._polling_time)
Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions tgbot/plugintest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from twx import botapi
from . import TGBot


class PluginTestCase(unittest.TestCase):
def fake_bot(self, *args, **kwargs):
bot = TGBot(*args, **kwargs)
bot.tg = FakeTelegramBot()
return bot

def assertReplied(self, bot, text):
self.assertEqual(self.last_reply(bot), text)

def last_reply(self, bot):
self.assertGreater(len(bot.tg._sent_messages), 0, msg='No replies')
return bot.tg._sent_messages[-1][0][1]



class FakeTelegramBot(botapi.TelegramBot):

class FakeRPCRequest(object):
def wait(self):
return None

def __init__(self):
botapi.TelegramBot.__init__(self, '')
self._sent_messages = []

def send_message(self, *args, **kwargs):
self._sent_messages.append((args, kwargs))
return FakeTelegramBot.FakeRPCRequest()

0 comments on commit c59ddb7

Please sign in to comment.