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))
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() ))]
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() ))]
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() ))]
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() ))]
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() ))]
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() ))]
def test_update_type_filter_protocol() -> None: _filter: FilterProtocol = UpdateTypeFilter(UpdateType.CHANNEL_POST) assert isinstance(_filter, FilterProtocol)