def test_commands(dispatcher): a_command_update = utils.command_update_by_text('/a') b_command_update = utils.command_update_by_text('/b') c_command_update = utils.command_update_by_text('/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_command_update)) asyncio.run(dispatcher.handle(b_command_update)) asyncio.run(dispatcher.handle(c_command_update)) assert before_calls_counter == len(calls) dispatcher.register_command_handler('/a', a_handler) dispatcher.register_command_handler('/b', b_handler) asyncio.run(dispatcher.handle(a_command_update)) assert calls[-1] is a_handler assert updates[-1] is a_command_update asyncio.run(dispatcher.handle(b_command_update)) assert calls[-1] is b_handler assert updates[-1] is b_command_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_command_update)) assert before_calls_counter == len(calls) dispatcher.register_command_handler('/c', c_handler) asyncio.run(dispatcher.handle(c_command_update)) assert calls[-1] is c_handler assert updates[-1] is c_command_update
def test_command_handler(dispatcher): a_command_update = utils.command_update_by_text('/a') b_command_update = utils.command_update_by_text('/b') updates = list() calls = list() @dispatcher.command_handler('/a') async def a_handler(c, u): updates.append(u) calls.append(a_handler) asyncio.run(dispatcher.handle(b_command_update)) assert not calls asyncio.run(dispatcher.handle(a_command_update)) assert calls[-1] is a_handler assert updates[-1] is a_command_update
def test_direct_handling_decorator(): update = utils.command_update_by_text('/test') sm = DictStateManager() dispatcher = StateDispatcher(sm, 'main') child_one = Dispatcher() dispatcher.register_state('1', child_one) dispatcher_updates = list() @dispatcher.command_handler('test') async def dispatcher_test(c, u): dispatcher_updates.append(u) asyncio.run(dispatcher.handle(update)) assert len(dispatcher_updates) == 1 sm.update = update sm.set('main', '1') asyncio.run(dispatcher.handle(update)) assert len(dispatcher_updates) == 2 # with direct handling @dispatcher.command_handler('test') @dispatcher.direct_handling async def dispatcher_test_direct_handling(c, u): dispatcher_updates.append(u) sm.update = update assert sm.get('main') == '1' asyncio.run(dispatcher.handle(update)) assert len(dispatcher_updates) == 2 sm.update = update sm.reset('main') assert not sm.get('main') asyncio.run(dispatcher.handle(update)) assert len(dispatcher_updates) == 3
def test_reverse_dispatching(): update = utils.command_update_by_text('/test') start_update = utils.command_update_by_text('/start') sm = DictStateManager() dispatcher = StateDispatcher(sm, 'main') child_one = Dispatcher() child_two = Dispatcher() dispatcher.register_state('1', child_one) dispatcher.register_state('2', child_two) dispatcher_calls, dispatcher_updates = list(), list() child_one_calls, child_one_updates = list(), list() child_two_calls, child_two_updates = list(), list() @dispatcher.command_handler('test') async def dispatcher_test(c, u): dispatcher_calls.append(dispatcher_test) dispatcher_updates.append(u) @dispatcher.command_handler('start') async def dispatcher_start(c, u): dispatcher_calls.append(dispatcher_start) dispatcher_updates.append(u) @child_one.command_handler('test') async def child_one_test(c, u): child_one_calls.append(child_one_test) child_one_updates.append(u) @child_two.command_handler('test') async def child_two_test(c, u): child_two_calls.append(child_two_test) child_two_updates.append(u) asyncio.run(dispatcher.handle(update)) assert dispatcher_calls[-1] == dispatcher_test assert len(dispatcher_updates) == 1 assert len(child_one_updates) == 0 assert len(child_two_updates) == 0 sm.update = update sm.set('main', '1') asyncio.run(dispatcher.handle(update)) assert child_one_calls[-1] == child_one_test assert len(dispatcher_updates) == 1 assert len(child_one_updates) == 1 assert len(child_two_updates) == 0 sm.update = update sm.set('main', '2') asyncio.run(dispatcher.handle(update)) assert child_two_calls[-1] == child_two_test assert len(dispatcher_updates) == 1 assert len(child_one_updates) == 1 assert len(child_two_updates) == 1 asyncio.run(dispatcher.handle(start_update)) assert dispatcher_calls[-1] == dispatcher_start assert dispatcher_updates[-1] == start_update assert len(dispatcher_updates) == 2 assert len(child_one_updates) == 1 assert len(child_two_updates) == 1
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