From 6da529c46b0614da57782f586044e92b2c41081c Mon Sep 17 00:00:00 2001 From: Bibo-Joshi Date: Sat, 8 Feb 2020 17:52:23 +0100 Subject: [PATCH] Pass correct parse_mode to InlineResults if bot.defaults is None (#1763) * Pass correct parse_mode to InlineResults if bot.defaults is None * Add tests for inlinequeryresults with (default)parse_mode * enhance tests --- telegram/bot.py | 5 +++- tests/test_bot.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/telegram/bot.py b/telegram/bot.py index a75771a87..11794a7ad 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -1512,7 +1512,10 @@ class Bot(TelegramObject): for res in results: if res._has_parse_mode and res.parse_mode == DEFAULT_NONE: - res.parse_mode = self.defaults.parse_mode + if self.defaults: + res.parse_mode = self.defaults.parse_mode + else: + res.parse_mode = None if res._has_input_message_content and res.input_message_content: if (res.input_message_content._has_parse_mode and res.input_message_content.parse_mode == DEFAULT_NONE): diff --git a/tests/test_bot.py b/tests/test_bot.py index 1bf5756ed..a35f773a1 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -28,7 +28,8 @@ from future.utils import string_types from telegram import (Bot, Update, ChatAction, TelegramError, User, InlineKeyboardMarkup, InlineKeyboardButton, InlineQueryResultArticle, InputTextMessageContent, - ShippingOption, LabeledPrice, ChatPermissions, Poll) + ShippingOption, LabeledPrice, ChatPermissions, Poll, + InlineQueryResultDocument) from telegram.error import BadRequest, InvalidToken, NetworkError, RetryAfter from telegram.utils.helpers import from_timestamp, escape_markdown @@ -237,6 +238,67 @@ class TestBot(object): switch_pm_text='switch pm', switch_pm_parameter='start_pm') + def test_answer_inline_query_no_default_parse_mode(self, monkeypatch, bot): + def test(_, url, data, *args, **kwargs): + return data == {'cache_time': 300, + 'results': [{'title': 'test_result', 'id': '123', 'type': 'document', + 'document_url': 'https://raw.githubusercontent.com/' + 'python-telegram-bot/logos/master/logo/png/' + 'ptb-logo_240.png', 'mime_type': 'image/png', + 'caption': 'ptb_logo'}], + 'next_offset': '42', 'switch_pm_parameter': 'start_pm', + 'inline_query_id': 1234, 'is_personal': True, + 'switch_pm_text': 'switch pm'} + + monkeypatch.setattr('telegram.utils.request.Request.post', test) + results = [InlineQueryResultDocument( + id='123', + document_url='https://raw.githubusercontent.com/python-telegram-bot/logos/master/' + 'logo/png/ptb-logo_240.png', + title='test_result', + mime_type='image/png', + caption='ptb_logo', + )] + + assert bot.answer_inline_query(1234, + results=results, + cache_time=300, + is_personal=True, + next_offset='42', + switch_pm_text='switch pm', + switch_pm_parameter='start_pm') + + @pytest.mark.parametrize('default_bot', [{'parse_mode': 'Markdown'}], indirect=True) + def test_answer_inline_query_default_parse_mode(self, monkeypatch, default_bot): + def test(_, url, data, *args, **kwargs): + return data == {'cache_time': 300, + 'results': [{'title': 'test_result', 'id': '123', 'type': 'document', + 'document_url': 'https://raw.githubusercontent.com/' + 'python-telegram-bot/logos/master/logo/png/' + 'ptb-logo_240.png', 'mime_type': 'image/png', + 'caption': 'ptb_logo', 'parse_mode': 'Markdown'}], + 'next_offset': '42', 'switch_pm_parameter': 'start_pm', + 'inline_query_id': 1234, 'is_personal': True, + 'switch_pm_text': 'switch pm'} + + monkeypatch.setattr('telegram.utils.request.Request.post', test) + results = [InlineQueryResultDocument( + id='123', + document_url='https://raw.githubusercontent.com/python-telegram-bot/logos/master/' + 'logo/png/ptb-logo_240.png', + title='test_result', + mime_type='image/png', + caption='ptb_logo', + )] + + assert default_bot.answer_inline_query(1234, + results=results, + cache_time=300, + is_personal=True, + next_offset='42', + switch_pm_text='switch pm', + switch_pm_parameter='start_pm') + @flaky(3, 1) @pytest.mark.timeout(10) def test_get_user_profile_photos(self, bot, chat_id):