aiogram/tests/test_api/test_client/test_base_bot.py

125 lines
4.1 KiB
Python
Raw Normal View History

2020-03-16 22:22:35 +05:00
import io
import aiofiles
2019-11-17 23:37:24 +02:00
import pytest
2020-03-16 22:22:35 +05:00
from aiofiles import threadpool
from aresponses import ResponsesMockServer
2019-11-17 23:37:24 +02:00
from aiogram.api.client.base import BaseBot
2020-03-18 11:45:16 +05:00
from aiogram.api.client.telegram import PRODUCTION
2019-11-17 23:37:24 +02:00
from aiogram.api.client.session.aiohttp import AiohttpSession
from aiogram.api.methods import GetMe
try:
from asynctest import CoroutineMock, patch
except ImportError:
2020-03-16 23:29:51 +05:00
from unittest.mock import AsyncMock as CoroutineMock, patch # type: ignore
2019-11-17 23:37:24 +02:00
class TestBaseBot:
def test_init(self):
base_bot = BaseBot("42:TEST")
2019-11-17 23:37:24 +02:00
assert isinstance(base_bot.session, AiohttpSession)
assert base_bot.id == 42
def test_hashable(self):
base_bot = BaseBot("42:TEST")
assert hash(base_bot) == hash("42:TEST")
def test_equals(self):
base_bot = BaseBot("42:TEST")
assert base_bot == BaseBot("42:TEST")
2019-11-28 23:52:02 +02:00
assert base_bot != "42:TEST"
2019-11-17 23:37:24 +02:00
@pytest.mark.asyncio
async def test_emit(self):
base_bot = BaseBot("42:TEST")
2019-11-17 23:37:24 +02:00
method = GetMe()
with patch(
"aiogram.api.client.session.aiohttp.AiohttpSession.make_request",
new_callable=CoroutineMock,
) as mocked_make_request:
2020-01-11 22:59:14 +02:00
await base_bot(method)
mocked_make_request.assert_awaited_with("42:TEST", method)
2019-11-26 11:13:48 +02:00
@pytest.mark.asyncio
async def test_close(self):
base_bot = BaseBot("42:TEST", session=AiohttpSession())
2019-11-26 11:13:48 +02:00
await base_bot.session.create_session()
with patch(
"aiogram.api.client.session.aiohttp.AiohttpSession.close", new_callable=CoroutineMock
) as mocked_close:
await base_bot.close()
mocked_close.assert_awaited()
@pytest.mark.asyncio
2019-12-10 01:14:58 +02:00
@pytest.mark.parametrize("close", [True, False])
async def test_context_manager(self, close: bool):
2019-11-26 11:13:48 +02:00
with patch(
"aiogram.api.client.session.aiohttp.AiohttpSession.close", new_callable=CoroutineMock
) as mocked_close:
2019-12-10 01:14:58 +02:00
async with BaseBot("42:TEST", session=AiohttpSession()).context(
auto_close=close
) as bot:
2019-11-26 11:13:48 +02:00
assert isinstance(bot, BaseBot)
2019-12-10 01:14:58 +02:00
if close:
mocked_close.assert_awaited()
else:
mocked_close.assert_not_awaited()
2020-03-16 22:22:35 +05:00
@pytest.mark.asyncio
async def test_download_file(self, aresponses: ResponsesMockServer):
aresponses.add(
aresponses.ANY, aresponses.ANY, "get", aresponses.Response(status=200, body=b"\f" * 10)
)
# https://github.com/Tinche/aiofiles#writing-tests-for-aiofiles
2020-03-16 23:29:51 +05:00
aiofiles.threadpool.wrap.register(CoroutineMock)(
2020-03-16 22:22:35 +05:00
lambda *args, **kwargs: threadpool.AsyncBufferedIOBase(*args, **kwargs)
)
2020-03-16 23:29:51 +05:00
mock_file = CoroutineMock()
2020-03-16 22:22:35 +05:00
base_bot = BaseBot("42:TEST")
with patch("aiofiles.threadpool.sync_open", return_value=mock_file):
await base_bot.download_file("TEST", "file.png")
mock_file.write.assert_called_once_with(b"\f" * 10)
@pytest.mark.asyncio
async def test_download_file_default_destination(self, aresponses: ResponsesMockServer):
base_bot = BaseBot("42:TEST")
aresponses.add(
aresponses.ANY, aresponses.ANY, "get", aresponses.Response(status=200, body=b"\f" * 10)
)
result = await base_bot.download_file("TEST")
assert isinstance(result, io.BytesIO)
assert result.read() == b"\f" * 10
@pytest.mark.asyncio
async def test_download_file_custom_destination(self, aresponses: ResponsesMockServer):
base_bot = BaseBot("42:TEST")
aresponses.add(
aresponses.ANY, aresponses.ANY, "get", aresponses.Response(status=200, body=b"\f" * 10)
)
custom = io.BytesIO()
result = await base_bot.download_file("TEST", custom)
assert isinstance(result, io.BytesIO)
assert result is custom
assert result.read() == b"\f" * 10
2020-03-18 11:45:16 +05:00
def test_get_file_url(self):
base_bot = BaseBot("42:TEST")
file_url = PRODUCTION.file_url(token="42:TEST", path="path")
assert file_url == base_bot.get_file_url("path")