Handle expected warnings & raise unexpected warnings (#1315)

* chore: replace fixture loop with event_loop

* chore: mark expected warnings

* chore: raise unexpected warnings

* chore: rm unused record

* fix: rm parenthesized context manager

* chore: warnings shall not pass

* chore: replace fixture loop with event_loop

* chore: mark expected warnings

* chore: raise unexpected warnings

* chore: rm unused record

* fix: rm parenthesized context manager

* chore: warnings shall not pass

* Revert "chore: raise unexpected warnings"

This reverts commit 4c91df243d.

* chore: warnings shall not pass v2

* fix: graceful aiohttp session close

* chore: minor typo

* chore: mark expected warnings

* fix: temporary mute ResourceWarning

#1320

* fix: close pool with redis

* chore: code reformat and lint

* chore: simplify tests with fixture

* chore: make aresponses clear

* chore: divide asserts with blank line

* chore: rm duplicated assertions

* chore: rm unnecessary extra

* chore: bump test dependencies

* chore: bump test dependencies (fix)
This commit is contained in:
Oleg A 2023-10-01 15:28:54 +03:00 committed by GitHub
parent 890a57cd15
commit eacea996d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 198 additions and 158 deletions

View file

@ -15,6 +15,26 @@ from aiogram.types import File, PhotoSize
from tests.mocked_bot import MockedBot
@pytest.fixture()
async def bot():
"""Override mocked bot fixture with real bot."""
async with Bot("42:TEST").context() as bot:
yield bot
@pytest.fixture()
def mocked_bot():
"""Mocked bot fixture."""
return MockedBot()
@pytest.fixture()
async def session():
"""Override session fixture."""
async with AiohttpSession() as session:
yield session
class TestBot:
def test_init(self):
bot = Bot("42:TEST")
@ -30,9 +50,7 @@ class TestBot:
assert bot == Bot("42:TEST")
assert bot != "42:TEST"
async def test_emit(self):
bot = Bot("42:TEST")
async def test_emit(self, bot: Bot):
method = GetMe()
with patch(
@ -42,8 +60,7 @@ class TestBot:
await bot(method)
mocked_make_request.assert_awaited_with(bot, method, timeout=None)
async def test_close(self):
session = AiohttpSession()
async def test_close(self, session: AiohttpSession):
bot = Bot("42:TEST", session=session)
await session.create_session()
@ -56,18 +73,23 @@ 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=AsyncMock
target="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:
session = AiohttpSession()
async with Bot("42:TEST", session=session).context(auto_close=close) as bot:
assert isinstance(bot, Bot)
if close:
mocked_close.assert_awaited()
else:
mocked_close.assert_not_awaited()
await session.close()
async def test_download_file(self, aresponses: ResponsesMockServer):
aresponses.add(
aresponses.ANY, aresponses.ANY, "get", aresponses.Response(status=200, body=b"\f" * 10)
method_pattern="get",
response=aresponses.Response(status=200, body=b"\f" * 10),
)
# https://github.com/Tinche/aiofiles#writing-tests-for-aiofiles
@ -77,30 +99,34 @@ class TestBot:
mock_file = MagicMock()
bot = Bot("42:TEST")
with patch("aiofiles.threadpool.sync_open", return_value=mock_file):
await bot.download_file("TEST", "file.png")
mock_file.write.assert_called_once_with(b"\f" * 10)
async def test_download_file_default_destination(self, aresponses: ResponsesMockServer):
bot = Bot("42:TEST")
async with Bot("42:TEST").context() as bot:
with patch("aiofiles.threadpool.sync_open", return_value=mock_file):
await bot.download_file("TEST", "file.png")
mock_file.write.assert_called_once_with(b"\f" * 10)
async def test_download_file_default_destination(
self,
bot: Bot,
aresponses: ResponsesMockServer,
):
aresponses.add(
aresponses.ANY, aresponses.ANY, "get", aresponses.Response(status=200, body=b"\f" * 10)
method_pattern="get",
response=aresponses.Response(status=200, body=b"\f" * 10),
)
result = await bot.download_file("TEST")
assert isinstance(result, io.BytesIO)
assert result.read() == b"\f" * 10
async def test_download_file_custom_destination(self, aresponses: ResponsesMockServer):
bot = Bot("42:TEST")
async def test_download_file_custom_destination(
self,
bot: Bot,
aresponses: ResponsesMockServer,
):
aresponses.add(
aresponses.ANY, aresponses.ANY, "get", aresponses.Response(status=200, body=b"\f" * 10)
method_pattern="get",
response=aresponses.Response(status=200, body=b"\f" * 10),
)
custom = io.BytesIO()
result = await bot.download_file("TEST", custom)
@ -109,19 +135,19 @@ class TestBot:
assert result is custom
assert result.read() == b"\f" * 10
async def test_download(self, bot: MockedBot, aresponses: ResponsesMockServer):
bot.add_result_for(
async def test_download(self, mocked_bot: MockedBot):
mocked_bot.add_result_for(
GetFile, ok=True, result=File(file_id="file id", file_unique_id="file id")
)
bot.add_result_for(
mocked_bot.add_result_for(
GetFile, ok=True, result=File(file_id="file id", file_unique_id="file id")
)
assert await bot.download(File(file_id="file id", file_unique_id="file id"))
assert await bot.download("file id")
assert await mocked_bot.download(File(file_id="file id", file_unique_id="file id"))
assert await mocked_bot.download("file id")
with pytest.raises(TypeError):
await bot.download(
await mocked_bot.download(
[PhotoSize(file_id="file id", file_unique_id="file id", width=123, height=123)]
)

View file

@ -24,61 +24,53 @@ class BareInputFile(InputFile):
class TestAiohttpSession:
async def test_create_session(self):
session = AiohttpSession()
assert session._session is None
aiohttp_session = await session.create_session()
assert session._session is not None
assert isinstance(aiohttp_session, aiohttp.ClientSession)
await session.close()
async def test_create_proxy_session(self):
session = AiohttpSession(
proxy=("socks5://proxy.url/", aiohttp.BasicAuth("login", "password", "encoding"))
)
auth = aiohttp.BasicAuth("login", "password", "encoding")
async with AiohttpSession(proxy=("socks5://proxy.url/", auth)) as session:
assert session._connector_type == aiohttp_socks.ProxyConnector
assert session._connector_type == aiohttp_socks.ProxyConnector
assert isinstance(session._connector_init, dict)
assert session._connector_init["proxy_type"] is aiohttp_socks.ProxyType.SOCKS5
assert isinstance(session._connector_init, dict)
assert session._connector_init["proxy_type"] is aiohttp_socks.ProxyType.SOCKS5
aiohttp_session = await session.create_session()
assert isinstance(aiohttp_session.connector, aiohttp_socks.ProxyConnector)
aiohttp_session = await session.create_session()
assert isinstance(aiohttp_session.connector, aiohttp_socks.ProxyConnector)
async def test_create_proxy_session_proxy_url(self):
session = AiohttpSession(proxy="socks4://proxy.url/")
async with AiohttpSession(proxy="socks4://proxy.url/") as session:
assert isinstance(session.proxy, str)
assert isinstance(session.proxy, str)
assert isinstance(session._connector_init, dict)
assert session._connector_init["proxy_type"] is aiohttp_socks.ProxyType.SOCKS4
assert isinstance(session._connector_init, dict)
assert session._connector_init["proxy_type"] is aiohttp_socks.ProxyType.SOCKS4
aiohttp_session = await session.create_session()
assert isinstance(aiohttp_session.connector, aiohttp_socks.ProxyConnector)
aiohttp_session = await session.create_session()
assert isinstance(aiohttp_session.connector, aiohttp_socks.ProxyConnector)
async def test_create_proxy_session_chained_proxies(self):
session = AiohttpSession(
proxy=[
"socks4://proxy.url/",
"socks5://proxy.url/",
"http://user:password@127.0.0.1:3128",
]
)
proxy_chain = [
"socks4://proxy.url/",
"socks5://proxy.url/",
"http://user:password@127.0.0.1:3128",
]
async with AiohttpSession(proxy=proxy_chain) as session:
assert isinstance(session.proxy, list)
assert isinstance(session.proxy, list)
assert isinstance(session._connector_init, dict)
assert isinstance(session._connector_init, dict)
assert isinstance(session._connector_init["proxy_infos"], list)
assert isinstance(session._connector_init["proxy_infos"][0], aiohttp_socks.ProxyInfo)
proxy_infos = session._connector_init["proxy_infos"]
assert isinstance(proxy_infos, list)
assert isinstance(proxy_infos[0], aiohttp_socks.ProxyInfo)
assert proxy_infos[0].proxy_type is aiohttp_socks.ProxyType.SOCKS4
assert proxy_infos[1].proxy_type is aiohttp_socks.ProxyType.SOCKS5
assert proxy_infos[2].proxy_type is aiohttp_socks.ProxyType.HTTP
assert (
session._connector_init["proxy_infos"][0].proxy_type is aiohttp_socks.ProxyType.SOCKS4
)
assert (
session._connector_init["proxy_infos"][1].proxy_type is aiohttp_socks.ProxyType.SOCKS5
)
assert session._connector_init["proxy_infos"][2].proxy_type is aiohttp_socks.ProxyType.HTTP
aiohttp_session = await session.create_session()
assert isinstance(aiohttp_session.connector, aiohttp_socks.ChainProxyConnector)
aiohttp_session = await session.create_session()
assert isinstance(aiohttp_session.connector, aiohttp_socks.ChainProxyConnector)
async def test_reset_connector(self):
session = AiohttpSession()
@ -93,6 +85,7 @@ class TestAiohttpSession:
assert session._should_reset_connector
await session.create_session()
assert session._should_reset_connector is False
await session.close()
async def test_close_session(self):
@ -170,54 +163,53 @@ class TestAiohttpSession:
),
)
session = AiohttpSession()
async with AiohttpSession() as session:
class TestMethod(TelegramMethod[int]):
__returning__ = int
__api_method__ = "method"
class TestMethod(TelegramMethod[int]):
__returning__ = int
__api_method__ = "method"
call = TestMethod()
call = TestMethod()
result = await session.make_request(bot, call)
assert isinstance(result, int)
assert result == 42
result = await session.make_request(bot, call)
assert isinstance(result, int)
assert result == 42
@pytest.mark.parametrize("error", [ClientError("mocked"), asyncio.TimeoutError()])
async def test_make_request_network_error(self, error):
bot = Bot("42:TEST")
async def side_effect(*args, **kwargs):
raise error
with patch(
"aiohttp.client.ClientSession._request",
new_callable=AsyncMock,
side_effect=side_effect,
):
with pytest.raises(TelegramNetworkError):
await bot.get_me()
async with Bot("42:TEST").context() as bot:
with patch(
"aiohttp.client.ClientSession._request",
new_callable=AsyncMock,
side_effect=side_effect,
):
with pytest.raises(TelegramNetworkError):
await bot.get_me()
async def test_stream_content(self, aresponses: ResponsesMockServer):
aresponses.add(
aresponses.ANY, aresponses.ANY, "get", aresponses.Response(status=200, body=b"\f" * 10)
)
session = AiohttpSession()
stream = session.stream_content(
"https://www.python.org/static/img/python-logo.png",
timeout=5,
chunk_size=1,
raise_for_status=True,
)
assert isinstance(stream, AsyncGenerator)
async with AiohttpSession() as session:
stream = session.stream_content(
"https://www.python.org/static/img/python-logo.png",
timeout=5,
chunk_size=1,
raise_for_status=True,
)
assert isinstance(stream, AsyncGenerator)
size = 0
async for chunk in stream:
assert isinstance(chunk, bytes)
chunk_size = len(chunk)
assert chunk_size == 1
size += chunk_size
assert size == 10
size = 0
async for chunk in stream:
assert isinstance(chunk, bytes)
chunk_size = len(chunk)
assert chunk_size == 1
size += chunk_size
assert size == 10
async def test_stream_content_404(self, aresponses: ResponsesMockServer):
aresponses.add(
@ -229,29 +221,30 @@ class TestAiohttpSession:
body=b"File not found",
),
)
session = AiohttpSession()
stream = session.stream_content(
"https://www.python.org/static/img/python-logo.png",
timeout=5,
chunk_size=1,
raise_for_status=True,
)
async with AiohttpSession() as session:
stream = session.stream_content(
"https://www.python.org/static/img/python-logo.png",
timeout=5,
chunk_size=1,
raise_for_status=True,
)
with pytest.raises(ClientError):
async for _ in stream:
...
with pytest.raises(ClientError):
async for _ in stream:
...
async def test_context_manager(self):
session = AiohttpSession()
assert isinstance(session, AsyncContextManager)
async with AiohttpSession() as session:
assert isinstance(session, AsyncContextManager)
with patch(
"aiogram.client.session.aiohttp.AiohttpSession.create_session",
new_callable=AsyncMock,
) as mocked_create_session, patch(
"aiogram.client.session.aiohttp.AiohttpSession.close", new_callable=AsyncMock
) as mocked_close:
async with session as ctx:
assert session == ctx
mocked_close.assert_awaited_once()
mocked_create_session.assert_awaited_once()
with patch(
"aiogram.client.session.aiohttp.AiohttpSession.create_session",
new_callable=AsyncMock,
) as mocked_create_session, patch(
"aiogram.client.session.aiohttp.AiohttpSession.close",
new_callable=AsyncMock,
) as mocked_close:
async with session as ctx:
assert session == ctx
mocked_close.assert_awaited_once()
mocked_create_session.assert_awaited_once()