Implement tests for message updating

This commit is contained in:
Matthias 2021-06-19 09:31:34 +02:00
parent a9f111dca0
commit 6e99e3fbbb
2 changed files with 39 additions and 24 deletions

View File

@ -1088,27 +1088,20 @@ class Telegram(RPCHandler):
message_id = query.message.message_id message_id = query.message.message_id
try: try:
try: self._updater.bot.edit_message_text(
self._updater.bot.edit_message_text( chat_id=chat_id,
chat_id=chat_id, message_id=message_id,
message_id=message_id, text=msg,
text=msg, parse_mode=parse_mode,
parse_mode=parse_mode, reply_markup=reply_markup
reply_markup=reply_markup
)
except BadRequest as e:
if 'not modified' in e.message.lower():
pass
else:
logger.warning(
'TelegramError: %s',
e.message
)
except TelegramError as telegram_err:
logger.warning(
'TelegramError: %s! Giving up on that message.',
telegram_err.message
) )
except BadRequest as e:
if 'not modified' in e.message.lower():
pass
else:
logger.warning('TelegramError: %s', e.message)
except TelegramError as telegram_err:
logger.warning('TelegramError: %s! Giving up on that message.', telegram_err.message)
def _send_msg(self, msg: str, parse_mode: str = ParseMode.MARKDOWN, def _send_msg(self, msg: str, parse_mode: str = ParseMode.MARKDOWN,
disable_notification: bool = False, disable_notification: bool = False,

View File

@ -13,7 +13,7 @@ from unittest.mock import ANY, MagicMock
import arrow import arrow
import pytest import pytest
from telegram import Chat, Message, ReplyKeyboardMarkup, Update from telegram import Chat, Message, ReplyKeyboardMarkup, Update
from telegram.error import NetworkError from telegram.error import BadRequest, NetworkError, TelegramError
from freqtrade import __version__ from freqtrade import __version__
from freqtrade.constants import CANCEL_REASON from freqtrade.constants import CANCEL_REASON
@ -25,8 +25,8 @@ from freqtrade.loggers import setup_logging
from freqtrade.persistence import PairLocks, Trade from freqtrade.persistence import PairLocks, Trade
from freqtrade.rpc import RPC from freqtrade.rpc import RPC
from freqtrade.rpc.telegram import Telegram, authorized_only from freqtrade.rpc.telegram import Telegram, authorized_only
from tests.conftest import (create_mock_trades, get_patched_freqtradebot, log_has, patch_exchange, from tests.conftest import (create_mock_trades, get_patched_freqtradebot, log_has, log_has_re,
patch_get_signal, patch_whitelist) patch_exchange, patch_get_signal, patch_whitelist)
class DummyCls(Telegram): class DummyCls(Telegram):
@ -1561,7 +1561,7 @@ def test__sell_emoji(default_conf, mocker, msg, expected):
assert telegram._get_sell_emoji(msg) == expected assert telegram._get_sell_emoji(msg) == expected
def test__send_msg(default_conf, mocker) -> None: def test_telegram__send_msg(default_conf, mocker, caplog) -> None:
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
bot = MagicMock() bot = MagicMock()
telegram, _, _ = get_telegram_testobject(mocker, default_conf, mock=False) telegram, _, _ = get_telegram_testobject(mocker, default_conf, mock=False)
@ -1572,6 +1572,28 @@ def test__send_msg(default_conf, mocker) -> None:
telegram._send_msg('test') telegram._send_msg('test')
assert len(bot.method_calls) == 1 assert len(bot.method_calls) == 1
# Test update
query = MagicMock()
telegram._send_msg('test', callback_path="DeadBeef", query=query, reload_able=True)
edit_message_text = telegram._updater.bot.edit_message_text
assert edit_message_text.call_count == 1
assert "Updated: " in edit_message_text.call_args_list[0][1]['text']
telegram._updater.bot.edit_message_text = MagicMock(side_effect=BadRequest("not modified"))
telegram._send_msg('test', callback_path="DeadBeef", query=query)
assert telegram._updater.bot.edit_message_text.call_count == 1
assert not log_has_re(r"TelegramError: .*", caplog)
telegram._updater.bot.edit_message_text = MagicMock(side_effect=BadRequest(""))
telegram._send_msg('test2', callback_path="DeadBeef", query=query)
assert telegram._updater.bot.edit_message_text.call_count == 1
assert log_has_re(r"TelegramError: .*", caplog)
telegram._updater.bot.edit_message_text = MagicMock(side_effect=TelegramError("DeadBEEF"))
telegram._send_msg('test3', callback_path="DeadBeef", query=query)
assert log_has_re(r"TelegramError: DeadBEEF! Giving up.*", caplog)
def test__send_msg_network_error(default_conf, mocker, caplog) -> None: def test__send_msg_network_error(default_conf, mocker, caplog) -> None:
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())