From cd12b54ec20c3b3b0079c573761cc5e100e690aa Mon Sep 17 00:00:00 2001 From: jrootjunior Date: Tue, 26 Nov 2019 11:13:48 +0200 Subject: [PATCH] Cover BaseBot context manager --- aiogram/api/client/session/base.py | 2 +- aiogram/utils/helper.py | 4 ++-- tests/test_api/test_client/test_base_bot.py | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/aiogram/api/client/session/base.py b/aiogram/api/client/session/base.py index 30b4400e..3999d9cd 100644 --- a/aiogram/api/client/session/base.py +++ b/aiogram/api/client/session/base.py @@ -33,7 +33,7 @@ class BaseSession(abc.ABC): raise Exception(response.description) @abc.abstractmethod - async def close(self): + async def close(self): # pragma: no cover pass @abc.abstractmethod diff --git a/aiogram/utils/helper.py b/aiogram/utils/helper.py index 67400511..fbb1524b 100644 --- a/aiogram/utils/helper.py +++ b/aiogram/utils/helper.py @@ -165,13 +165,13 @@ class ListItem(Item): You can use &, | and + operators for that. """ - def add(self, other): + def add(self, other): # pragma: no cover return self + other def __get__(self, instance, owner): return ItemsList(self._value) - def __getitem__(self, item): + def __getitem__(self, item): # pragma: no cover # Only for IDE. This method is never be called. return self._value diff --git a/tests/test_api/test_client/test_base_bot.py b/tests/test_api/test_client/test_base_bot.py index e0ca18b6..04ea8540 100644 --- a/tests/test_api/test_client/test_base_bot.py +++ b/tests/test_api/test_client/test_base_bot.py @@ -1,6 +1,7 @@ import pytest from asynctest import CoroutineMock, patch +from aiogram import Bot from aiogram.api.client.base import BaseBot from aiogram.api.client.session.aiohttp import AiohttpSession from aiogram.api.methods import GetMe @@ -23,3 +24,23 @@ class TestBaseBot: ) as mocked_make_request: await base_bot.emit(method) mocked_make_request.assert_awaited_with("TOKEN", method) + + @pytest.mark.asyncio + async def test_close(self): + base_bot = BaseBot("TOKEN", session=AiohttpSession()) + 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 + async def test_context_manager(self): + with patch( + "aiogram.api.client.session.aiohttp.AiohttpSession.close", new_callable=CoroutineMock + ) as mocked_close: + async with BaseBot("TOKEN", session=AiohttpSession()) as bot: + assert isinstance(bot, BaseBot) + mocked_close.assert_awaited()