From 52ca61b388c9a2d5a5efb7cd9c20ac0c3f523f6d Mon Sep 17 00:00:00 2001 From: Oleg A Date: Mon, 18 Sep 2023 14:53:05 +0300 Subject: [PATCH 1/6] chore: replace fixture loop with event_loop --- tests/test_utils/test_chat_action.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_utils/test_chat_action.py b/tests/test_utils/test_chat_action.py index d6041c37..b5c0a965 100644 --- a/tests/test_utils/test_chat_action.py +++ b/tests/test_utils/test_chat_action.py @@ -13,9 +13,9 @@ from tests.mocked_bot import MockedBot class TestChatActionSender: - async def test_wait(self, bot: Bot, loop: asyncio.BaseEventLoop): + async def test_wait(self, bot: Bot, event_loop: asyncio.BaseEventLoop): sender = ChatActionSender.typing(bot=bot, chat_id=42) - loop.call_soon(sender._close_event.set) + event_loop.call_soon(sender._close_event.set) start = time.monotonic() await sender._wait(1) assert time.monotonic() - start < 1 From b9026428c3a2a7ab249be445dbf3ed7b1bb39ff0 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Mon, 18 Sep 2023 15:25:45 +0300 Subject: [PATCH 2/6] chore: mark expected warnings --- tests/test_dispatcher/test_dispatcher.py | 35 ++++++++++++++++-------- tests/test_filters/test_exception.py | 3 +- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/tests/test_dispatcher/test_dispatcher.py b/tests/test_dispatcher/test_dispatcher.py index b0cc5b79..d7a0335b 100644 --- a/tests/test_dispatcher/test_dispatcher.py +++ b/tests/test_dispatcher/test_dispatcher.py @@ -187,8 +187,9 @@ class TestDispatcher: async def test_process_update_empty(self, bot: MockedBot): dispatcher = Dispatcher() - result = await dispatcher._process_update(bot=bot, update=Update(update_id=42)) - assert not result + with pytest.warns(RuntimeWarning, match="Detected unknown update type") as record: + result = await dispatcher._process_update(bot=bot, update=Update(update_id=42)) + assert not result async def test_process_update_handled(self, bot: MockedBot): dispatcher = Dispatcher() @@ -197,7 +198,8 @@ class TestDispatcher: async def update_handler(update: Update): pass - assert await dispatcher._process_update(bot=bot, update=Update(update_id=42)) + with pytest.warns(RuntimeWarning, match="Detected unknown update type"): + assert await dispatcher._process_update(bot=bot, update=Update(update_id=42)) @pytest.mark.parametrize( "event_type,update,has_chat,has_user", @@ -479,9 +481,13 @@ class TestDispatcher: async def test_listen_unknown_update(self): dp = Dispatcher() - - with pytest.raises(SkipHandler): + with ( + pytest.raises(SkipHandler), + pytest.warns(RuntimeWarning, match="Detected unknown update type") as record, + ): await dp._listen_update(Update(update_id=42)) + if not record: + pytest.fail("Expected 'Detected unknown update type' warning.") async def test_listen_unhandled_update(self): dp = Dispatcher() @@ -608,7 +614,9 @@ class TestDispatcher: async def update_handler(update: Update): raise Exception("Kaboom!") - assert await dispatcher._process_update(bot=bot, update=Update(update_id=42)) + with pytest.warns(RuntimeWarning, match="Detected unknown update type"): + assert await dispatcher._process_update(bot=bot, update=Update(update_id=42)) + log_records = [rec.message for rec in caplog.records] assert len(log_records) == 1 assert "Cause exception while process update" in log_records[0] @@ -834,12 +842,17 @@ class TestDispatcher: dispatcher = Dispatcher() dispatcher.message.register(invalid_message_handler) - response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=0.1) - assert response is None - await asyncio.sleep(0.5) + pattern = r"Detected slow response into webhook" + with pytest.warns(RuntimeWarning, match=pattern) as record: + response = await dispatcher.feed_webhook_update(bot, RAW_UPDATE, _timeout=0.1) + assert response is None + await asyncio.sleep(0.5) - log_records = [rec.message for rec in caplog.records] - assert "Cause exception while process update" in log_records[0] + log_records = [rec.message for rec in caplog.records] + assert "Cause exception while process update" in log_records[0] + + if not record: + pytest.fail("Expected 'Detected slow response into webhook' warning.") def test_specify_updates_calculation(self): def simple_msg_handler() -> None: diff --git a/tests/test_filters/test_exception.py b/tests/test_filters/test_exception.py index be056b72..dfc7fa80 100644 --- a/tests/test_filters/test_exception.py +++ b/tests/test_filters/test_exception.py @@ -72,4 +72,5 @@ class TestDispatchException: async def handler0(event): return "Handled" - assert await dp.feed_update(bot, Update(update_id=0)) == "Handled" + with pytest.warns(RuntimeWarning, match="Detected unknown update type"): + assert await dp.feed_update(bot, Update(update_id=0)) == "Handled" From 3a15a393b0a600a8ee253d66c2ef0c7fa73b2f40 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Mon, 18 Sep 2023 15:39:18 +0300 Subject: [PATCH 3/6] chore: raise unexpected warnings --- pyproject.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index aa31ab34..bbccb271 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -240,6 +240,10 @@ asyncio_mode = "auto" testpaths = [ "tests", ] +filterwarnings = [ + "error", + "ignore::pytest.PytestUnraisableExceptionWarning", +] [tool.coverage.run] branch = false From 5a6bdfd54bab09059e7f01f55dfe61b4ba1853cd Mon Sep 17 00:00:00 2001 From: Oleg A Date: Mon, 18 Sep 2023 16:23:43 +0300 Subject: [PATCH 4/6] chore: rm unused record --- tests/test_dispatcher/test_dispatcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_dispatcher/test_dispatcher.py b/tests/test_dispatcher/test_dispatcher.py index d7a0335b..20bc54f6 100644 --- a/tests/test_dispatcher/test_dispatcher.py +++ b/tests/test_dispatcher/test_dispatcher.py @@ -187,7 +187,7 @@ class TestDispatcher: async def test_process_update_empty(self, bot: MockedBot): dispatcher = Dispatcher() - with pytest.warns(RuntimeWarning, match="Detected unknown update type") as record: + with pytest.warns(RuntimeWarning, match="Detected unknown update type"): result = await dispatcher._process_update(bot=bot, update=Update(update_id=42)) assert not result From b6e28a0b90522ccaa552b29ff626e8d16f23dc5e Mon Sep 17 00:00:00 2001 From: Oleg A Date: Mon, 18 Sep 2023 16:26:33 +0300 Subject: [PATCH 5/6] fix: rm parenthesized context manager --- tests/test_dispatcher/test_dispatcher.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_dispatcher/test_dispatcher.py b/tests/test_dispatcher/test_dispatcher.py index 20bc54f6..730a59b2 100644 --- a/tests/test_dispatcher/test_dispatcher.py +++ b/tests/test_dispatcher/test_dispatcher.py @@ -481,10 +481,8 @@ class TestDispatcher: async def test_listen_unknown_update(self): dp = Dispatcher() - with ( - pytest.raises(SkipHandler), - pytest.warns(RuntimeWarning, match="Detected unknown update type") as record, - ): + pattern = "Detected unknown update type" + with pytest.raises(SkipHandler), pytest.warns(RuntimeWarning, match=pattern) as record: await dp._listen_update(Update(update_id=42)) if not record: pytest.fail("Expected 'Detected unknown update type' warning.") From d12edb9e3acd576095702357c1c2db7ca2a70680 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Mon, 18 Sep 2023 16:33:48 +0300 Subject: [PATCH 6/6] chore: warnings shall not pass --- .github/PULL_REQUEST_TEMPLATE.md | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index ade26bd1..9a4d9351 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -30,6 +30,5 @@ Please describe the tests that you ran to verify your changes. Provide instructi - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have made corresponding changes to the documentation -- [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes