Пример #1
0
async def test_update_type_filter(bot: Bot,
                                  make_bot_update: _MakeBotUpdate) -> None:
    _filter = UpdateTypeFilter(UpdateType.CHANNEL_POST)
    assert await _filter.check(
        bot, make_bot_update(None, Context({}), channel_post={}))
    assert not await _filter.check(
        bot, make_bot_update(None, Context({}), update_id=1))
Пример #2
0
async def test_message_text_filter(bot: Bot, make_message: _MakeMessage,
                                   make_bot_update: _MakeBotUpdate) -> None:
    _filter = MessageTextFilter(re.compile(r'\d{2}\.\d{2}'))
    assert await _filter.check(
        bot,
        make_bot_update(None, Context({}), message=make_message(text='01.02')))
    assert not await _filter.check(
        bot, make_bot_update(None, Context({}), message=make_message()))
Пример #3
0
async def test_content_types_filter_false(
        bot: Bot, make_message: _MakeMessage,
        make_bot_update: _MakeBotUpdate) -> None:
    _filter = ContentTypeFilter((ContentType.TEXT, ))
    assert not await _filter.check(bot, make_bot_update(None, Context({})))
    assert not await _filter.check(
        bot, make_bot_update(None, Context({}),
                             message=make_message(photo=[])))
Пример #4
0
async def test_commands_filter(bot: Bot, make_message: _MakeMessage,
                               make_bot_update: _MakeBotUpdate) -> None:
    _filter = CommandsFilter(('command1', ))
    assert await _filter.check(
        bot,
        make_bot_update(None,
                        Context({}),
                        message=make_message(text='/command1')))
    assert not await _filter.check(
        bot,
        make_bot_update(
            None, Context({}), message=make_message(text='/command2')))
    assert not await _filter.check(bot, make_bot_update(None, Context({})))
Пример #5
0
async def test_group_chat_filter(bot: Bot, make_message: _MakeMessage,
                                 make_bot_update: _MakeBotUpdate) -> None:
    _filter: FilterProtocol = GroupChatFilter()
    assert await _filter.check(
        bot,
        make_bot_update(None,
                        Context({}),
                        message=make_message(chat={
                            'id': 1,
                            'type': 'group'
                        })))
    assert not await _filter.check(
        bot, make_bot_update(None, Context({}), message=make_message()))
Пример #6
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)
Пример #7
0
def test_context_set_item(context: Context) -> None:
    context['key5'] = 6
    assert context.to_dict() == {
        'key1': 'str1',
        'key2': 'str2',
        'key3': 4,
        'key5': 6
    }
Пример #8
0
async def test_content_types_filter(bot: Bot, make_message: _MakeMessage,
                                    make_bot_update: _MakeBotUpdate,
                                    payload: str) -> None:
    _filter = ContentTypeFilter((ContentType.TEXT, ))
    assert await _filter.check(
        bot,
        make_bot_update(None, Context({}),
                        **{payload: make_message(text='text1')}))
Пример #9
0
async def test_callback_query_data_filter(
        bot: Bot, make_bot_update: _MakeBotUpdate) -> None:
    _filter = CallbackQueryDataFilter(re.compile(r'\d{2}\.\d{2}'))
    user = {'id': 1, 'is_bot': False, 'first_name': '2'}
    cq = CallbackQuery.from_dict({
        'id': '1',
        'from': user,
        'data': '01.02',
        'chat_instance': '1'
    })
    assert await _filter.check(
        bot, make_bot_update(None, Context({}), callback_query=cq))
    cq = CallbackQuery.from_dict({
        'id': '1',
        'from': user,
        'date': 1,
        'chat_instance': '1'
    })
    assert not await _filter.check(
        bot, make_bot_update(None, Context({}), callback_query=cq))
Пример #10
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
Пример #11
0
async def test_state_filter(bot: Bot, make_bot_update: _MakeBotUpdate) -> None:
    _filter = StateFilter('state1')
    assert await _filter.check(bot, make_bot_update('state1', Context({})))
    assert not await _filter.check(bot, make_bot_update('state2', Context({})))
Пример #12
0
def context() -> Context:
    return Context({'key1': 'str1', 'key2': 'str2', 'key3': 4})
Пример #13
0
def test_context_get_item(context: Context) -> None:
    assert context['key2'] == 'str2'
    assert context.get('key4') is None
Пример #14
0
async def test_and_filter(bot: Bot, make_bot_update: _MakeBotUpdate,
                          flag1: bool, flag2: bool, result: bool) -> None:
    _filter: FilterProtocol = ANDFilter(FlagFilter(flag1), FlagFilter(flag2))
    update = make_bot_update(None, Context({}))
    assert await _filter.check(bot, update) == result
Пример #15
0
def test_context_delitem(context: Context) -> None:
    del context['key3']
    assert context.to_dict() == {'key1': 'str1', 'key2': 'str2'}
Пример #16
0
def test_context_to_dict(context: Context) -> None:
    assert context.to_dict() == {'key1': 'str1', 'key2': 'str2', 'key3': 4}
Пример #17
0
def test_context_init() -> None:
    ctx = Context({'1': 2})
    assert ctx.to_dict() == {'1': 2}