mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 18:19:34 +00:00
Tests on Python 3.11 (#1044)
* Try to use Python 3.11 * Remove `asynctest` dependency * Fixed aiofiles tests * Added changelog
This commit is contained in:
parent
c7a85de579
commit
6db3778c6f
12 changed files with 36 additions and 88 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import io
|
||||
import os
|
||||
from tempfile import mkstemp
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import aiofiles
|
||||
import pytest
|
||||
|
|
@ -13,12 +14,6 @@ from aiogram.methods import GetFile, GetMe
|
|||
from aiogram.types import File, PhotoSize
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
try:
|
||||
from asynctest import CoroutineMock, patch
|
||||
except ImportError:
|
||||
from unittest.mock import AsyncMock as CoroutineMock # type: ignore
|
||||
from unittest.mock import patch
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
|
|
@ -44,7 +39,7 @@ class TestBot:
|
|||
|
||||
with patch(
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.make_request",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_make_request:
|
||||
await bot(method)
|
||||
mocked_make_request.assert_awaited_with(bot, method, timeout=None)
|
||||
|
|
@ -55,7 +50,7 @@ class TestBot:
|
|||
await session.create_session()
|
||||
|
||||
with patch(
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.close", new_callable=CoroutineMock
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.close", new_callable=AsyncMock
|
||||
) as mocked_close:
|
||||
await bot.session.close()
|
||||
mocked_close.assert_awaited()
|
||||
|
|
@ -63,7 +58,7 @@ class TestBot:
|
|||
@pytest.mark.parametrize("close", [True, False])
|
||||
async def test_context_manager(self, close: bool):
|
||||
with patch(
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.close", new_callable=CoroutineMock
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.close", new_callable=AsyncMock
|
||||
) as mocked_close:
|
||||
async with Bot("42:TEST", session=AiohttpSession()).context(auto_close=close) as bot:
|
||||
assert isinstance(bot, Bot)
|
||||
|
|
@ -78,11 +73,11 @@ class TestBot:
|
|||
)
|
||||
|
||||
# https://github.com/Tinche/aiofiles#writing-tests-for-aiofiles
|
||||
aiofiles.threadpool.wrap.register(CoroutineMock)(
|
||||
lambda *args, **kwargs: aiofiles.threadpool.AsyncBufferedIOBase(*args, **kwargs)
|
||||
aiofiles.threadpool.wrap.register(MagicMock)(
|
||||
lambda *args, **kwargs: aiofiles.threadpool.binary.AsyncBufferedIOBase(*args, **kwargs)
|
||||
)
|
||||
|
||||
mock_file = CoroutineMock()
|
||||
mock_file = MagicMock()
|
||||
|
||||
bot = Bot("42:TEST")
|
||||
with patch("aiofiles.threadpool.sync_open", return_value=mock_file):
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import asyncio
|
||||
from typing import AsyncContextManager, AsyncGenerator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import aiohttp_socks
|
||||
import pytest
|
||||
|
|
@ -14,12 +15,6 @@ from aiogram.methods import Request, TelegramMethod
|
|||
from aiogram.types import UNSET, InputFile
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
try:
|
||||
from asynctest import CoroutineMock, patch
|
||||
except ImportError:
|
||||
from unittest.mock import AsyncMock as CoroutineMock # type: ignore
|
||||
from unittest.mock import patch
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
|
|
@ -106,7 +101,7 @@ class TestAiohttpSession:
|
|||
session = AiohttpSession()
|
||||
await session.create_session()
|
||||
|
||||
with patch("aiohttp.ClientSession.close", new=CoroutineMock()) as mocked_close:
|
||||
with patch("aiohttp.ClientSession.close", new=AsyncMock()) as mocked_close:
|
||||
await session.close()
|
||||
mocked_close.assert_called_once()
|
||||
|
||||
|
|
@ -184,7 +179,7 @@ class TestAiohttpSession:
|
|||
|
||||
with patch(
|
||||
"aiohttp.client.ClientSession._request",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
side_effect=side_effect,
|
||||
):
|
||||
with pytest.raises(TelegramNetworkError):
|
||||
|
|
@ -215,9 +210,9 @@ class TestAiohttpSession:
|
|||
|
||||
with patch(
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.create_session",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_create_session, patch(
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.close", new_callable=CoroutineMock
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.close", new_callable=AsyncMock
|
||||
) as mocked_close:
|
||||
async with session as ctx:
|
||||
assert session == ctx
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import datetime
|
||||
import json
|
||||
from typing import AsyncContextManager, AsyncGenerator, Optional
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
|
@ -25,12 +26,6 @@ from aiogram.methods import DeleteMessage, GetMe, TelegramMethod
|
|||
from aiogram.types import UNSET, User
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
try:
|
||||
from asynctest import CoroutineMock, patch
|
||||
except ImportError:
|
||||
from unittest.mock import AsyncMock as CoroutineMock # type: ignore
|
||||
from unittest.mock import patch
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
|
|
@ -227,7 +222,7 @@ class TestBaseSession:
|
|||
|
||||
with patch(
|
||||
"tests.test_api.test_client.test_session.test_base_session.CustomSession.close",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_close:
|
||||
async with session as ctx:
|
||||
assert session == ctx
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import time
|
|||
import warnings
|
||||
from collections import Counter
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
|
@ -33,12 +34,6 @@ from aiogram.types import (
|
|||
from aiogram.types.error_event import ErrorEvent
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
try:
|
||||
from asynctest import CoroutineMock, patch
|
||||
except ImportError:
|
||||
from unittest.mock import AsyncMock as CoroutineMock # type: ignore
|
||||
from unittest.mock import patch
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
|
|
@ -176,7 +171,7 @@ class TestDispatcher:
|
|||
bot.add_result_for(GetUpdates, ok=False, error_code=500, description="restarting")
|
||||
with patch(
|
||||
"aiogram.utils.backoff.Backoff.asleep",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_asleep:
|
||||
assert isinstance(await anext(listen), Update)
|
||||
assert mocked_asleep.awaited
|
||||
|
|
@ -594,7 +589,7 @@ class TestDispatcher:
|
|||
|
||||
with patch(
|
||||
"aiogram.dispatcher.dispatcher.Dispatcher.silent_call_request",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_silent_call_request:
|
||||
result = await dispatcher._process_update(bot=bot, update=Update(update_id=42))
|
||||
print(result)
|
||||
|
|
@ -620,7 +615,7 @@ class TestDispatcher:
|
|||
yield Update(update_id=42)
|
||||
|
||||
with patch(
|
||||
"aiogram.dispatcher.dispatcher.Dispatcher._process_update", new_callable=CoroutineMock
|
||||
"aiogram.dispatcher.dispatcher.Dispatcher._process_update", new_callable=AsyncMock
|
||||
) as mocked_process_update, patch(
|
||||
"aiogram.dispatcher.dispatcher.Dispatcher._listen_updates"
|
||||
) as patched_listen_updates:
|
||||
|
|
@ -682,11 +677,11 @@ class TestDispatcher:
|
|||
yield Update(update_id=42)
|
||||
|
||||
with patch(
|
||||
"aiogram.dispatcher.dispatcher.Dispatcher._process_update", new_callable=CoroutineMock
|
||||
"aiogram.dispatcher.dispatcher.Dispatcher._process_update", new_callable=AsyncMock
|
||||
) as mocked_process_update, patch(
|
||||
"aiogram.dispatcher.router.Router.emit_startup", new_callable=CoroutineMock
|
||||
"aiogram.dispatcher.router.Router.emit_startup", new_callable=AsyncMock
|
||||
) as mocked_emit_startup, patch(
|
||||
"aiogram.dispatcher.router.Router.emit_shutdown", new_callable=CoroutineMock
|
||||
"aiogram.dispatcher.router.Router.emit_shutdown", new_callable=AsyncMock
|
||||
) as mocked_emit_shutdown, patch(
|
||||
"aiogram.dispatcher.dispatcher.Dispatcher._listen_updates"
|
||||
) as patched_listen_updates:
|
||||
|
|
@ -722,7 +717,7 @@ class TestDispatcher:
|
|||
|
||||
with patch(
|
||||
"aiogram.dispatcher.dispatcher.Dispatcher.silent_call_request",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_silent_call_request:
|
||||
response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=0.1)
|
||||
assert response is None
|
||||
|
|
|
|||
|
|
@ -1,17 +1,12 @@
|
|||
import functools
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.dispatcher.event.event import EventObserver
|
||||
from aiogram.dispatcher.event.handler import HandlerObject
|
||||
|
||||
try:
|
||||
from asynctest import CoroutineMock, patch
|
||||
except ImportError:
|
||||
from unittest.mock import AsyncMock as CoroutineMock # type: ignore
|
||||
from unittest.mock import patch
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
|
|
@ -54,7 +49,7 @@ class TestEventObserver:
|
|||
|
||||
with patch(
|
||||
"aiogram.dispatcher.event.handler.CallableMixin.call",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_my_handler:
|
||||
results = await observer.trigger("test")
|
||||
assert results is None
|
||||
|
|
|
|||
|
|
@ -1,15 +1,10 @@
|
|||
from typing import Awaitable
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.filters import Filter
|
||||
|
||||
try:
|
||||
from asynctest import CoroutineMock, patch
|
||||
except ImportError:
|
||||
from unittest.mock import AsyncMock as CoroutineMock # type: ignore
|
||||
from unittest.mock import patch
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
|
|
@ -26,7 +21,7 @@ class TestBaseFilter:
|
|||
|
||||
with patch(
|
||||
"tests.test_filters.test_base.MyFilter.__call__",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_call:
|
||||
call = my_filter(event="test")
|
||||
await call
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import asyncio
|
||||
import time
|
||||
from datetime import datetime
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
|
@ -10,13 +11,7 @@ from aiogram.types import Chat, Message, User
|
|||
from aiogram.utils.chat_action import ChatActionMiddleware, ChatActionSender
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
try:
|
||||
from asynctest import CoroutineMock, patch
|
||||
except ImportError:
|
||||
from unittest.mock import AsyncMock as CoroutineMock # type: ignore
|
||||
from unittest.mock import patch
|
||||
|
||||
pytestmarm = pytest.mark.asyncio
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestChatActionSender:
|
||||
|
|
@ -55,7 +50,7 @@ class TestChatActionSender:
|
|||
async def test_worker(self, bot: Bot):
|
||||
with patch(
|
||||
"aiogram.client.bot.Bot.send_chat_action",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_send_chat_action:
|
||||
async with ChatActionSender.typing(
|
||||
bot=bot, chat_id=42, interval=0.01, initial_sleep=0
|
||||
|
|
@ -101,10 +96,10 @@ class TestChatActionMiddleware:
|
|||
middleware = ChatActionMiddleware()
|
||||
with patch(
|
||||
"aiogram.utils.chat_action.ChatActionSender._run",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_run, patch(
|
||||
"aiogram.utils.chat_action.ChatActionSender._stop",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_stop:
|
||||
data = {"handler": HandlerObject(callback=handler1), "bot": bot}
|
||||
message = Message(
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import time
|
|||
from asyncio import Event
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from aiohttp import web
|
||||
|
|
@ -21,12 +22,6 @@ from aiogram.webhook.aiohttp_server import (
|
|||
from aiogram.webhook.security import IPFilter
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
try:
|
||||
from asynctest import CoroutineMock, patch
|
||||
except ImportError:
|
||||
from unittest.mock import AsyncMock as CoroutineMock # type: ignore
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
class TestAiohttpServer:
|
||||
def test_setup_application(self):
|
||||
|
|
@ -110,7 +105,7 @@ class TestSimpleRequestHandler:
|
|||
handler.handle_in_background = True
|
||||
with patch(
|
||||
"aiogram.dispatcher.dispatcher.Dispatcher.silent_call_request",
|
||||
new_callable=CoroutineMock,
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_silent_call_request:
|
||||
handler_event.clear()
|
||||
resp = await self.make_reqest(client=client)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue