-
Notifications
You must be signed in to change notification settings - Fork 0
Description
As part of the general-purpose .transcript-based test runner, I want to unify all or most of the various Bot and other mocks.
I'm toying with the idea of creating a [partial] mock of telegram-bot-api, which can intercept requests calls to https://api.telegram.org/... and respond to things like getme, sendmessage, etc., including keeping track of if bots have existing chats open with users (returning 403 if not), etc.
def test_something(telegram):
bot = ntelebot.bot.Bot('1234:token')
with pytest.raises(ntelebot.errors.Forbidden):
bot.send_message(chat_id=1000, text='Should fail')
telegram.send_update(1234, {'message': {'from': {'id': 1000}, 'chat': {'id': 1234}, 'text': 'Initial message'}})
bot.send_message(chat_id=1000, text='Should succeed')This could alternatively be part of [Mock]Bot:
def test_something(telegram):
bot = ntelebot.bot.Bot('1234:token')
with pytest.raises(ntelebot.errors.Forbidden):
bot.send_message(chat_id=1000, text='Should fail')
bot.handle_update({'message': {'from': {'id': 1000}, 'chat': {'id': 1234}, 'text': 'Initial message'}})
bot.send_message(chat_id=1000, text='Should succeed')(or maybe both, with the latter just being a convenient way to access the former).
The point would be to be able to clean up things like:
ntelebot/ntelebot/test_preprocess.py
Lines 40 to 45 in 9e42260
| if chat_id in self.unauthorized: | |
| raise ntelebot.errors.Forbidden() | |
| self.messages[chat_id, None] = text | |
| self.parse_modes[chat_id, None] = parse_mode | |
| return reply_to_message_id and 'REPLY' or 'SEND' |
used in places like:
ntelebot/ntelebot/test_preprocess.py
Lines 160 to 162 in 9e42260
| assert ctx.reply_text(response_text) == 'REPLY' | |
| assert bot.messages[chat['id'], None] is response_text | |
| bot.messages.clear() |