Пример #1
0
def test_messages(dispatcher):
    a_messsage_update = utils.message_update_by_text('a')
    b_message_update = utils.message_update_by_text('b')
    c_message_update = utils.message_update_by_text('c')
    command_update = utils.command_update_by_text('/command')

    calls = list()
    updates = list()

    async def a_handler(c, u):
        updates.append(u)
        calls.append(a_handler)

    async def b_handler(c, u):
        updates.append(u)
        calls.append(b_handler)

    async def c_handler(c, u):
        updates.append(u)
        calls.append(c_handler)

    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(a_messsage_update))
    asyncio.run(dispatcher.handle(b_message_update))
    asyncio.run(dispatcher.handle(c_message_update))
    assert before_calls_counter == len(calls)

    dispatcher.register_message_handler('a', a_handler)
    dispatcher.register_message_handler('b', b_handler)

    asyncio.run(dispatcher.handle(a_messsage_update))
    assert calls[-1] is a_handler
    assert updates[-1] is a_messsage_update

    asyncio.run(dispatcher.handle(b_message_update))
    assert calls[-1] is b_handler
    assert updates[-1] is b_message_update

    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(command_update))
    assert before_calls_counter == len(calls)

    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(c_message_update))
    assert before_calls_counter == len(calls)

    dispatcher.register_message_handler('c', c_handler)
    asyncio.run(dispatcher.handle(c_message_update))
    assert calls[-1] is c_handler
    assert updates[-1] is c_message_update
Пример #2
0
def test_middleware(dispatcher):
    update = utils.message_update_by_text('test')
    calls = list()

    async def mw_true(u):
        calls.append(mw_true)
        return True

    async def mw_false(u):
        calls.append(mw_false)
        return False

    async def message_handler(c, u):
        calls.append(message_handler)

    asyncio.run(dispatcher.handle(update))
    assert not calls
    dispatcher.register_message_handler('test', message_handler)
    asyncio.run(dispatcher.handle(update))
    assert len(calls) == 1
    assert calls[-1] is message_handler
    dispatcher.register_middleware(mw_false)
    asyncio.run(dispatcher.handle(update))
    assert len(calls) == 3
    assert calls[-2] is mw_false
    assert calls[-1] is message_handler
    dispatcher.register_middleware(mw_true)
    asyncio.run(dispatcher.handle(update))
    assert len(calls) == 5
    assert calls[-2] is mw_false
    assert calls[-1] is mw_true
def test_message_handler(dispatcher):
    a_message_update = utils.message_update_by_text('a')
    b_message_update = utils.message_update_by_text('b')

    updates = list()
    calls = list()

    @dispatcher.message_handler('a')
    async def a_message_handler(c, u):
        updates.append(u)
        calls.append(a_message_handler)

    asyncio.run(dispatcher.handle(b_message_update))
    assert not calls
    asyncio.run(dispatcher.handle(a_message_update))
    assert calls[-1] is a_message_handler
    assert updates[-1] is a_message_update
def test_middleware(dispatcher):
    update = utils.message_update_by_text('test')
    calls = list()

    @dispatcher.middleware
    async def mw_first(u):
        calls.append(mw_first)

    asyncio.run(dispatcher.handle(update))
    assert calls[-1] is mw_first
Пример #5
0
def test_inline_query(dispatcher):
    a_inline_query_update = utils.inline_query_update_by_query('a')
    b_inline_query_update = utils.inline_query_update_by_query('b')
    c_inline_query_update = utils.inline_query_update_by_query('c')
    message_update = utils.message_update_by_text('message')

    calls = list()
    updates = list()

    async def a_handler(c, u):
        updates.append(u)
        calls.append(a_handler)

    async def b_handler(c, u):
        updates.append(u)
        calls.append(b_handler)

    async def c_handler(c, u):
        updates.append(u)
        calls.append(c_handler)

    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(a_inline_query_update))
    asyncio.run(dispatcher.handle(b_inline_query_update))
    asyncio.run(dispatcher.handle(c_inline_query_update))
    assert before_calls_counter == len(calls)

    dispatcher.register_inline_handler('a', a_handler)
    dispatcher.register_inline_handler('b', b_handler)

    asyncio.run(dispatcher.handle(a_inline_query_update))
    assert calls[-1] is a_handler
    assert updates[-1] is a_inline_query_update

    asyncio.run(dispatcher.handle(b_inline_query_update))
    assert calls[-1] is b_handler
    assert updates[-1] is b_inline_query_update

    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(message_update))
    assert before_calls_counter == len(calls)

    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(c_inline_query_update))
    assert before_calls_counter == len(calls)

    dispatcher.register_inline_handler('c', c_handler)
    asyncio.run(dispatcher.handle(c_inline_query_update))
    assert calls[-1] is c_handler
    assert updates[-1] is c_inline_query_update
