Exemplo n.º 1
0
def test_upload_attachment_error_retry(
    mock_make_attachment,
    mock_upload_file_to_vk,
    mock_request,
):
    mock_request.side_effect = [
        {"upload_url": "_"},
        RequestException(None, None, None, {"error_code": 1}),
        {"upload_url": "_"},
        "ok",
    ]

    mock_upload_file_to_vk.side_effect = [
        "ok",
        "ok",
    ]

    mock_make_attachment.return_value = "ok"

    vkontakte = VkontakteLongpoll("token")

    result = asyncio.get_event_loop().run_until_complete(
        vkontakte.upload_attachment(Attachment.new(b""), peer_id=123)
    )

    assert result == "ok"
Exemplo n.º 2
0
def test_execute_send_exception():
    vkontakte = VkontakteLongpoll(token="token")

    attachment = vkontakte._make_attachment(ATTACHMENTS["image"])
    attachment = attachment._replace(id=None)

    with pytest.raises(ValueError):
        asyncio.get_event_loop().run_until_complete(
            vkontakte.execute_send(1, "text", attachment, {}))
Exemplo n.º 3
0
def test_execute_request():
    vkontakte = VkontakteLongpoll(token="token")

    async def req(method, kwargs):
        assert method == "method"
        assert kwargs["arg"] == "val"
        return 1
    vkontakte._request = req

    result = asyncio.get_event_loop().run_until_complete(
        vkontakte.execute_request("method", {"arg": "val"})
    )

    assert result == 1
Exemplo n.º 4
0
def test_execute_send_string():
    vkontakte = VkontakteLongpoll(token="token")

    async def req(method, kwargs):
        assert method == "messages.send"
        assert kwargs["attachment"] == "hey,hoy"
        return 1
    vkontakte._request = req

    result = asyncio.get_event_loop().run_until_complete(
        vkontakte.execute_send(1, "text", ("hey", "hoy"), {})
    )

    assert result == 1
Exemplo n.º 5
0
    async def test():
        vkontakte = VkontakteLongpoll(token="token", session=aiohttp.ClientSession())

        vkontakte.longpoll_data = {
            "ts": "ts",
            "key": "key",
            "server": "server",
        }

        await vkontakte.acquire_updates(None)

        with pytest.raises(asyncio.CancelledError):
            await vkontakte.acquire_updates(None)

        await vkontakte.acquire_updates(None)
Exemplo n.º 6
0
    async def test():
        vkontakte = VkontakteLongpoll(token="token", session=aiohttp.ClientSession())

        tasks = [VKRequest("m1", {}), VKRequest("m2", {})]

        await vkontakte._execute_loop_perform_execute("code", tasks)

        assert tasks[0].done()
        assert tasks[1].done()
        assert tasks[0].result() == 1
        with pytest.raises(RequestException):
            assert tasks[1].result()

        tasks = [VKRequest("m1", {}), VKRequest("m2", {})]

        await vkontakte._execute_loop_perform_execute("code", tasks)

        assert tasks[0].done()
        assert tasks[1].done()
        assert tasks[0].result() == 1
        with pytest.raises(RequestException):
            assert tasks[1].result()

        tasks = [VKRequest("m1", {}), VKRequest("m2", {})]

        tasks[1].set_exception(asyncio.CancelledError)

        await vkontakte._execute_loop_perform_execute("code", tasks)

        assert tasks[0].done()
        assert tasks[1].done()
        assert tasks[0].result() == 1
        with pytest.raises(asyncio.CancelledError):
            assert tasks[1].result()
Exemplo n.º 7
0
    async def test():
        vkontakte = VkontakteLongpoll(token="token", session=aiohttp.ClientSession())

        assert await vkontakte.raw_request("method1", {"arg": "val1"}) == 1

        with pytest.raises(RequestException):
            await vkontakte.raw_request("method2", {"arg": "val2"})
Exemplo n.º 8
0
def test_execute_send_sticker():
    vkontakte = VkontakteLongpoll(token="token")

    async def req(method, kwargs):
        assert method == "messages.send"
        assert "attachment" not in kwargs
        assert kwargs["sticker_id"] == "123"
        return 1
    vkontakte._request = req

    sticker_attachment = Attachment.existing("123", "sticker")

    result = asyncio.get_event_loop().run_until_complete(
        vkontakte.execute_send(1, "text", sticker_attachment, {})
    )

    assert result == 1
Exemplo n.º 9
0
def test_upload_attachment_error_no_retry(
    mock_upload_file_to_vk,
    mock_request,
):
    mock_request.side_effect = [
        {"upload_url": "_"},
        RequestException(None, None, None, {"error_code": 1}),
    ]

    mock_upload_file_to_vk.side_effect = [
        "ok",
    ]

    vkontakte = VkontakteLongpoll("token")

    with pytest.raises(RequestException):
        asyncio.get_event_loop().run_until_complete(
            vkontakte.upload_attachment(Attachment.new(b""))
        )
Exemplo n.º 10
0
    async def test():
        vkontakte = VkontakteLongpoll(token="token", session=aiohttp.ClientSession())

        for k, v in ATTACHMENTS.items():
            attachment = vkontakte._make_attachment(v)

            if k == "graffiti":
                assert attachment.file is None
                continue

            assert attachment.type == k
            assert attachment.file is None

            if k in ("video", "graffiti"):
                continue

            assert await attachment.get_file() == "content"

        assert next(mock_read.side_effect, None) is None
Exemplo n.º 11
0
def test_execute_send_new():
    vkontakte = VkontakteLongpoll(token="token")

    async def _upl_att(attachment, peer_id):
        return attachment._replace(id=1, raw={"ok": "ok"})
    vkontakte.upload_attachment = _upl_att

    async def req(method, kwargs):
        assert method == "messages.send"
        assert kwargs["attachment"] == "1"
        return 1
    vkontakte._request = req

    attachment = Attachment.new(b"content", "image")

    result = asyncio.get_event_loop().run_until_complete(
        vkontakte.execute_send(1, "text", attachment, {})
    )

    assert result == 1
Exemplo n.º 12
0
    async def test():
        vkontakte = VkontakteLongpoll(token="token", session=aiohttp.ClientSession())

        assert await vkontakte.resolve_screen_name("durov") == data
        assert await vkontakte.resolve_screen_name("durov") == data

        NAIVE_CACHE.update({i: None for i in range(500_000)})

        assert await vkontakte.resolve_screen_name("krukov") == data

        assert len(NAIVE_CACHE) == 1
        assert next(mock_request.side_effect, None) is None
Exemplo n.º 13
0
 async def test():
     vkontakte = VkontakteLongpoll(token="token", session=aiohttp.ClientSession())
     result = await vkontakte._upload_file_to_vk("url", {"file": "file"})
     assert result == {"r": "ok"}
Exemplo n.º 14
0
 async def test():
     vkontakte = VkontakteLongpoll(token="token", session=aiohttp.ClientSession())
     assert await vkontakte.resolve_screen_name("asdjfgakyuhagkuf") == {}
Exemplo n.º 15
0
def test_no_token():
    with pytest.raises(ValueError):
        VkontakteLongpoll("")