Backport text decorations. Improve timeouts in tests. Improve TelegramAPIServer

This commit is contained in:
Alex Root Junior 2021-01-24 23:12:07 +02:00
parent 4292b3934f
commit 1a185928a2
11 changed files with 82 additions and 48 deletions

View file

@ -1,4 +1,4 @@
from aiogram.client.telegram import PRODUCTION
from aiogram.client.telegram import PRODUCTION, TelegramAPIServer
class TestAPIServer:
@ -9,3 +9,13 @@ class TestAPIServer:
def test_file_url(self):
file_url = PRODUCTION.file_url(token="42:TEST", path="path")
assert file_url == "https://api.telegram.org/file/bot42:TEST/path"
def test_from_base(self):
local_server = TelegramAPIServer.from_base("http://localhost:8081", is_local=True)
method_url = local_server.api_url("42:TEST", method="apiMethod")
file_url = local_server.file_url(token="42:TEST", path="path")
assert method_url == "http://localhost:8081/bot42:TEST/apiMethod"
assert file_url == "http://localhost:8081/file/bot42:TEST/path"
assert local_server.is_local

View file

@ -1,4 +1,5 @@
import pytest
from aiogram.methods import Close, Request
from tests.mocked_bot import MockedBot

View file

@ -1,4 +1,5 @@
import pytest
from aiogram.methods import CopyMessage, Request
from aiogram.types import MessageId
from tests.mocked_bot import MockedBot
@ -10,7 +11,9 @@ class TestCopyMessage:
prepare_result = bot.add_result_for(CopyMessage, ok=True, result=MessageId(message_id=42))
response: MessageId = await CopyMessage(
chat_id=42, from_chat_id=42, message_id=42,
chat_id=42,
from_chat_id=42,
message_id=42,
)
request: Request = bot.get_request()
assert request.method == "copyMessage"
@ -22,7 +25,9 @@ class TestCopyMessage:
prepare_result = bot.add_result_for(CopyMessage, ok=True, result=MessageId(message_id=42))
response: MessageId = await bot.copy_message(
chat_id=42, from_chat_id=42, message_id=42,
chat_id=42,
from_chat_id=42,
message_id=42,
)
request: Request = bot.get_request()
assert request.method == "copyMessage"

View file

@ -1,4 +1,5 @@
import pytest
from aiogram.methods import LogOut, Request
from tests.mocked_bot import MockedBot

View file

@ -1,4 +1,5 @@
import pytest
from aiogram.methods import Request, UnpinAllChatMessages
from tests.mocked_bot import MockedBot
@ -8,7 +9,9 @@ class TestUnpinAllChatMessages:
async def test_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(UnpinAllChatMessages, ok=True, result=True)
response: bool = await UnpinAllChatMessages(chat_id=42,)
response: bool = await UnpinAllChatMessages(
chat_id=42,
)
request: Request = bot.get_request()
assert request.method == "unpinAllChatMessages"
# assert request.data == {}
@ -18,7 +21,9 @@ class TestUnpinAllChatMessages:
async def test_bot_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(UnpinAllChatMessages, ok=True, result=True)
response: bool = await bot.unpin_all_chat_messages(chat_id=42,)
response: bool = await bot.unpin_all_chat_messages(
chat_id=42,
)
request: Request = bot.get_request()
assert request.method == "unpinAllChatMessages"
# assert request.data == {}

View file

@ -37,12 +37,12 @@ except ImportError:
async def simple_message_handler(message: Message):
await asyncio.sleep(1.5)
await asyncio.sleep(0.2)
return message.answer("ok")
async def invalid_message_handler(message: Message):
await asyncio.sleep(1.5)
await asyncio.sleep(0.2)
raise Exception(42)
@ -578,21 +578,11 @@ class TestDispatcher:
dispatcher = Dispatcher()
dispatcher.message.register(simple_message_handler)
response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=2)
response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=0.3)
assert isinstance(response, dict)
assert response["method"] == "sendMessage"
assert response["text"] == "ok"
# @pytest.mark.asyncio
# async def test_feed_webhook_update_fast_process_error(self, bot: MockedBot):
# dispatcher = Dispatcher()
# dispatcher.message_handler.register(invalid_message_handler)
#
# response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=2)
# assert isinstance(response, dict)
# assert response["method"] == "sendMessage"
# assert response["text"] == "ok"
@pytest.mark.asyncio
async def test_feed_webhook_update_slow_process(self, bot: MockedBot, recwarn):
warnings.simplefilter("always")
@ -604,9 +594,9 @@ class TestDispatcher:
"aiogram.dispatcher.dispatcher.Dispatcher._silent_call_request",
new_callable=CoroutineMock,
) as mocked_silent_call_request:
response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=1)
response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=0.1)
assert response is None
await asyncio.sleep(1)
await asyncio.sleep(0.2)
mocked_silent_call_request.assert_awaited()
@pytest.mark.asyncio
@ -616,9 +606,9 @@ class TestDispatcher:
dispatcher = Dispatcher()
dispatcher.message.register(invalid_message_handler)
response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=1)
response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=0.1)
assert response is None
await asyncio.sleep(1)
await asyncio.sleep(0.1)
log_records = [rec.message for rec in caplog.records]
assert "Cause exception while process update" in log_records[0]

View file

@ -31,13 +31,13 @@ class TestMarkdown:
[text, ("test", "test"), None, "test test"],
[bold, ("test", "test"), " ", "*test test*"],
[hbold, ("test", "test"), " ", "<b>test test</b>"],
[italic, ("test", "test"), " ", "_test test_\r"],
[italic, ("test", "test"), " ", "_\rtest test_\r"],
[hitalic, ("test", "test"), " ", "<i>test test</i>"],
[code, ("test", "test"), " ", "`test test`"],
[hcode, ("test", "test"), " ", "<code>test test</code>"],
[pre, ("test", "test"), " ", "```test test```"],
[hpre, ("test", "test"), " ", "<pre>test test</pre>"],
[underline, ("test", "test"), " ", "__test test__"],
[underline, ("test", "test"), " ", "__\rtest test__\r"],
[hunderline, ("test", "test"), " ", "<u>test test</u>"],
[strikethrough, ("test", "test"), " ", "~test test~"],
[hstrikethrough, ("test", "test"), " ", "<s>test test</s>"],

View file

@ -53,7 +53,7 @@ class TestTextDecoration:
'<a href="https://aiogram.dev">test</a>',
],
[markdown_decoration, MessageEntity(type="bold", offset=0, length=5), "*test*"],
[markdown_decoration, MessageEntity(type="italic", offset=0, length=5), "_test_\r"],
[markdown_decoration, MessageEntity(type="italic", offset=0, length=5), "_\rtest_\r"],
[markdown_decoration, MessageEntity(type="code", offset=0, length=5), "`test`"],
[markdown_decoration, MessageEntity(type="pre", offset=0, length=5), "```test```"],
[
@ -61,7 +61,11 @@ class TestTextDecoration:
MessageEntity(type="pre", offset=0, length=5, language="python"),
"```python\ntest\n```",
],
[markdown_decoration, MessageEntity(type="underline", offset=0, length=5), "__test__"],
[
markdown_decoration,
MessageEntity(type="underline", offset=0, length=5),
"__\rtest__\r",
],
[
markdown_decoration,
MessageEntity(type="strikethrough", offset=0, length=5),
@ -210,7 +214,7 @@ class TestTextDecoration:
[
html_decoration,
"test te👍🏿st test",
[MessageEntity(type="bold", offset=5, length=6, url=None, user=None)],
[MessageEntity(type="bold", offset=5, length=8, url=None, user=None)],
"test <b>te👍🏿st</b> test",
],
[