예제 #1
0
def test_lont_message_for_kwargs():
    ctx = Context(backend=CoroutineMock(perform_send=CoroutineMock()))
    ctx.default_target_id = 1

    asyncio.get_event_loop().run_until_complete(ctx.reply("a" * (4096 * 2 - 1)))

    ctx.backend.perform_send.assert_any_await(1, "a" * 4096, (), {})
    ctx.backend.perform_send.assert_any_await(1, "a" * 4095, (), {})
예제 #2
0
def test_reply_no_default_target():
    ctx = Context(backend=MagicMock(execute_send=CoroutineMock()))
    ctx.default_target_id = None

    with pytest.raises(RuntimeError):
        asyncio.get_event_loop().run_until_complete(ctx.reply("hey"))

    ctx.backend.execute_send.assert_not_called()
예제 #3
0
async def _(msg: Message, ctx: Context):
    """
    Asks user for his nickname
    """
    user_info = await get_user_info(ctx)
    await asyncio.wait((ctx.set_state(user_state='name'),
                        ctx.reply(await translate("Введите своё имя", "ru",
                                                  user_info.language))))
예제 #4
0
async def _(upd: Update, ctx: Context):
    if upd.type == UpdateType.MSG:
        user_id = upd.raw['object']['message']['from_id']
        ctx.mgr: Manager = ctx.config['db_manager']
        ctx.user, created = await ctx.mgr.get_or_create(User, id=user_id)
        if upd.receiver_type == ReceiverType.MULTI:
            ctx.chat, chat_created = await ctx.mgr.get_or_create(
                Chat, id=upd.receiver_id - 2 * 10**9)
            ctx.chat_user, chat_user_created = await ctx.mgr.get_or_create(
                ChatUser, user=ctx.user, chat=ctx.chat)
예제 #5
0
def test_set_state():
    storage = {}

    class Storage:
        @staticmethod
        async def save(k, v):
            storage[k] = v

        @staticmethod
        async def load(k, d):
            return storage.get(k, d)

    class App:
        storage = Storage

    ctx = Context(app=App)

    # Happy path
    ctx.group_uid = "*gr"
    ctx.group_state_key = "_sg:*gr"
    ctx.user_uid = "*us"
    ctx.user_state_key = "_su:*us"

    async def test():
        await ctx.set_state(user_state="new_us_state")
        await ctx.set_state(group_state="new_gr_state")

    asyncio.get_event_loop().run_until_complete(test())

    assert storage[ctx.group_state_key] == "new_gr_state"
    assert storage[ctx.user_state_key] == "new_us_state"

    # Wrong update state 1
    ctx.group_uid = ""
    ctx.group_state_key = ""

    async def test():
        with pytest.raises(ValueError):
            await ctx.set_state(group_state="new_new_gr_state")

    asyncio.get_event_loop().run_until_complete(test())

    # Wrong update state 2
    ctx.user_uid = ""
    ctx.user_state_key = ""

    async def test():
        with pytest.raises(ValueError):
            await ctx.set_state(user_state="new_new_us_state")

    asyncio.get_event_loop().run_until_complete(test())

    assert storage["_sg:*gr"] == "new_gr_state"
    assert storage["_su:*us"] == "new_us_state"
예제 #6
0
def test_set_state_non_string():
    storage = {}

    class Storage:
        @staticmethod
        async def save(k, v):
            storage[k] = v

        @staticmethod
        async def load(k, d):
            return storage.get(k, d)

    class App:
        storage = Storage

    ctx = Context(app=App)

    async def test():
        with pytest.raises(ValueError):
            await ctx.set_state(0, None)

        with pytest.raises(ValueError):
            await ctx.set_state(None, 0)

        with pytest.raises(ValueError):
            await ctx.set_state(0, 0)

    asyncio.get_event_loop().run_until_complete(test())
예제 #7
0
def test_context():
    called = []

    class Backend:
        async def perform_send(self, target_id, message, attachments, kwargs):
            called.append(("ps", message))

        async def perform_api_call(self, method, kwargs):
            called.append(("pac", method, kwargs))

    ctx = Context(backend=Backend())
    ctx.default_target_id = 1

    asyncio.get_event_loop().run_until_complete(ctx.reply("hey1"))
    asyncio.get_event_loop().run_until_complete(ctx.send_message(1, "hey2"))
    asyncio.get_event_loop().run_until_complete(ctx.request("a", v="hey3"))

    assert called == [
        ("ps", "hey1"),
        ("ps", "hey2"),
        ("pac", "a", {"v": "hey3"}),
    ]

    called.clear()

    message = "a" * 4096 + "b" * 4096 + "c" * 4096

    asyncio.get_event_loop().run_until_complete(ctx.reply(message))

    assert called == [
        ("ps", message[:4096]),
        ("ps", message[4096: 4096 * 2]),
        ("ps", message[4096 * 2:]),
    ]
예제 #8
0
def test_dynamic_attributes():
    ctx = Context()

    ctx.var1 = "val1"
    ctx.var2 = "val2"

    assert ctx.var1 == "val1"
    assert ctx.var2 == "val2"
    assert ctx.get("var3") is None
    assert ctx.get("var3", "default_value_1") == "default_value_1"
예제 #9
0
def test_replace_method():
    calls = []

    async def replacement(self, message, attachments=(), **kwargs):
        calls.append((message, attachments, kwargs))

    ctx = Context()
    ctx.replace_method("reply", replacement)
    asyncio.get_event_loop().run_until_complete(ctx.reply("hey"))

    assert calls == [("hey", (), {})]

    with pytest.raises(ValueError):
        ctx.replace_method("a" * 64, lambda: 0)
예제 #10
0
def test_check():
    r = Router()
    h = Handler(None, "", "", 0)

    c = Context()
    c.group_state = ""
    c.user_state = ""

    assert r._check(h, None, c)

    c.group_state = ""
    c.user_state = "state"
    assert not r._check(h, None, c)
예제 #11
0
def test_reply_non_str():
    ctx = Context(backend=MagicMock(perform_send=CoroutineMock()))
    ctx.default_target_id = 1
    asyncio.get_event_loop().run_until_complete(ctx.reply(123))
    ctx.backend.perform_send.assert_called_with(1, '123', (), {})