-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,5 @@ docs/_build/ | |
|
||
# PyBuilder | ||
target/ | ||
|
||
.idea |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |