Ejemplo n.º 1
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)
Ejemplo n.º 2
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
 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))
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
def test_bot_update_get_item(bot_update: BotUpdate) -> None:
    assert bot_update['key2'] == 'str2'
    assert bot_update.get('key4') is None
Ejemplo n.º 13
0
def test_bot_update_state_setter(bot_update: BotUpdate) -> None:
    assert bot_update.state == 'state1'
    bot_update.state = 'state2'
    assert bot_update.state == 'state2'
Ejemplo n.º 14
0
def test_bot_update_init(context: Context, update: Update) -> None:
    bu = BotUpdate('state1', context, update)
    assert bu.state == 'state1'
    assert bu.context == context
    assert bu.message == update.message
Ejemplo n.º 15
0
def bot_update(context: Context, update: Update) -> BotUpdate:
    bu = BotUpdate(state='state1', context=context, update=update)
    bu['key1'] = 'str1'
    bu['key2'] = 'str2'
    bu['key3'] = 4
    return bu
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
def test_bot_update_delitem(bot_update: BotUpdate) -> None:
    assert bot_update['key3'] == 4
    del bot_update['key3']
    assert bot_update.get('key3') is None