Add test for notification settings

This commit is contained in:
Matthias 2020-09-19 20:04:12 +02:00
parent 413d7ddf70
commit 2554dc48e4
7 changed files with 30 additions and 11 deletions

View File

@ -36,6 +36,9 @@ class RPCMessageType(Enum):
def __repr__(self): def __repr__(self):
return self.value return self.value
def __str__(self):
return self.value
class RPCException(Exception): class RPCException(Exception):
""" """

View File

@ -59,7 +59,7 @@ class RPCManager:
try: try:
mod.send_msg(msg) mod.send_msg(msg)
except NotImplementedError: except NotImplementedError:
logger.error(f"Message type {msg['type']} not implemented by handler {mod.name}.") logger.error(f"Message type '{msg['type']}' not implemented by handler {mod.name}.")
def startup_messages(self, config: Dict[str, Any], pairlist) -> None: def startup_messages(self, config: Dict[str, Any], pairlist) -> None:
if config['dry_run']: if config['dry_run']:

View File

@ -133,9 +133,9 @@ class Telegram(RPC):
""" Send a message to telegram channel """ """ Send a message to telegram channel """
noti = self._config['telegram'].get('notification_settings', {} noti = self._config['telegram'].get('notification_settings', {}
).get(msg['type'].value, 'on') ).get(msg['type'], 'on')
if noti == 'off': if noti == 'off':
logger.info(f"Notification {msg['type']} not sent.") logger.info(f"Notification '{msg['type']}' not sent.")
# Notification disabled # Notification disabled
return return

View File

@ -54,7 +54,7 @@ class Webhook(RPC):
else: else:
raise NotImplementedError('Unknown message type: {}'.format(msg['type'])) raise NotImplementedError('Unknown message type: {}'.format(msg['type']))
if not valuedict: if not valuedict:
logger.info("Message type %s not configured for webhooks", msg['type']) logger.info("Message type '%s' not configured for webhooks", msg['type'])
return return
payload = {key: value.format(**msg) for (key, value) in valuedict.items()} payload = {key: value.format(**msg) for (key, value) in valuedict.items()}

View File

@ -127,7 +127,7 @@ def test_send_msg_webhook_CustomMessagetype(mocker, default_conf, caplog) -> Non
rpc_manager.send_msg({'type': RPCMessageType.STARTUP_NOTIFICATION, rpc_manager.send_msg({'type': RPCMessageType.STARTUP_NOTIFICATION,
'status': 'TestMessage'}) 'status': 'TestMessage'})
assert log_has( assert log_has(
"Message type RPCMessageType.STARTUP_NOTIFICATION not implemented by handler webhook.", "Message type 'startup' not implemented by handler webhook.",
caplog) caplog)

View File

@ -1299,16 +1299,14 @@ def test_show_config_handle(default_conf, update, mocker) -> None:
assert '*Initial Stoploss:* `-0.1`' in msg_mock.call_args_list[0][0][0] assert '*Initial Stoploss:* `-0.1`' in msg_mock.call_args_list[0][0][0]
def test_send_msg_buy_notification(default_conf, mocker) -> None: def test_send_msg_buy_notification(default_conf, mocker, caplog) -> None:
msg_mock = MagicMock() msg_mock = MagicMock()
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.rpc.telegram.Telegram', 'freqtrade.rpc.telegram.Telegram',
_init=MagicMock(), _init=MagicMock(),
_send_msg=msg_mock _send_msg=msg_mock
) )
freqtradebot = get_patched_freqtradebot(mocker, default_conf) msg = {
telegram = Telegram(freqtradebot)
telegram.send_msg({
'type': RPCMessageType.BUY_NOTIFICATION, 'type': RPCMessageType.BUY_NOTIFICATION,
'exchange': 'Bittrex', 'exchange': 'Bittrex',
'pair': 'ETH/BTC', 'pair': 'ETH/BTC',
@ -1321,7 +1319,10 @@ def test_send_msg_buy_notification(default_conf, mocker) -> None:
'current_rate': 1.099e-05, 'current_rate': 1.099e-05,
'amount': 1333.3333333333335, 'amount': 1333.3333333333335,
'open_date': arrow.utcnow().shift(hours=-1) 'open_date': arrow.utcnow().shift(hours=-1)
}) }
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
telegram = Telegram(freqtradebot)
telegram.send_msg(msg)
assert msg_mock.call_args[0][0] \ assert msg_mock.call_args[0][0] \
== '\N{LARGE BLUE CIRCLE} *Bittrex:* Buying ETH/BTC\n' \ == '\N{LARGE BLUE CIRCLE} *Bittrex:* Buying ETH/BTC\n' \
'*Amount:* `1333.33333333`\n' \ '*Amount:* `1333.33333333`\n' \
@ -1329,6 +1330,21 @@ def test_send_msg_buy_notification(default_conf, mocker) -> None:
'*Current Rate:* `0.00001099`\n' \ '*Current Rate:* `0.00001099`\n' \
'*Total:* `(0.001000 BTC, 12.345 USD)`' '*Total:* `(0.001000 BTC, 12.345 USD)`'
freqtradebot.config['telegram']['notification_settings'] = {'buy': 'off'}
caplog.clear()
msg_mock.reset_mock()
telegram.send_msg(msg)
msg_mock.call_count == 0
log_has("Notification 'buy' not sent.", caplog)
freqtradebot.config['telegram']['notification_settings'] = {'buy': 'silent'}
caplog.clear()
msg_mock.reset_mock()
telegram.send_msg(msg)
msg_mock.call_count == 1
msg_mock.call_args_list[0][1]['disable_notification'] is True
def test_send_msg_buy_cancel_notification(default_conf, mocker) -> None: def test_send_msg_buy_cancel_notification(default_conf, mocker) -> None:
msg_mock = MagicMock() msg_mock = MagicMock()

View File

@ -174,7 +174,7 @@ def test_exception_send_msg(default_conf, mocker, caplog):
webhook = Webhook(get_patched_freqtradebot(mocker, default_conf)) webhook = Webhook(get_patched_freqtradebot(mocker, default_conf))
webhook.send_msg({'type': RPCMessageType.BUY_NOTIFICATION}) webhook.send_msg({'type': RPCMessageType.BUY_NOTIFICATION})
assert log_has(f"Message type {RPCMessageType.BUY_NOTIFICATION} not configured for webhooks", assert log_has(f"Message type '{RPCMessageType.BUY_NOTIFICATION}' not configured for webhooks",
caplog) caplog)
default_conf["webhook"] = get_webhook_dict() default_conf["webhook"] = get_webhook_dict()