2019-11-17 23:37:24 +02:00
|
|
|
import pytest
|
|
|
|
|
|
2020-03-25 15:35:32 +03:00
|
|
|
from aiogram import Bot
|
2019-11-17 23:37:24 +02:00
|
|
|
from aiogram.api.client.session.aiohttp import AiohttpSession
|
|
|
|
|
from aiogram.api.methods import GetMe
|
|
|
|
|
|
2020-01-11 21:15:46 +02:00
|
|
|
try:
|
|
|
|
|
from asynctest import CoroutineMock, patch
|
|
|
|
|
except ImportError:
|
2020-01-11 21:33:45 +02:00
|
|
|
from unittest.mock import AsyncMock as CoroutineMock, patch # type: ignore
|
2020-01-11 21:15:46 +02:00
|
|
|
|
2019-11-17 23:37:24 +02:00
|
|
|
|
|
|
|
|
class TestBaseBot:
|
|
|
|
|
def test_init(self):
|
2020-03-25 15:35:32 +03:00
|
|
|
base_bot = Bot("42:TEST")
|
2019-11-17 23:37:24 +02:00
|
|
|
assert isinstance(base_bot.session, AiohttpSession)
|
2019-11-28 23:12:44 +02:00
|
|
|
assert base_bot.id == 42
|
|
|
|
|
|
|
|
|
|
def test_hashable(self):
|
2020-03-25 15:35:32 +03:00
|
|
|
base_bot = Bot("42:TEST")
|
2019-11-28 23:12:44 +02:00
|
|
|
assert hash(base_bot) == hash("42:TEST")
|
|
|
|
|
|
|
|
|
|
def test_equals(self):
|
2020-03-25 15:35:32 +03:00
|
|
|
base_bot = Bot("42:TEST")
|
|
|
|
|
assert base_bot == Bot("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):
|
2020-03-25 15:35:32 +03:00
|
|
|
base_bot = Bot("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)
|
2019-11-28 23:12:44 +02:00
|
|
|
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):
|
2020-03-25 15:35:32 +03:00
|
|
|
base_bot = Bot("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:
|
2020-03-25 15:35:32 +03:00
|
|
|
async with Bot("42:TEST", session=AiohttpSession()).context(
|
2019-12-10 01:14:58 +02:00
|
|
|
auto_close=close
|
|
|
|
|
) as bot:
|
2020-03-25 15:35:32 +03:00
|
|
|
assert isinstance(bot, Bot)
|
2019-12-10 01:14:58 +02:00
|
|
|
if close:
|
|
|
|
|
mocked_close.assert_awaited()
|
|
|
|
|
else:
|
|
|
|
|
mocked_close.assert_not_awaited()
|