Only one warning for multiple CallbackqueryHandler's on ConversationHandler (#1319)

This commit is contained in:
Ambro 2019-02-13 18:08:49 -03:00 committed by Eldinnie
parent e54a3188ce
commit d0936f76ad
2 changed files with 61 additions and 1 deletions

View file

@ -189,17 +189,20 @@ class ConversationHandler(Handler):
logging.warning("If 'per_message=True', all entry points and state handlers" logging.warning("If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers " " must be 'CallbackQueryHandler', since no other handlers "
"have a message context.") "have a message context.")
break
else: else:
for handler in all_handlers: for handler in all_handlers:
if isinstance(handler, CallbackQueryHandler): if isinstance(handler, CallbackQueryHandler):
logging.warning("If 'per_message=False', 'CallbackQueryHandler' will not be " logging.warning("If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message.") "tracked for every message.")
break
if self.per_chat: if self.per_chat:
for handler in all_handlers: for handler in all_handlers:
if isinstance(handler, (InlineQueryHandler, ChosenInlineResultHandler)): if isinstance(handler, (InlineQueryHandler, ChosenInlineResultHandler)):
logging.warning("If 'per_chat=True', 'InlineQueryHandler' can not be used, " logging.warning("If 'per_chat=True', 'InlineQueryHandler' can not be used, "
"since inline queries have no chat context.") "since inline queries have no chat context.")
break
def _get_key(self, update): def _get_key(self, update):
chat = update.effective_chat chat = update.effective_chat

View file

@ -23,7 +23,8 @@ import pytest
from telegram import (CallbackQuery, Chat, ChosenInlineResult, InlineQuery, Message, from telegram import (CallbackQuery, Chat, ChosenInlineResult, InlineQuery, Message,
PreCheckoutQuery, ShippingQuery, Update, User) PreCheckoutQuery, ShippingQuery, Update, User)
from telegram.ext import (ConversationHandler, CommandHandler, CallbackQueryHandler) from telegram.ext import (ConversationHandler, CommandHandler, CallbackQueryHandler,
InlineQueryHandler)
@pytest.fixture(scope='class') @pytest.fixture(scope='class')
@ -405,3 +406,59 @@ class TestConversationHandler(object):
dp.job_queue.tick() dp.job_queue.tick()
assert handler.conversations.get((self.group.id, user1.id)) is None assert handler.conversations.get((self.group.id, user1.id)) is None
assert handler.conversations.get((self.group.id, user2.id)) is None assert handler.conversations.get((self.group.id, user2.id)) is None
def test_per_message_warning_is_only_shown_once(self, caplog):
ConversationHandler(
entry_points=self.entry_points,
states={
self.THIRSTY: [CommandHandler('pourCoffee', self.drink)],
self.BREWING: [CommandHandler('startCoding', self.code)]
},
fallbacks=self.fallbacks,
per_message=True
)
assert len(caplog.messages) == 1
assert caplog.messages[0] == (
"If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers"
" have a message context."
)
def test_per_message_false_warning_is_only_shown_once(self, caplog):
ConversationHandler(
entry_points=self.entry_points,
states={
self.THIRSTY: [CallbackQueryHandler(self.drink)],
self.BREWING: [CallbackQueryHandler(self.code)],
},
fallbacks=self.fallbacks,
per_message=False
)
assert len(caplog.messages) == 1
assert caplog.messages[0] == (
"If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message."
)
def test_warnings_per_chat_is_only_shown_once(self, caplog):
def hello(bot, update):
return self.BREWING
def bye(bot, update):
return ConversationHandler.END
ConversationHandler(
entry_points=self.entry_points,
states={
self.THIRSTY: [InlineQueryHandler(hello)],
self.BREWING: [InlineQueryHandler(bye)]
},
fallbacks=self.fallbacks,
per_chat=True
)
assert len(caplog.messages) == 1
assert caplog.messages[0] == (
"If 'per_chat=True', 'InlineQueryHandler' can not be used,"
" since inline queries have no chat context."
)