mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 02:03:04 +00:00
Enable Python 3.10 for tests (#719)
* Try to test on Python 3.10 * Prevent to YAML type cast * Bump poetry * Removed async_lru * Disable fail-fast on tests * Fixed typing * Added patch-notes
This commit is contained in:
parent
99c99cec78
commit
7405db2ad3
11 changed files with 36 additions and 81 deletions
|
|
@ -75,4 +75,3 @@ def bot():
|
|||
yield bot
|
||||
finally:
|
||||
Bot.reset_current(token)
|
||||
bot.me.invalidate(bot)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from aiogram import Bot
|
|||
from aiogram.client.session.base import BaseSession
|
||||
from aiogram.methods import TelegramMethod
|
||||
from aiogram.methods.base import Request, Response, TelegramType
|
||||
from aiogram.types import UNSET, ResponseParameters
|
||||
from aiogram.types import UNSET, ResponseParameters, User
|
||||
|
||||
|
||||
class MockedSession(BaseSession):
|
||||
|
|
@ -46,6 +46,14 @@ class MockedBot(Bot):
|
|||
|
||||
def __init__(self, **kwargs):
|
||||
super(MockedBot, self).__init__("42:TEST", session=MockedSession(), **kwargs)
|
||||
self._me = User(
|
||||
id=self.id,
|
||||
is_bot=True,
|
||||
first_name="FirstName",
|
||||
last_name="LastName",
|
||||
username="tbot",
|
||||
language_code="uk-UA",
|
||||
)
|
||||
|
||||
def add_result_for(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -30,25 +30,6 @@ class TestGetMe:
|
|||
assert response == prepare_result.result
|
||||
|
||||
async def test_me_property(self, bot: MockedBot):
|
||||
prepare_result = bot.add_result_for(
|
||||
GetMe, ok=True, result=User(id=42, is_bot=False, first_name="User")
|
||||
)
|
||||
|
||||
response: User = await bot.me()
|
||||
request: Request = bot.get_request()
|
||||
|
||||
assert isinstance(response, User)
|
||||
assert request.method == "getMe"
|
||||
assert request.data == {}
|
||||
assert response == prepare_result.result
|
||||
|
||||
response2: User = await bot.me()
|
||||
assert response2 == response
|
||||
|
||||
response3: User = await bot.me()
|
||||
assert response3 == response
|
||||
assert response2 == response3
|
||||
|
||||
cache_info = bot.me.cache_info()
|
||||
assert cache_info.hits == 2
|
||||
assert cache_info.misses == 1
|
||||
assert response == bot._me
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import pytest
|
|||
from aiogram import F
|
||||
from aiogram.dispatcher.filters import Command, CommandObject
|
||||
from aiogram.dispatcher.filters.command import CommandStart
|
||||
from aiogram.methods import GetMe
|
||||
from aiogram.types import Chat, Message, User
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
|
|
@ -58,10 +57,6 @@ class TestCommandFilter:
|
|||
# TODO: test ignore case
|
||||
# TODO: test ignore mention
|
||||
|
||||
bot.add_result_for(
|
||||
GetMe, ok=True, result=User(id=42, is_bot=True, first_name="The bot", username="tbot")
|
||||
)
|
||||
|
||||
message = Message(
|
||||
message_id=0, text=text, chat=Chat(id=42, type="private"), date=datetime.datetime.now()
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import pytest
|
||||
from async_lru import alru_cache
|
||||
|
||||
from aiogram.utils.deep_linking import (
|
||||
create_start_link,
|
||||
|
|
@ -36,60 +35,39 @@ def wrong_payload_fixture(request):
|
|||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def get_bot_user_fixture(monkeypatch):
|
||||
"""Monkey patching of bot.me calling."""
|
||||
|
||||
@alru_cache()
|
||||
async def get_bot_user_mock(self):
|
||||
from aiogram.types import User
|
||||
|
||||
return User(
|
||||
id=12345678,
|
||||
is_bot=True,
|
||||
first_name="FirstName",
|
||||
last_name="LastName",
|
||||
username="username",
|
||||
language_code="uk-UA",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(MockedBot, "me", get_bot_user_mock)
|
||||
|
||||
|
||||
class TestDeepLinking:
|
||||
async def test_get_start_link(self, bot, payload):
|
||||
async def test_get_start_link(self, bot: MockedBot, payload: str):
|
||||
link = await create_start_link(bot=bot, payload=payload)
|
||||
assert link == f"https://t.me/username?start={payload}"
|
||||
assert link == f"https://t.me/tbot?start={payload}"
|
||||
|
||||
async def test_wrong_symbols(self, bot, wrong_payload):
|
||||
async def test_wrong_symbols(self, bot: MockedBot, wrong_payload: str):
|
||||
with pytest.raises(ValueError):
|
||||
await create_start_link(bot, wrong_payload)
|
||||
|
||||
async def test_get_startgroup_link(self, bot, payload):
|
||||
async def test_get_startgroup_link(self, bot: MockedBot, payload: str):
|
||||
link = await create_startgroup_link(bot, payload)
|
||||
assert link == f"https://t.me/username?startgroup={payload}"
|
||||
assert link == f"https://t.me/tbot?startgroup={payload}"
|
||||
|
||||
async def test_filter_encode_and_decode(self, payload):
|
||||
async def test_filter_encode_and_decode(self, payload: str):
|
||||
encoded = encode_payload(payload)
|
||||
decoded = decode_payload(encoded)
|
||||
assert decoded == str(payload)
|
||||
|
||||
async def test_get_start_link_with_encoding(self, bot, wrong_payload):
|
||||
async def test_get_start_link_with_encoding(self, bot: MockedBot, wrong_payload: str):
|
||||
# define link
|
||||
link = await create_start_link(bot, wrong_payload, encode=True)
|
||||
|
||||
# define reference link
|
||||
encoded_payload = encode_payload(wrong_payload)
|
||||
|
||||
assert link == f"https://t.me/username?start={encoded_payload}"
|
||||
assert link == f"https://t.me/tbot?start={encoded_payload}"
|
||||
|
||||
async def test_64_len_payload(self, bot):
|
||||
async def test_64_len_payload(self, bot: MockedBot):
|
||||
payload = "p" * 64
|
||||
link = await create_start_link(bot, payload)
|
||||
assert link
|
||||
|
||||
async def test_too_long_payload(self, bot):
|
||||
async def test_too_long_payload(self, bot: MockedBot):
|
||||
payload = "p" * 65
|
||||
print(payload, len(payload))
|
||||
with pytest.raises(ValueError):
|
||||
await create_start_link(bot, payload)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue