mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
chore: simplify tests with fixture
This commit is contained in:
parent
5c4f4c58c3
commit
8076cc2bf7
1 changed files with 78 additions and 51 deletions
|
|
@ -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,40 +50,41 @@ class TestBot:
|
|||
assert bot == Bot("42:TEST")
|
||||
assert bot != "42:TEST"
|
||||
|
||||
async def test_emit(self):
|
||||
async with Bot("42:TEST").context() as bot:
|
||||
method = GetMe()
|
||||
async def test_emit(self, bot: Bot):
|
||||
method = GetMe()
|
||||
|
||||
with patch(
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.make_request",
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_make_request:
|
||||
await bot(method)
|
||||
mocked_make_request.assert_awaited_with(bot, method, timeout=None)
|
||||
with patch(
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.make_request",
|
||||
new_callable=AsyncMock,
|
||||
) as mocked_make_request:
|
||||
await bot(method)
|
||||
mocked_make_request.assert_awaited_with(bot, method, timeout=None)
|
||||
|
||||
async def test_close(self):
|
||||
async with AiohttpSession() as session:
|
||||
bot = Bot("42:TEST", session=session)
|
||||
await session.create_session()
|
||||
async def test_close(self, session: AiohttpSession):
|
||||
bot = Bot("42:TEST", session=session)
|
||||
await session.create_session()
|
||||
|
||||
with patch(
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.close", new_callable=AsyncMock
|
||||
) as mocked_close:
|
||||
await bot.session.close()
|
||||
mocked_close.assert_awaited()
|
||||
with patch(
|
||||
"aiogram.client.session.aiohttp.AiohttpSession.close", new_callable=AsyncMock
|
||||
) as mocked_close:
|
||||
await bot.session.close()
|
||||
mocked_close.assert_awaited()
|
||||
|
||||
@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 bot.session.close()
|
||||
await session.close()
|
||||
|
||||
async def test_download_file(self, aresponses: ResponsesMockServer):
|
||||
aresponses.add(
|
||||
|
|
@ -85,50 +106,56 @@ class TestBot:
|
|||
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):
|
||||
async with Bot("42:TEST").context() as bot:
|
||||
aresponses.add(
|
||||
aresponses.ANY,
|
||||
aresponses.ANY,
|
||||
"get",
|
||||
aresponses.Response(status=200, body=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),
|
||||
)
|
||||
|
||||
result = await bot.download_file("TEST")
|
||||
result = await bot.download_file("TEST")
|
||||
|
||||
assert isinstance(result, io.BytesIO)
|
||||
assert result.read() == b"\f" * 10
|
||||
assert isinstance(result, io.BytesIO)
|
||||
assert result.read() == b"\f" * 10
|
||||
|
||||
async def test_download_file_custom_destination(self, aresponses: ResponsesMockServer):
|
||||
async with Bot("42:TEST").context() as bot:
|
||||
aresponses.add(
|
||||
aresponses.ANY,
|
||||
aresponses.ANY,
|
||||
"get",
|
||||
aresponses.Response(status=200, body=b"\f" * 10),
|
||||
)
|
||||
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),
|
||||
)
|
||||
|
||||
custom = io.BytesIO()
|
||||
custom = io.BytesIO()
|
||||
|
||||
result = await bot.download_file("TEST", custom)
|
||||
result = await bot.download_file("TEST", custom)
|
||||
|
||||
assert isinstance(result, io.BytesIO)
|
||||
assert result is custom
|
||||
assert result.read() == b"\f" * 10
|
||||
assert isinstance(result, io.BytesIO)
|
||||
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)]
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue