示例#1
0
async def test_handler_check() -> None:
    async def func1(_: Bot, __: BotUpdate) -> None:
        ...

    handler = Handler(func1, (
        UpdateTypeFilter(UpdateType.MESSAGE),
        StateFilter('state1'),
    ))

    table = HandlerTable()
    table.freeze()
    _bot = PollBot('token', table, MemoryStorage())
    ctx = Context({'key1': 'str1', 'key2': 'str2', 'key3': 4})
    message = Message.from_dict({
        'message_id': 1,
        'date': 1,
        'chat': {
            'id': 1,
            'type': 'private'
        }
    })
    bu1 = BotUpdate('state1', ctx, Update(update_id=1, message=message))
    assert await handler.check(_bot, bu1)
    bu2 = BotUpdate('state2', ctx, Update(update_id=2, message=message))
    assert not await handler.check(_bot, bu2)
示例#2
0
async def bot() -> Bot:
    table = HandlerTable()
    table.freeze()
    bot = PollBot('token', table, MemoryStorage())
    bot['key1'] = 'str1'
    bot['key2'] = 'str2'
    bot['key3'] = 4

    return bot
async def test_get_handler(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.message(state='state1')(handler)
    ht.freeze()
    _bot = PollBot('token', ht, MemoryStorage())
    ctx = Context({'key1': 'str1', 'key2': 'str2', 'key3': 4})
    message = Message.from_dict({'message_id': 1, 'date': 1,
                                 'chat': {'id': 1, 'type': 'private'}})
    bu1 = BotUpdate('state1', ctx, Update(update_id=1, message=message))
    assert await ht.get_handler(_bot, bu1) == handler
    bu2 = BotUpdate('state2', ctx, Update(update_id=2, message=message))
    assert await ht.get_handler(_bot, bu2) is None
def test_freeze(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    assert not ht.frozen
    ht.message_handler(handler, state='state1', commands=['command1'],
                       content_types=[ContentType.CONTACT],
                       text_match='pattern',
                       filters=[PrivateChatFilter()])
    ht.freeze()
    assert ht.frozen
    with pytest.raises(RuntimeError, match='Cannot modify frozen list.'):
        ht.message_handler(handler, state='state1', commands=['command1'],
                           content_types=[ContentType.CONTACT],
                           text_match='pattern',
                           filters=[PrivateChatFilter()])