Esempio n. 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)
def test_poll_answer(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.poll_answer(state='state1', filters=[GroupChatFilter()])(handler)

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.POLL_ANSWER),
        StateFilter('state1'),
        GroupChatFilter()
    ))]
Esempio n. 3
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
def test_edited_message(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.edited_message(state='state1',
                      filters=[GroupChatFilter()])(handler)

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.EDITED_MESSAGE),
        StateFilter('state1'),
        GroupChatFilter()
    ))]
def test_chat_member(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.chat_member(state='state1',
                   filters=[GroupChatFilter()])(handler)

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.CHAT_MEMBER),
        StateFilter('state1'),
        GroupChatFilter()
    ))]
def test_pre_checkout_query(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.pre_checkout_query(state='state1',
                          filters=[GroupChatFilter()])(handler)

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.PRE_CHECKOUT_QUERY),
        StateFilter('state1'),
        GroupChatFilter()
    ))]
def test_shipping_query(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.shipping_query(state='state1',
                      filters=[GroupChatFilter()])(handler)

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.SHIPPING_QUERY),
        StateFilter('state1'),
        GroupChatFilter()
    ))]
def test_chosen_inline_result(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.chosen_inline_result(state='state1',
                            filters=[GroupChatFilter()])(handler)

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.CHOSEN_INLINE_RESULT),
        StateFilter('state1'),
        GroupChatFilter()
    ))]
def test_table_inline_query(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.inline_query(state='state1',
                    filters=[GroupChatFilter()])(handler)

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.INLINE_QUERY),
        StateFilter('state1'),
        GroupChatFilter()
    ))]
Esempio n. 10
0
def test_table_edited_channel_post(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.edited_channel_post(state='state1',
                           filters=[GroupChatFilter()])(handler)

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.EDITED_CHANNEL_POST),
        StateFilter('state1'),
        GroupChatFilter()
    ))]
Esempio n. 11
0
def test_channel_post_handler(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.channel_post_handler(handler,
                            state='state1',
                            filters=[GroupChatFilter()])

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.CHANNEL_POST),
        StateFilter('state1'),
        GroupChatFilter()
    ))]
Esempio n. 12
0
def test_callback_query(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.callback_query(state='state1',
                      data_match='pattern',
                      filters=[GroupChatFilter()])(handler)

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.CALLBACK_QUERY),
        StateFilter('state1'),
        CallbackQueryDataFilter(re.compile('pattern')),
        GroupChatFilter()
    ))]
Esempio n. 13
0
def test_message(handler: HandlerCallable) -> None:
    ht = HandlerTable()
    ht.message(state='state1', commands=['command1'],
               content_types=[ContentType.CONTACT],
               text_match='pattern',
               filters=[PrivateChatFilter()])(handler)

    assert ht._handlers == [Handler(handler, filters=(
        UpdateTypeFilter(UpdateType.MESSAGE),
        StateFilter('state1'),
        CommandsFilter(('command1',)),
        ContentTypeFilter((ContentType.CONTACT,)),
        MessageTextFilter(re.compile('pattern')),
        PrivateChatFilter()
    ))]
Esempio n. 14
0
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()])
Esempio n. 15
0
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
Esempio n. 16
0
def test_protocol() -> None:
    ht: HandlerTableProtocol = HandlerTable()
    assert isinstance(ht, HandlerTableProtocol)