Skip to content

Unified mocks #8

@nmlorg

Description

@nmlorg

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:

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:

assert ctx.reply_text(response_text) == 'REPLY'
assert bot.messages[chat['id'], None] is response_text
bot.messages.clear()

Metadata

Metadata

Assignees

Labels

cleanupCode changes that improve maintainability without changing behavior

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions