8000 Improve Test Instability Caused by `Message` Fixtures by Bibo-Joshi · Pull Request #4507 · python-telegram-bot/python-telegram-bot · GitHub
[go: up one dir, main page]

Skip to content

Improve Test Instability Caused by Message Fixtures #4507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 42 additions & 45 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,26 @@
from .auxil.build_messages import make_message


@pytest.fixture(scope="module")
async def message(bot, chat_id): # mostly used in tests for edit_message
out = await bot.send_message(
@pytest.fixture
async def one_time_message(bot, chat_id):
# mostly used in tests for edit_message and hence can't be reused
return await bot.send_message(
chat_id, "Text", disable_web_page_preview=True, disable_notification=True
)
out._unfreeze()
return out


@pytest.fixture(scope="module")
async def static_message(bot, chat_id):
# must not be edited to keep tests independent! We only use bot.send_message so that
# we have a valid message_id to e.g. reply to
return await bot.send_message(
chat_id, "Text", disable_web_page_preview=True, disable_notification=True
)


@pytest.fixture
async def media_message(bot, chat_id):
# mostly used in tests for edit_message and hence can't be reused
with data_file("telegram.ogg").open("rb") as f:
return await bot.send_voice(chat_id, voice=f, caption="my caption", read_timeout=10)

Expand Down Expand Up @@ -1971,7 +1980,7 @@ async def post(url, request_data: RequestData, *args, **kwargs):
indirect=["default_bot"],
)
async def test_send_message_default_quote_parse_mode(
self, default_bot, chat_id, message, custom, monkeypatch
self, default_bot, chat_id, custom, monkeypatch
):
async def make_assertion(url, request_data: RequestData, *args, **kwargs):
assert request_data.parameters["reply_parameters"].get("quote_parse_mode") == (
Expand All @@ -1984,9 +1993,7 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs):
kwargs["quote_parse_mode"] = custom

monkeypatch.setattr(default_bot.request, "post", make_assertion)
await default_bot.send_message(
chat_id, message, reply_parameters=ReplyParameters(**kwargs)
)
await default_bot.send_message(chat_id, "test", reply_parameters=ReplyParameters(**kwargs))

@pytest.mark.parametrize(
("default_bot", "custom"),
Expand Down Expand Up @@ -2339,13 +2346,13 @@ async def test_multiple_init_cycles(self, bot):
async with test_bot:
await test_bot.get_me()

async def test_forward_message(self, bot, chat_id, message):
async def test_forward_message(self, bot, chat_id, static_message):
forward_message = await bot.forward_message(
chat_id, from_chat_id=chat_id, message_id=message.message_id
chat_id, from_chat_id=chat_id, message_id=static_message.message_id
)

assert forward_message.text == message.text
assert forward_message.forward_origin.sender_user == message.from_user
assert forward_message.text == static_message.text
assert forward_message.forward_origin.sender_user == static_message.from_user
assert isinstance(forward_message.forward_origin.date, dtm.datetime)

async def test_forward_protected_message(self, bot, chat_id):
Expand Down Expand Up @@ -2831,18 +2838,18 @@ async def test_get_one_user_profile_photo(self, bot, chat_id):
assert user_profile_photos.total_count == 1
assert user_profile_photos.photos[0][0].file_size == 5403

async def test_edit_message_text(self, bot, message):
async def test_edit_message_text(self, bot, one_time_message):
message = await bot.edit_message_text(
text="new_text",
chat_id=message.chat_id,
message_id=message.message_id,
chat_id=one_time_message.chat_id,
message_id=one_time_message.message_id,
parse_mode="HTML",
disable_web_page_preview=True,
)

assert message.text == "new_text"

async def test_edit_message_text_entities(self, bot, message):
async def test_edit_message_text_entities(self, bot, one_time_message):
test_string = "Italic Bold Code"
entities = [
MessageEntity(MessageEntity.ITALIC, 0, 6),
Expand All @@ -2851,26 +2858,25 @@ async def test_edit_message_text_entities(self, bot, message):
]
message = await bot.edit_message_text(
text=test_string,
chat_id=message.chat_id,
message_id=message.message_id,
chat_id=one_time_message.chat_id,
message_id=one_time_message.message_id,
entities=entities,
)

assert message.text == test_string
assert message.entities == tuple(entities)

@pytest.mark.parametrize("default_bot", [{"parse_mode": "Markdown"}], indirect=True)
async def test_edit_message_text_default_parse_mode(self, default_bot, chat_id):
async def test_edit_message_text_default_parse_mode(
self, default_bot, chat_id, one_time_message
):
test_string = "Italic Bold Code"
test_markdown_string = "_Italic_ *Bold* `Code`"

# can't use `message` fixture as that would change its value
message = await default_bot.send_message(chat_id, "dummy text")

message = await default_bot.edit_message_text(
text=test_markdown_string,
chat_id=message.chat_id,
message_id=message.message_id,
chat_id=one_time_message.chat_id,
message_id=one_time_message.message_id,
disable_web_page_preview=True,
)
assert message.text_markdown == test_markdown_string
Expand All @@ -2886,21 +2892,16 @@ async def test_edit_message_text_default_parse_mode(self, default_bot, chat_id):
assert message.text == test_markdown_string
assert message.text_markdown == escape_markdown(test_markdown_string)

suffix = " edited"
message = await default_bot.edit_message_text(
text=test_markdown_string,
chat_id=message.chat_id,
message_id=message.message_id,
disable_web_page_preview=True,
)
message = await default_bot.edit_message_text(
text=test_markdown_string,
text=test_markdown_string + suffix,
chat_id=message.chat_id,
message_id=message.message_id,
parse_mode="HTML",
disable_web_page_preview=True,
)
assert message.text == test_markdown_string
assert message.text_markdown == escape_markdown(test_markdown_string)
assert message.text == test_markdown_string + suffix
assert message.text_markdown == escape_markdown(test_markdown_string) + suffix

@pytest.mark.skip(reason="need reference to an inline message")
async def test_edit_message_text_inline(self):
Expand Down Expand Up @@ -2937,16 +2938,10 @@ async def test_edit_message_caption_entities(self, bot, media_message):
# edit_message_media is tested in test_inputmedia

@pytest.mark.parametrize("default_bot", [{"parse_mode": "Markdown"}], indirect=True)
async def test_edit_message_caption_default_parse_mode(self, default_bot, chat_id):
async def test_edit_message_caption_default_parse_mode(self, default_bot, media_message):
test_string = "Italic Bold Code"
test_markdown_string = "_Italic_ *Bold* `Code`"

# can't use `media_message` fixture as that would change its value
with data_file("telegram.ogg").open("rb") as f:
media_message = await default_bot.send_voice(
chat_id, voice=f, caption="my caption", read_timeout=10
)

message = await default_bot.edit_message_caption(
caption=test_markdown_string,
chat_id=media_message.chat_id,
Expand Down Expand Up @@ -2992,10 +2987,12 @@ async def test_edit_message_caption_with_parse_mode(self, bot, media_message):
async def test_edit_message_caption_inline(self):
pass

async def test_edit_reply_markup(self, bot, message):
async def test_edit_reply_markup(self, bot, one_time_message):
new_markup = InlineKeyboardMarkup([[InlineKeyboardButton(text="test", callback_data="1")]])
message = await bot.edit_message_reply_markup(
chat_id=message.chat_id, message_id=message.message_id, reply_markup=new_markup
chat_id=one_time_message.chat_id,
message_id=one_time_message.message_id,
reply_markup=new_markup,
)

assert message is not True
Expand Down Expand Up @@ -4181,9 +4178,9 @@ async def test_set_get_my_short_description(self, bot):
bot.get_my_short_description("de"),
) == 3 * [BotShortDescription("")]

async def test_set_message_reaction(self, bot, chat_id, message):
async def test_set_message_reaction(self, bot, chat_id, static_message):
assert await bot.set_message_reaction(
chat_id, message.message_id, ReactionEmoji.THUMBS_DOWN, True
chat_id, static_message.message_id, ReactionEmoji.THUMBS_DOWN, True
)

@pytest.mark.parametrize("bot_class", [Bot, ExtBot])
Expand Down
Loading
0