Пример #6
0
def test_match_sequence(dispatcher):
    abc_message_update = utils.message_update_by_text('abc')

    updates = list()
    calls = list()

    async def universal_handler(c, u):
        updates.append(u)
        calls.append(universal_handler)

    async def abc_handler(c, u):
        updates.append(u)
        calls.append(abc_handler)

    dispatcher.register_message_handler(re.compile('.*'), universal_handler)
    dispatcher.register_message_handler('abc', abc_handler)
    asyncio.run(dispatcher.handle(abc_message_update))
    assert calls[-1] is abc_handler
    assert updates[-1] is abc_message_update
Пример #7
0
def test_venue(dispatcher):
    venue_update = utils.venue_update()
    message_update = utils.message_update_by_text('message')

    calls = list()
    updates = list()

    async def venue_handler(c, u):
        updates.append(u)
        calls.append(venue_handler)

    async def message_handler(c, u):
        updates.append(u)
        calls.append(message_handler)

    dispatcher.register_message_handler(re.compile('.*'), message_handler)
    asyncio.run(dispatcher.handle(message_update))
    asyncio.run(dispatcher.handle(venue_update))
    assert calls[-1] is message_handler
    assert updates[-1] is message_update
    dispatcher.register_venue_handler(venue_handler)
    asyncio.run(dispatcher.handle(venue_update))
    assert calls[-1] is venue_handler
    assert updates[-1] is venue_update
Пример #8
0
def test_location(dispatcher):
    location_update = utils.location_update()
    message_update = utils.message_update_by_text('message')

    calls = list()
    updates = list()

    async def location_handler(c, u):
        updates.append(u)
        calls.append(location_handler)

    async def message_handler(c, u):
        updates.append(u)
        calls.append(message_handler)

    dispatcher.register_message_handler(re.compile('.*'), message_handler)
    asyncio.run(dispatcher.handle(message_update))
    asyncio.run(dispatcher.handle(location_update))
    assert calls[-1] is message_handler
    assert updates[-1] is message_update
    dispatcher.register_location_handler(location_handler)
    asyncio.run(dispatcher.handle(location_update))
    assert calls[-1] is location_handler
    assert updates[-1] is location_update
Пример #9
0
def test_new_chat_photo(dispatcher):
    new_chat_photo_update = utils.new_chat_photo_update()
    message_update = utils.message_update_by_text('message')

    calls = list()
    updates = list()

    async def new_chat_photo_handler(c, u):
        updates.append(u)
        calls.append(new_chat_photo_handler)

    async def message_handler(c, u):
        updates.append(u)
        calls.append(message_handler)

    dispatcher.register_message_handler(re.compile('.*'), message_handler)
    asyncio.run(dispatcher.handle(message_update))
    asyncio.run(dispatcher.handle(new_chat_photo_update))
    assert calls[-1] is message_handler
    assert updates[-1] is message_update
    dispatcher.register_new_chat_photo_handler(new_chat_photo_handler)
    asyncio.run(dispatcher.handle(new_chat_photo_update))
    assert calls[-1] is new_chat_photo_handler
    assert updates[-1] is new_chat_photo_update
Пример #10
0
def test_document(dispatcher):
    document_update = utils.document_update()
    message_update = utils.message_update_by_text('message')

    calls = list()
    updates = list()

    async def document_handler(c, u):
        updates.append(u)
        calls.append(document_handler)

    async def message_handler(c, u):
        updates.append(u)
        calls.append(message_handler)

    dispatcher.register_message_handler(re.compile('.*'), message_handler)
    asyncio.run(dispatcher.handle(message_update))
    asyncio.run(dispatcher.handle(document_update))
    assert calls[-1] is message_handler
    assert updates[-1] is message_update
    dispatcher.register_document_handler(document_handler)
    asyncio.run(dispatcher.handle(document_update))
    assert calls[-1] is document_handler
    assert updates[-1] is document_update
Пример #11
0
def test_patterns_regexp(dispatcher):
    foobar_message_update = utils.message_update_by_text('foobar')
    hi_dude_message_update = utils.message_update_by_text('hi dude')
    hi_mate_message_update = utils.message_update_by_text('hi mate')
    foobar_callback_update = utils.callback_update_by_data('foobar')
    drop_a_callback_update = utils.callback_update_by_data('drop_a')
    drop_b_callback_update = utils.callback_update_by_data('drop_b')
    foobar_command_update = utils.command_update_by_text('/foobar')
    give_1_command_update = utils.command_update_by_text('/give_1')
    give_2_command_update = utils.command_update_by_text('/give_2')
    foobar_inline_query_update = utils.inline_query_update_by_query('foobar')
    send_a_inline_query_update = utils.inline_query_update_by_query('send a')
    send_b_inline_query_update = utils.inline_query_update_by_query('send b')

    calls = list()
    updates = list()

    async def hi_message_handler(c, u):
        updates.append(u)
        calls.append(hi_message_handler)

    async def drop_callback_handler(c, u):
        updates.append(u)
        calls.append(drop_callback_handler)

    async def give_command_handler(c, u):
        updates.append(u)
        calls.append(give_command_handler)

    async def send_inline_query_handler(c, u):
        updates.append(u)
        calls.append(send_inline_query_handler)

    async def common_handler(c, u):
        updates.append(u)
        calls.append(common_handler)

    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(hi_dude_message_update))
    asyncio.run(dispatcher.handle(hi_mate_message_update))
    asyncio.run(dispatcher.handle(foobar_message_update))
    assert before_calls_counter == len(calls)
    dispatcher.register_message_handler(re.compile('^hi'), hi_message_handler)
    asyncio.run(dispatcher.handle(hi_dude_message_update))
    assert calls[-1] is hi_message_handler
    assert updates[-1] is hi_dude_message_update
    asyncio.run(dispatcher.handle(hi_mate_message_update))
    assert calls[-1] is hi_message_handler
    assert updates[-1] is hi_mate_message_update
    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(foobar_message_update))
    assert before_calls_counter == len(calls)
    dispatcher.register_message_handler(re.compile('.*'), common_handler)
    asyncio.run(dispatcher.handle(foobar_message_update))
    assert calls[-1] is common_handler
    assert updates[-1] is foobar_message_update

    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(drop_a_callback_update))
    asyncio.run(dispatcher.handle(drop_b_callback_update))
    asyncio.run(dispatcher.handle(foobar_callback_update))
    assert before_calls_counter == len(calls)
    dispatcher.register_callback_handler(re.compile('^drop'),
                                         drop_callback_handler)
    asyncio.run(dispatcher.handle(drop_a_callback_update))
    assert calls[-1] is drop_callback_handler
    assert updates[-1] is drop_a_callback_update
    asyncio.run(dispatcher.handle(drop_b_callback_update))
    assert calls[-1] is drop_callback_handler
    assert updates[-1] is drop_b_callback_update
    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(foobar_callback_update))
    assert before_calls_counter == len(calls)
    dispatcher.register_callback_handler(re.compile('.*'), common_handler)
    asyncio.run(dispatcher.handle(foobar_callback_update))
    assert calls[-1] is common_handler
    assert updates[-1] is foobar_callback_update

    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(give_1_command_update))
    asyncio.run(dispatcher.handle(give_2_command_update))
    asyncio.run(dispatcher.handle(foobar_command_update))
    assert before_calls_counter == len(calls)
    dispatcher.register_command_handler(re.compile('^/give'),
                                        give_command_handler)
    asyncio.run(dispatcher.handle(give_1_command_update))
    assert calls[-1] is give_command_handler
    assert updates[-1] is give_1_command_update
    asyncio.run(dispatcher.handle(give_2_command_update))
    assert calls[-1] is give_command_handler
    assert updates[-1] is give_2_command_update
    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(foobar_command_update))
    assert before_calls_counter == len(calls)
    dispatcher.register_command_handler(re.compile('^/.*'), common_handler)
    asyncio.run(dispatcher.handle(foobar_command_update))
    assert calls[-1] is common_handler
    assert updates[-1] is foobar_command_update

    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(send_a_inline_query_update))
    asyncio.run(dispatcher.handle(send_b_inline_query_update))
    asyncio.run(dispatcher.handle(foobar_inline_query_update))
    assert before_calls_counter == len(calls)
    dispatcher.register_inline_handler(re.compile('^send'),
                                       send_inline_query_handler)
    asyncio.run(dispatcher.handle(send_a_inline_query_update))
    assert calls[-1] is send_inline_query_handler
    assert updates[-1] is send_a_inline_query_update
    asyncio.run(dispatcher.handle(send_b_inline_query_update))
    assert calls[-1] is send_inline_query_handler
    assert updates[-1] is send_b_inline_query_update
    before_calls_counter = len(calls)
    asyncio.run(dispatcher.handle(foobar_inline_query_update))
    assert before_calls_counter == len(calls)
    dispatcher.register_inline_handler(re.compile('.*'), common_handler)
    asyncio.run(dispatcher.handle(foobar_inline_query_update))
    assert calls[-1] is common_handler
    assert updates[-1] is foobar_inline_query_update