From 977f98a4257aa77cfada7aca492edfa500e14d94 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Thu, 12 Jan 2023 01:05:55 +0200 Subject: [PATCH] Re-disable cov branches --- aiogram/utils/chat_action.py | 2 +- pyproject.toml | 2 +- tests/test_utils/test_i18n.py | 81 ++++++++++++++++++++++++++--------- 3 files changed, 62 insertions(+), 23 deletions(-) diff --git a/aiogram/utils/chat_action.py b/aiogram/utils/chat_action.py index 530854db..b6bda916 100644 --- a/aiogram/utils/chat_action.py +++ b/aiogram/utils/chat_action.py @@ -111,7 +111,7 @@ class ChatActionSender: async with self._lock: if not self.running: return - if not self._close_event.is_set(): + if not self._close_event.is_set(): # pragma: no branches self._close_event.set() await self._closed_event.wait() self._task = None diff --git a/pyproject.toml b/pyproject.toml index feb89ed5..9d7dae75 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -226,7 +226,7 @@ testpaths = [ ] [tool.coverage.run] -branch = true +branch = false parallel = true omit = [ "aiogram/__about__.py", diff --git a/tests/test_utils/test_i18n.py b/tests/test_utils/test_i18n.py index 6879c73b..434d2e02 100644 --- a/tests/test_utils/test_i18n.py +++ b/tests/test_utils/test_i18n.py @@ -56,24 +56,24 @@ class TestI18nCore: @pytest.mark.parametrize( "locale,case,result", [ - [None, dict(singular="test"), "test"], - [None, dict(singular="test", locale="uk"), "тест"], - ["en", dict(singular="test", locale="uk"), "тест"], - ["uk", dict(singular="test", locale="uk"), "тест"], - ["uk", dict(singular="test"), "тест"], - ["it", dict(singular="test"), "test"], - [None, dict(singular="test", n=2), "test"], - [None, dict(singular="test", n=2, locale="uk"), "тест"], - ["en", dict(singular="test", n=2, locale="uk"), "тест"], - ["uk", dict(singular="test", n=2, locale="uk"), "тест"], - ["uk", dict(singular="test", n=2), "тест"], - ["it", dict(singular="test", n=2), "test"], - [None, dict(singular="test", plural="test2", n=2), "test2"], - [None, dict(singular="test", plural="test2", n=2, locale="uk"), "test2"], - ["en", dict(singular="test", plural="test2", n=2, locale="uk"), "test2"], - ["uk", dict(singular="test", plural="test2", n=2, locale="uk"), "test2"], - ["uk", dict(singular="test", plural="test2", n=2), "test2"], - ["it", dict(singular="test", plural="test2", n=2), "test2"], + [None, {"singular": "test"}, "test"], + [None, {"singular": "test", "locale": "uk"}, "тест"], + ["en", {"singular": "test", "locale": "uk"}, "тест"], + ["uk", {"singular": "test", "locale": "uk"}, "тест"], + ["uk", {"singular": "test"}, "тест"], + ["it", {"singular": "test"}, "test"], + [None, {"singular": "test", "n": 2}, "test"], + [None, {"singular": "test", "n": 2, "locale": "uk"}, "тест"], + ["en", {"singular": "test", "n": 2, "locale": "uk"}, "тест"], + ["uk", {"singular": "test", "n": 2, "locale": "uk"}, "тест"], + ["uk", {"singular": "test", "n": 2}, "тест"], + ["it", {"singular": "test", "n": 2}, "test"], + [None, {"singular": "test", "plural": "test2", "n": 2}, "test2"], + [None, {"singular": "test", "plural": "test2", "n": 2, "locale": "uk"}, "test2"], + ["en", {"singular": "test", "plural": "test2", "n": 2, "locale": "uk"}, "test2"], + ["uk", {"singular": "test", "plural": "test2", "n": 2, "locale": "uk"}, "test2"], + ["uk", {"singular": "test", "plural": "test2", "n": 2}, "test2"], + ["it", {"singular": "test", "plural": "test2", "n": 2}, "test2"], ], ) def test_gettext(self, i18n: I18n, locale: str, case: Dict[str, Any], result: str): @@ -115,8 +115,17 @@ class TestSimpleI18nMiddleware: middleware = SimpleI18nMiddleware(i18n=i18n) middleware.setup(router=dp) - assert middleware not in dp.update.outer_middleware assert middleware in dp.message.outer_middleware + assert middleware in dp.callback_query.outer_middleware + + async def test_setup_exclude(self, i18n: I18n): + dp = Dispatcher() + middleware = SimpleI18nMiddleware(i18n=i18n) + middleware.setup(router=dp, exclude={"message"}) + + assert middleware not in dp.update.outer_middleware + assert middleware not in dp.message.outer_middleware + assert middleware in dp.callback_query.outer_middleware async def test_get_unknown_locale(self, i18n: I18n): dp = Dispatcher() @@ -136,6 +145,19 @@ class TestSimpleI18nMiddleware: ) assert locale == i18n.default_locale + async def test_custom_keys(self, i18n: I18n): + async def handler(event, data): + return data + + middleware = SimpleI18nMiddleware( + i18n=i18n, i18n_key="translator", middleware_key="middleware" + ) + context: dict[str, Any] = await middleware(handler, None, {}) + assert "translator" in context + assert context["translator"] == i18n + assert "middleware" in context + assert context["middleware"] == middleware + class TestConstI18nMiddleware: async def test_middleware(self, i18n: I18n): @@ -143,13 +165,17 @@ class TestConstI18nMiddleware: result = await middleware( next_call, Update(update_id=42), - {"event_from_user": User(id=42, is_bot=False, language_code="it", first_name="Test")}, + { + "event_from_user": User( + id=42, is_bot=False, language_code="it", first_name="Test" + ), + }, ) assert result == "тест" class TestFSMI18nMiddleware: - async def test_middleware(self, i18n: I18n, bot: MockedBot): + async def test_middleware(self, i18n: I18n, bot: MockedBot, extra): middleware = FSMI18nMiddleware(i18n=i18n) storage = MemoryStorage() state = FSMContext( @@ -165,3 +191,16 @@ class TestFSMI18nMiddleware: assert i18n.current_locale == "uk" result = await middleware(next_call, Update(update_id=42), data) assert result == "тест" + + async def test_without_state(self, i18n: I18n, bot: MockedBot, extra): + middleware = FSMI18nMiddleware(i18n=i18n) + data = { + "event_from_user": User(id=42, is_bot=False, language_code="it", first_name="Test"), + } + result = await middleware(next_call, Update(update_id=42), data) + assert i18n.current_locale == "en" + assert result == "test" + + assert i18n.current_locale == "en" + result = await middleware(next_call, Update(update_id=42), data) + assert i18n.current_locale == "en"