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)]
)