From 2bc65560ebbf77e1f301fac5472e1bafa1fd565e Mon Sep 17 00:00:00 2001 From: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:53:26 +0200 Subject: [PATCH] Stabilize Some Flaky Tests (#4500) --- tests/_files/test_animation.py | 3 ++- tests/test_bot.py | 31 +++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/_files/test_animation.py b/tests/_files/test_animation.py index a312d3575..0f581259d 100644 --- a/tests/_files/test_animation.py +++ b/tests/_files/test_animation.py @@ -233,7 +233,8 @@ class TestAnimationWithRequest(AnimationTestBase): assert message.animation.file_unique_id assert message.animation.file_name == animation.file_name assert message.animation.mime_type == animation.mime_type - assert message.animation.file_size == animation.file_size + # TGs reported file size is not reliable + assert isinstance(message.animation.file_size, int) assert message.animation.thumbnail.width == self.width assert message.animation.thumbnail.height == self.height assert message.has_protected_content diff --git a/tests/test_bot.py b/tests/test_bot.py index 379addbd8..426b5aa66 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -2372,15 +2372,12 @@ class TestBotWithRequest: assert all("can't be forwarded" in str(exc) for exc in result) async def test_forward_messages(self, bot, chat_id): - tasks = asyncio.gather( - bot.send_message(chat_id, text="will be forwarded"), - bot.send_message(chat_id, text="will be forwarded"), - ) - - msg1, msg2 = await tasks + # not using gather here to have deteriminically ordered message_ids + msg1 = await bot.send_message(chat_id, text="will be forwarded") + msg2 = await bot.send_message(chat_id, text="will be forwarded") forward_messages = await bot.forward_messages( - chat_id, from_chat_id=chat_id, message_ids=sorted((msg1.message_id, msg2.message_id)) + chat_id, from_chat_id=chat_id, message_ids=(msg1.message_id, msg2.message_id) ) assert isinstance(forward_messages, tuple) @@ -3939,14 +3936,12 @@ class TestBotWithRequest: assert len(message.caption_entities) == 0 async def test_copy_messages(self, bot, chat_id): - tasks = asyncio.gather( - bot.send_message(chat_id, text="will be copied 1"), - bot.send_message(chat_id, text="will be copied 2"), - ) - msg1, msg2 = await tasks + # not using gather here to have deterministically ordered message_ids + msg1 = await bot.send_message(chat_id, text="will be copied 1") + msg2 = await bot.send_message(chat_id, text="will be copied 2") copy_messages = await bot.copy_messages( - chat_id, from_chat_id=chat_id, message_ids=sorted((msg1.message_id, msg2.message_id)) + chat_id, from_chat_id=chat_id, message_ids=(msg1.message_id, msg2.message_id) ) assert isinstance(copy_messages, tuple) @@ -4059,7 +4054,7 @@ class TestBotWithRequest: bot.callback_data_cache.clear_callback_data() bot.callback_data_cache.clear_callback_queries() - async def test_get_chat_arbitrary_callback_data(self, channel_id, cdc_bot): + async def test_get_chat_arbitrary_callback_data(self, chat_id, cdc_bot): bot = cdc_bot try: @@ -4068,7 +4063,7 @@ class TestBotWithRequest: ) message = await bot.send_message( - channel_id, text="get_chat_arbitrary_callback_data", reply_markup=reply_markup + chat_id, text="get_chat_arbitrary_callback_data", reply_markup=reply_markup ) await message.pin() @@ -4078,7 +4073,11 @@ class TestBotWithRequest: ) assert data == "callback_data" - cfi = await bot.get_chat(channel_id) + cfi = await bot.get_chat(chat_id) + + if not cfi.pinned_message: + pytest.xfail("Pinning messages is not always reliable on TG") + assert cfi.pinned_message == message assert cfi.pinned_message.reply_markup == reply_markup assert await message.unpin() # (not placed in finally block since msg can be unbound)