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)
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
def test_bot_update_chat_member(user_dict: _UserDict, context: Context) -> None: chat_member = ChatMemberUpdated.from_dict({ 'chat': { 'id': 111, 'type': 'group' }, 'from': { 'id': 1, 'is_bot': False, 'first_name': 'name' }, 'date': 123, 'old_chat_member': { 'user': { 'id': 1, 'is_bot': False, 'first_name': 'name1' }, 'status': 'status1' }, 'new_chat_member': { 'user': { 'id': 1, 'is_bot': False, 'first_name': 'name2' }, 'status': 'status2' } }) _update = Update(update_id=1, chat_member=chat_member) _bot_update = BotUpdate('state1', context, _update) assert _bot_update.chat_member == chat_member
def test_bot_update_callback_query(user_dict: _UserDict, context: Context) -> None: callback_query = CallbackQuery.from_dict({ 'id': '1', 'from': user_dict, 'chat_instance': 'ci' }) _update = Update(update_id=1, callback_query=callback_query) _bot_update = BotUpdate('state1', context, _update) assert _bot_update.callback_query == callback_query
def test_bot_update_chosen_inline_result(user_dict: _UserDict, context: Context) -> None: chosen_inline_result = ChosenInlineResult.from_dict({ 'result_id': '1', 'from': user_dict, 'query': 'q' }) _update = Update(update_id=1, chosen_inline_result=chosen_inline_result) _bot_update = BotUpdate('state1', context, _update) assert _bot_update.chosen_inline_result == chosen_inline_result
def test_bot_update_inline_query(user_dict: _UserDict, context: Context) -> None: inline_query = InlineQuery.from_dict({ 'id': '1', 'from': user_dict, 'query': 'q', 'offset': 'o' }) _update = Update(update_id=1, inline_query=inline_query) _bot_update = BotUpdate('state1', context, _update) assert _bot_update.inline_query == inline_query
def test_bot_update_pre_checkout_query(user_dict: _UserDict, context: Context) -> None: pre_checkout_query = PreCheckoutQuery.from_dict({ 'id': '1', 'from': user_dict, 'currency': 'c', 'total_amount': 1, 'invoice_payload': 'ip' }) _update = Update(update_id=1, pre_checkout_query=pre_checkout_query) _bot_update = BotUpdate('state1', context, _update) assert _bot_update.pre_checkout_query == pre_checkout_query
def test_bot_update_poll_answer(user_dict: _UserDict, context: Context) -> None: poll_answer = PollAnswer.from_dict({ 'poll_id': 'id', 'user': { 'id': 1, 'is_bot': False, 'first_name': 'name' }, 'option_ids': [] }) _update = Update(update_id=1, poll_answer=poll_answer) _bot_update = BotUpdate('state1', context, _update) assert _bot_update.poll_answer == poll_answer
def test_bot_update_poll(user_dict: _UserDict, context: Context) -> None: poll = Poll.from_dict({ 'id': 'id', 'question': 'question', 'options': [], 'total_voter_count': 3, 'is_closed': True, 'is_anonymous': True, 'type': 'quiz', 'allows_multiple_answers': False, 'explanation_entities': [] }) _update = Update(update_id=1, poll=poll) _bot_update = BotUpdate('state1', context, _update) assert _bot_update.poll == poll
def _make_bot_update(state: Optional[str], context: Context, update_id: int = 1, **params: Any) -> BotUpdate: params = { 'message': None, 'edited_message': None, 'channel_post': None, 'edited_channel_post': None, 'inline_query': None, 'chosen_inline_result': None, 'callback_query': None, 'shipping_query': None, 'pre_checkout_query': None, 'poll': None, 'poll_answer': None, **params } return BotUpdate(state, context, Update(update_id, **params))
async def test_api_methods_get_updates(bot: Bot) -> None: update = Update.from_dict({'update_id': 1}) bot.request_mock.return_value = APIResponse.from_dict({ 'ok': True, 'result': [{ 'update_id': 1 }] }) assert await bot.get_updates(offset=0, limit=10, timeout=15, allowed_updates=[UpdateType.MESSAGE ]) == (update, ) assert bot.request_mock.call_args_list == [ call(RequestMethod.GET, 'getUpdates', offset=0, limit=10, timeout=15, allowed_updates='["message"]') ]
def test_bot_update_shipping_query(user_dict: _UserDict, context: Context) -> None: shipping_address = { 'country_code': 'cc', 'state': 's', 'city': 'c', 'street_line1': 'sl1', 'street_line2': 'sl2', 'post_code': 'pc' } shipping_query = ShippingQuery.from_dict({ 'id': '1', 'from': user_dict, 'invoice_payload': 'ip', 'shipping_address': shipping_address }) _update = Update(update_id=1, shipping_query=shipping_query) _bot_update = BotUpdate('state1', context, _update) assert _bot_update.shipping_query == shipping_query
def test_json_dumps_base_telegram() -> None: update = Update.from_dict({'update_id': 1}) assert api_methods._json_dumps(update) == json_dumps(update.to_dict())
def update(message: Message) -> Update: return Update(update_id=1, message=message)
def test_bot_update_edited_channel_post(message: Message, context: Context) -> None: _update = Update(update_id=1, edited_channel_post=message) _bot_update = BotUpdate('state1', context, _update) assert _bot_update.edited_channel_post == message