From 4cbbb80bc3a1717642a73467d9351d8b7bf2dee9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 24 Dec 2020 07:10:01 +0100 Subject: [PATCH 1/3] Refactor test_telegram to simplify tests --- tests/rpc/test_rpc_telegram.py | 169 +++++++++++++-------------------- 1 file changed, 65 insertions(+), 104 deletions(-) diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index b8c5d8858..71782411b 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -54,11 +54,18 @@ class DummyCls(Telegram): raise Exception('test') -def test__init__(default_conf, mocker) -> None: +def get_telegram_testobject(mocker, default_conf): + ftbot = get_patched_freqtradebot(mocker, default_conf) + telegram = Telegram(ftbot) + + return telegram, ftbot + + +def test_telegram__init__(default_conf, mocker) -> None: mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) - telegram = Telegram(get_patched_freqtradebot(mocker, default_conf)) + telegram, _ = get_telegram_testobject(mocker, default_conf) assert telegram._config == default_conf @@ -66,7 +73,7 @@ def test_telegram_init(default_conf, mocker, caplog) -> None: start_polling = MagicMock() mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock(return_value=start_polling)) - Telegram(get_patched_freqtradebot(mocker, default_conf)) + get_telegram_testobject(mocker, default_conf) assert start_polling.call_count == 0 # number of handles registered @@ -88,7 +95,7 @@ def test_cleanup(default_conf, mocker, ) -> None: updater_mock.stop = MagicMock() mocker.patch('freqtrade.rpc.telegram.Updater', updater_mock) - telegram = Telegram(get_patched_freqtradebot(mocker, default_conf)) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram.cleanup() assert telegram._updater.stop.call_count == 1 @@ -180,8 +187,7 @@ def test_telegram_status(default_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram._status(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -208,12 +214,10 @@ def test_status_handle(default_conf, update, ticker, fee, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) - freqtradebot.state = State.STOPPED # Status is also enabled when stopped telegram._status(update=update, context=MagicMock()) @@ -257,10 +261,10 @@ def test_status_table_handle(default_conf, update, ticker, fee, mocker) -> None: ) default_conf['stake_amount'] = 15.0 - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + + patch_get_signal(freqtradebot, (True, False)) freqtradebot.state = State.STOPPED # Status table is also enabled when stopped @@ -308,9 +312,9 @@ def test_daily_handle(default_conf, update, ticker, limit_buy_order, fee, _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) # Create some test data freqtradebot.enter_positions() @@ -386,9 +390,8 @@ def test_daily_wrong_input(default_conf, update, ticker, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) # Try invalid data msg_mock.reset_mock() @@ -425,9 +428,8 @@ def test_profit_handle(default_conf, update, ticker, ticker_sell_up, fee, _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) telegram._profit(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -519,11 +521,9 @@ def test_telegram_balance_handle(default_conf, update, mocker, rpc_balance, tick _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) - telegram._balance(update=update, context=MagicMock()) result = msg_mock.call_args_list[0][0][0] assert msg_mock.call_count == 1 @@ -548,11 +548,9 @@ def test_balance_handle_empty_response(default_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) - freqtradebot.config['dry_run'] = False telegram._balance(update=update, context=MagicMock()) result = msg_mock.call_args_list[0][0][0] @@ -570,11 +568,9 @@ def test_balance_handle_empty_response_dry(default_conf, update, mocker) -> None _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) - telegram._balance(update=update, context=MagicMock()) result = msg_mock.call_args_list[0][0][0] assert msg_mock.call_count == 1 @@ -608,11 +604,9 @@ def test_balance_handle_too_large_response(default_conf, update, mocker) -> None _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) - telegram._balance(update=update, context=MagicMock()) assert msg_mock.call_count > 1 # Test if wrap happens around 4000 - @@ -630,8 +624,7 @@ def test_start_handle(default_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) freqtradebot.state = State.STOPPED assert freqtradebot.state == State.STOPPED @@ -648,8 +641,7 @@ def test_start_handle_already_running(default_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) freqtradebot.state = State.RUNNING assert freqtradebot.state == State.RUNNING @@ -667,8 +659,7 @@ def test_stop_handle(default_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) freqtradebot.state = State.RUNNING assert freqtradebot.state == State.RUNNING @@ -686,8 +677,7 @@ def test_stop_handle_already_stopped(default_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) freqtradebot.state = State.STOPPED assert freqtradebot.state == State.STOPPED @@ -705,8 +695,7 @@ def test_stopbuy_handle(default_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) assert freqtradebot.config['max_open_trades'] != 0 telegram._stopbuy(update=update, context=MagicMock()) @@ -724,8 +713,7 @@ def test_reload_config_handle(default_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) freqtradebot.state = State.RUNNING assert freqtradebot.state == State.RUNNING @@ -909,9 +897,8 @@ def test_forcesell_handle_invalid(default_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) # Trader is not running freqtradebot.state = State.STOPPED @@ -1014,9 +1001,8 @@ def test_performance_handle(default_conf, update, ticker, fee, fetch_ticker=ticker, get_fee=fee, ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) # Create some test data freqtradebot.enter_positions() @@ -1049,9 +1035,8 @@ def test_count_handle(default_conf, update, ticker, fee, mocker) -> None: fetch_ticker=ticker, get_fee=fee, ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) freqtradebot.state = State.STOPPED telegram._count(update=update, context=MagicMock()) @@ -1085,9 +1070,8 @@ def test_telegram_lock_handle(default_conf, update, ticker, fee, mocker) -> None fetch_ticker=ticker, get_fee=fee, ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) PairLocks.lock_pair('ETH/BTC', arrow.utcnow().shift(minutes=4).datetime, 'randreason') PairLocks.lock_pair('XRP/BTC', arrow.utcnow().shift(minutes=20).datetime, 'deadbeef') @@ -1110,9 +1094,7 @@ def test_whitelist_static(default_conf, update, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) telegram._whitelist(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1131,9 +1113,7 @@ def test_whitelist_dynamic(default_conf, update, mocker) -> None: default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 4 }] - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) telegram._whitelist(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1148,9 +1128,7 @@ def test_blacklist_static(default_conf, update, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) telegram._blacklist(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1190,9 +1168,8 @@ def test_telegram_logs(default_conf, update, mocker) -> None: ) setup_logging(default_conf) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, _ = get_telegram_testobject(mocker, default_conf) - telegram = Telegram(freqtradebot) context = MagicMock() context.args = [] telegram._logs(update=update, context=context) @@ -1223,9 +1200,7 @@ def test_edge_disabled(default_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram._edge(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1245,9 +1220,7 @@ def test_edge_enabled(edge_conf, update, mocker) -> None: _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, edge_conf) - - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, edge_conf) telegram._edge(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1263,8 +1236,8 @@ def test_telegram_trades(mocker, update, default_conf, fee): _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) + context = MagicMock() context.args = [] @@ -1299,8 +1272,7 @@ def test_telegram_delete_trade(mocker, update, default_conf, fee): _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) context = MagicMock() context.args = [] @@ -1325,9 +1297,7 @@ def test_help_handle(default_conf, update, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram._help(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1341,8 +1311,7 @@ def test_version_handle(default_conf, update, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram._version(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1357,8 +1326,8 @@ def test_show_config_handle(default_conf, update, mocker) -> None: _send_msg=msg_mock ) default_conf['runmode'] = RunMode.DRY_RUN - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) telegram._show_config(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1398,8 +1367,8 @@ def test_send_msg_buy_notification(default_conf, mocker, caplog) -> None: 'amount': 1333.3333333333335, 'open_date': arrow.utcnow().shift(hours=-1) } - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram.send_msg(msg) assert msg_mock.call_args[0][0] \ == '\N{LARGE BLUE CIRCLE} *Bittrex:* Buying ETH/BTC\n' \ @@ -1431,8 +1400,8 @@ def test_send_msg_buy_cancel_notification(default_conf, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram.send_msg({ 'type': RPCMessageType.BUY_CANCEL_NOTIFICATION, 'exchange': 'Bittrex', @@ -1450,8 +1419,8 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) + old_convamount = telegram._fiat_converter.convert_amount telegram._fiat_converter.convert_amount = lambda a, b, c: -24.812 telegram.send_msg({ @@ -1520,8 +1489,8 @@ def test_send_msg_sell_cancel_notification(default_conf, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) + old_convamount = telegram._fiat_converter.convert_amount telegram._fiat_converter.convert_amount = lambda a, b, c: -24.812 telegram.send_msg({ @@ -1554,8 +1523,7 @@ def test_send_msg_status_notification(default_conf, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram.send_msg({ 'type': RPCMessageType.STATUS_NOTIFICATION, 'status': 'running' @@ -1570,8 +1538,7 @@ def test_warning_notification(default_conf, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram.send_msg({ 'type': RPCMessageType.WARNING_NOTIFICATION, 'status': 'message' @@ -1586,8 +1553,7 @@ def test_startup_notification(default_conf, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram.send_msg({ 'type': RPCMessageType.STARTUP_NOTIFICATION, 'status': '*Custom:* `Hello World`' @@ -1602,8 +1568,7 @@ def test_send_msg_unknown_type(default_conf, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) with pytest.raises(NotImplementedError, match=r'Unknown message type: None'): telegram.send_msg({ 'type': None, @@ -1618,8 +1583,8 @@ def test_send_msg_buy_notification_no_fiat(default_conf, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram.send_msg({ 'type': RPCMessageType.BUY_NOTIFICATION, 'exchange': 'Bittrex', @@ -1649,8 +1614,7 @@ def test_send_msg_sell_notification_no_fiat(default_conf, mocker) -> None: _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram.send_msg({ 'type': RPCMessageType.SELL_NOTIFICATION, 'exchange': 'Binance', @@ -1696,8 +1660,7 @@ def test__sell_emoji(default_conf, mocker, msg, expected): _init=MagicMock(), _send_msg=msg_mock ) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) assert telegram._get_sell_emoji(msg) == expected @@ -1705,8 +1668,7 @@ def test__sell_emoji(default_conf, mocker, msg, expected): def test__send_msg(default_conf, mocker) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) bot = MagicMock() - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram._updater = MagicMock() telegram._updater.bot = bot @@ -1719,8 +1681,7 @@ def test__send_msg_network_error(default_conf, mocker, caplog) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) bot = MagicMock() bot.send_message = MagicMock(side_effect=NetworkError('Oh snap')) - freqtradebot = get_patched_freqtradebot(mocker, default_conf) - telegram = Telegram(freqtradebot) + telegram, _ = get_telegram_testobject(mocker, default_conf) telegram._updater = MagicMock() telegram._updater.bot = bot From be4a4be7a3a34d19f05d2f3932325afd8692ce82 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 24 Dec 2020 07:29:26 +0100 Subject: [PATCH 2/3] Further simplify test_telegram --- tests/rpc/test_rpc_telegram.py | 373 ++++++--------------------------- 1 file changed, 69 insertions(+), 304 deletions(-) diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index 71782411b..dc8ff46c1 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -54,18 +54,25 @@ class DummyCls(Telegram): raise Exception('test') -def get_telegram_testobject(mocker, default_conf): +def get_telegram_testobject(mocker, default_conf, mock=True): + msg_mock = MagicMock() + if mock: + mocker.patch.multiple( + 'freqtrade.rpc.telegram.Telegram', + _init=MagicMock(), + _send_msg=msg_mock + ) ftbot = get_patched_freqtradebot(mocker, default_conf) telegram = Telegram(ftbot) - return telegram, ftbot + return telegram, ftbot, msg_mock def test_telegram__init__(default_conf, mocker) -> None: mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, _ = get_telegram_testobject(mocker, default_conf) assert telegram._config == default_conf @@ -73,7 +80,7 @@ def test_telegram_init(default_conf, mocker, caplog) -> None: start_polling = MagicMock() mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock(return_value=start_polling)) - get_telegram_testobject(mocker, default_conf) + get_telegram_testobject(mocker, default_conf, mock=False) assert start_polling.call_count == 0 # number of handles registered @@ -95,7 +102,7 @@ def test_cleanup(default_conf, mocker, ) -> None: updater_mock.stop = MagicMock() mocker.patch('freqtrade.rpc.telegram.Updater', updater_mock) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, _ = get_telegram_testobject(mocker, default_conf, mock=False) telegram.cleanup() assert telegram._updater.stop.call_count == 1 @@ -152,11 +159,9 @@ def test_telegram_status(default_conf, update, mocker) -> None: default_conf['telegram']['enabled'] = False default_conf['telegram']['chat_id'] = "123" - msg_mock = MagicMock() status_table = MagicMock() mocker.patch.multiple( 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), _rpc_trade_status=MagicMock(return_value=[{ 'trade_id': 1, 'pair': 'ETH/BTC', @@ -184,10 +189,9 @@ def test_telegram_status(default_conf, update, mocker) -> None: 'is_open': True }]), _status_table=status_table, - _send_msg=msg_mock ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram._status(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -205,16 +209,13 @@ def test_status_handle(default_conf, update, ticker, fee, mocker) -> None: fetch_ticker=ticker, get_fee=fee, ) - msg_mock = MagicMock() status_table = MagicMock() mocker.patch.multiple( 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), _status_table=status_table, - _send_msg=msg_mock ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) @@ -253,16 +254,10 @@ def test_status_table_handle(default_conf, update, ticker, fee, mocker) -> None: fetch_ticker=ticker, get_fee=fee, ) - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) default_conf['stake_amount'] = 15.0 - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) @@ -305,14 +300,8 @@ def test_daily_handle(default_conf, update, ticker, limit_buy_order, fee, fetch_ticker=ticker, get_fee=fee, ) - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) @@ -383,14 +372,8 @@ def test_daily_wrong_input(default_conf, update, ticker, mocker) -> None: 'freqtrade.exchange.Exchange', fetch_ticker=ticker ) - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) # Try invalid data @@ -421,14 +404,8 @@ def test_profit_handle(default_conf, update, ticker, ticker_sell_up, fee, fetch_ticker=ticker, get_fee=fee, ) - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) telegram._profit(update=update, context=MagicMock()) @@ -480,16 +457,8 @@ def test_telegram_stats(default_conf, update, ticker, ticker_sell_up, fee, fetch_ticker=ticker, get_fee=fee, ) - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - - freqtradebot = get_patched_freqtradebot(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) telegram._stats(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -514,14 +483,7 @@ def test_telegram_balance_handle(default_conf, update, mocker, rpc_balance, tick mocker.patch('freqtrade.exchange.Exchange.get_valid_pair_combination', side_effect=lambda a, b: f"{a}/{b}") - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) telegram._balance(update=update, context=MagicMock()) @@ -541,14 +503,7 @@ def test_balance_handle_empty_response(default_conf, update, mocker) -> None: default_conf['dry_run'] = False mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value={}) - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) freqtradebot.config['dry_run'] = False @@ -561,14 +516,7 @@ def test_balance_handle_empty_response(default_conf, update, mocker) -> None: def test_balance_handle_empty_response_dry(default_conf, update, mocker) -> None: mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value={}) - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) telegram._balance(update=update, context=MagicMock()) @@ -597,14 +545,7 @@ def test_balance_handle_too_large_response(default_conf, update, mocker) -> None 'value': 1000.0, }) - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) telegram._balance(update=update, context=MagicMock()) @@ -617,14 +558,8 @@ def test_balance_handle_too_large_response(default_conf, update, mocker) -> None def test_start_handle(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) freqtradebot.state = State.STOPPED assert freqtradebot.state == State.STOPPED @@ -634,14 +569,8 @@ def test_start_handle(default_conf, update, mocker) -> None: def test_start_handle_already_running(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) freqtradebot.state = State.RUNNING assert freqtradebot.state == State.RUNNING @@ -652,14 +581,8 @@ def test_start_handle_already_running(default_conf, update, mocker) -> None: def test_stop_handle(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) freqtradebot.state = State.RUNNING assert freqtradebot.state == State.RUNNING @@ -670,14 +593,8 @@ def test_stop_handle(default_conf, update, mocker) -> None: def test_stop_handle_already_stopped(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) freqtradebot.state = State.STOPPED assert freqtradebot.state == State.STOPPED @@ -688,14 +605,8 @@ def test_stop_handle_already_stopped(default_conf, update, mocker) -> None: def test_stopbuy_handle(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) assert freqtradebot.config['max_open_trades'] != 0 telegram._stopbuy(update=update, context=MagicMock()) @@ -706,14 +617,8 @@ def test_stopbuy_handle(default_conf, update, mocker) -> None: def test_reload_config_handle(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) freqtradebot.state = State.RUNNING assert freqtradebot.state == State.RUNNING @@ -890,14 +795,8 @@ def test_forcesell_all_handle(default_conf, update, ticker, fee, mocker) -> None def test_forcesell_handle_invalid(default_conf, update, mocker) -> None: mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0) - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) # Trader is not running @@ -990,18 +889,13 @@ def test_forcebuy_handle_exception(default_conf, update, markets, mocker) -> Non def test_performance_handle(default_conf, update, ticker, fee, limit_buy_order, limit_sell_order, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) + mocker.patch.multiple( 'freqtrade.exchange.Exchange', fetch_ticker=ticker, get_fee=fee, ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) # Create some test data @@ -1024,18 +918,12 @@ def test_performance_handle(default_conf, update, ticker, fee, def test_count_handle(default_conf, update, ticker, fee, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) mocker.patch.multiple( 'freqtrade.exchange.Exchange', fetch_ticker=ticker, get_fee=fee, ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) freqtradebot.state = State.STOPPED @@ -1059,18 +947,12 @@ def test_count_handle(default_conf, update, ticker, fee, mocker) -> None: def test_telegram_lock_handle(default_conf, update, ticker, fee, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) mocker.patch.multiple( 'freqtrade.exchange.Exchange', fetch_ticker=ticker, get_fee=fee, ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) PairLocks.lock_pair('ETH/BTC', arrow.utcnow().shift(minutes=4).datetime, 'randreason') @@ -1088,13 +970,8 @@ def test_telegram_lock_handle(default_conf, update, ticker, fee, mocker) -> None def test_whitelist_static(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) telegram._whitelist(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1103,17 +980,11 @@ def test_whitelist_static(default_conf, update, mocker) -> None: def test_whitelist_dynamic(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True)) default_conf['pairlists'] = [{'method': 'VolumePairList', 'number_assets': 4 }] - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram._whitelist(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1122,13 +993,8 @@ def test_whitelist_dynamic(default_conf, update, mocker) -> None: def test_blacklist_static(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) telegram._blacklist(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1160,15 +1026,13 @@ def test_blacklist_static(default_conf, update, mocker) -> None: def test_telegram_logs(default_conf, update, mocker) -> None: - msg_mock = MagicMock() mocker.patch.multiple( 'freqtrade.rpc.telegram.Telegram', _init=MagicMock(), - _send_msg=msg_mock ) setup_logging(default_conf) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) context = MagicMock() context.args = [] @@ -1193,14 +1057,8 @@ def test_telegram_logs(default_conf, update, mocker) -> None: def test_edge_disabled(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram._edge(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1208,19 +1066,13 @@ def test_edge_disabled(default_conf, update, mocker) -> None: def test_edge_enabled(edge_conf, update, mocker) -> None: - msg_mock = MagicMock() mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock( return_value={ 'E/F': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), } )) - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, edge_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, edge_conf) telegram._edge(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1229,14 +1081,8 @@ def test_edge_enabled(edge_conf, update, mocker) -> None: def test_telegram_trades(mocker, update, default_conf, fee): - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) context = MagicMock() context.args = [] @@ -1265,14 +1111,8 @@ def test_telegram_trades(mocker, update, default_conf, fee): def test_telegram_delete_trade(mocker, update, default_conf, fee): - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) context = MagicMock() context.args = [] @@ -1291,13 +1131,7 @@ def test_telegram_delete_trade(mocker, update, default_conf, fee): def test_help_handle(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram._help(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1305,13 +1139,8 @@ def test_help_handle(default_conf, update, mocker) -> None: def test_version_handle(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram._version(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1319,15 +1148,10 @@ def test_version_handle(default_conf, update, mocker) -> None: def test_show_config_handle(default_conf, update, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) + default_conf['runmode'] = RunMode.DRY_RUN - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) telegram._show_config(update=update, context=MagicMock()) assert msg_mock.call_count == 1 @@ -1347,12 +1171,7 @@ def test_show_config_handle(default_conf, update, mocker) -> None: def test_send_msg_buy_notification(default_conf, mocker, caplog) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) + msg = { 'type': RPCMessageType.BUY_NOTIFICATION, 'exchange': 'Bittrex', @@ -1367,7 +1186,7 @@ def test_send_msg_buy_notification(default_conf, mocker, caplog) -> None: 'amount': 1333.3333333333335, 'open_date': arrow.utcnow().shift(hours=-1) } - telegram, freqtradebot = get_telegram_testobject(mocker, default_conf) + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) telegram.send_msg(msg) assert msg_mock.call_args[0][0] \ @@ -1394,13 +1213,8 @@ def test_send_msg_buy_notification(default_conf, mocker, caplog) -> None: def test_send_msg_buy_cancel_notification(default_conf, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram.send_msg({ 'type': RPCMessageType.BUY_CANCEL_NOTIFICATION, @@ -1413,13 +1227,8 @@ def test_send_msg_buy_cancel_notification(default_conf, mocker) -> None: def test_send_msg_sell_notification(default_conf, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) old_convamount = telegram._fiat_converter.convert_amount telegram._fiat_converter.convert_amount = lambda a, b, c: -24.812 @@ -1483,13 +1292,8 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None: def test_send_msg_sell_cancel_notification(default_conf, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) old_convamount = telegram._fiat_converter.convert_amount telegram._fiat_converter.convert_amount = lambda a, b, c: -24.812 @@ -1517,13 +1321,8 @@ def test_send_msg_sell_cancel_notification(default_conf, mocker) -> None: def test_send_msg_status_notification(default_conf, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram.send_msg({ 'type': RPCMessageType.STATUS_NOTIFICATION, 'status': 'running' @@ -1532,13 +1331,7 @@ def test_send_msg_status_notification(default_conf, mocker) -> None: def test_warning_notification(default_conf, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram.send_msg({ 'type': RPCMessageType.WARNING_NOTIFICATION, 'status': 'message' @@ -1547,13 +1340,7 @@ def test_warning_notification(default_conf, mocker) -> None: def test_startup_notification(default_conf, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram.send_msg({ 'type': RPCMessageType.STARTUP_NOTIFICATION, 'status': '*Custom:* `Hello World`' @@ -1562,13 +1349,7 @@ def test_startup_notification(default_conf, mocker) -> None: def test_send_msg_unknown_type(default_conf, mocker) -> None: - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, _ = get_telegram_testobject(mocker, default_conf) with pytest.raises(NotImplementedError, match=r'Unknown message type: None'): telegram.send_msg({ 'type': None, @@ -1577,13 +1358,7 @@ def test_send_msg_unknown_type(default_conf, mocker) -> None: def test_send_msg_buy_notification_no_fiat(default_conf, mocker) -> None: del default_conf['fiat_display_currency'] - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram.send_msg({ 'type': RPCMessageType.BUY_NOTIFICATION, @@ -1608,13 +1383,8 @@ def test_send_msg_buy_notification_no_fiat(default_conf, mocker) -> None: def test_send_msg_sell_notification_no_fiat(default_conf, mocker) -> None: del default_conf['fiat_display_currency'] - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) + telegram.send_msg({ 'type': RPCMessageType.SELL_NOTIFICATION, 'exchange': 'Binance', @@ -1654,13 +1424,8 @@ def test_send_msg_sell_notification_no_fiat(default_conf, mocker) -> None: ]) def test__sell_emoji(default_conf, mocker, msg, expected): del default_conf['fiat_display_currency'] - msg_mock = MagicMock() - mocker.patch.multiple( - 'freqtrade.rpc.telegram.Telegram', - _init=MagicMock(), - _send_msg=msg_mock - ) - telegram, _ = get_telegram_testobject(mocker, default_conf) + + telegram, _, _ = get_telegram_testobject(mocker, default_conf) assert telegram._get_sell_emoji(msg) == expected @@ -1668,7 +1433,7 @@ def test__sell_emoji(default_conf, mocker, msg, expected): def test__send_msg(default_conf, mocker) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) bot = MagicMock() - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, _ = get_telegram_testobject(mocker, default_conf, mock=False) telegram._updater = MagicMock() telegram._updater.bot = bot @@ -1681,7 +1446,7 @@ def test__send_msg_network_error(default_conf, mocker, caplog) -> None: mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) bot = MagicMock() bot.send_message = MagicMock(side_effect=NetworkError('Oh snap')) - telegram, _ = get_telegram_testobject(mocker, default_conf) + telegram, _, _ = get_telegram_testobject(mocker, default_conf, mock=False) telegram._updater = MagicMock() telegram._updater.bot = bot From 5bf739b917d8a32872c8f1e5e32722f1c5a2d7ce Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 24 Dec 2020 07:39:46 +0100 Subject: [PATCH 3/3] Simplify more telegram tests --- tests/rpc/test_rpc_telegram.py | 52 +++++++++++++--------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index dc8ff46c1..5040f35cf 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -6,7 +6,7 @@ import re from datetime import datetime from random import choice, randint from string import ascii_uppercase -from unittest.mock import ANY, MagicMock, PropertyMock +from unittest.mock import ANY, MagicMock import arrow import pytest @@ -631,7 +631,7 @@ def test_reload_config_handle(default_conf, update, mocker) -> None: def test_telegram_forcesell_handle(default_conf, update, ticker, fee, ticker_sell_up, mocker) -> None: mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0) - rpc_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock()) + msg_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) patch_exchange(mocker) patch_whitelist(mocker, default_conf) @@ -659,8 +659,8 @@ def test_telegram_forcesell_handle(default_conf, update, ticker, fee, context.args = ["1"] telegram._forcesell(update=update, context=context) - assert rpc_mock.call_count == 3 - last_msg = rpc_mock.call_args_list[-1][0][0] + assert msg_mock.call_count == 3 + last_msg = msg_mock.call_args_list[-1][0][0] assert { 'type': RPCMessageType.SELL_NOTIFICATION, 'trade_id': 1, @@ -686,7 +686,7 @@ def test_telegram_forcesell_down_handle(default_conf, update, ticker, fee, ticker_sell_down, mocker) -> None: mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0) - rpc_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock()) + msg_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) patch_exchange(mocker) patch_whitelist(mocker, default_conf) @@ -718,9 +718,9 @@ def test_telegram_forcesell_down_handle(default_conf, update, ticker, fee, context.args = ["1"] telegram._forcesell(update=update, context=context) - assert rpc_mock.call_count == 3 + assert msg_mock.call_count == 3 - last_msg = rpc_mock.call_args_list[-1][0][0] + last_msg = msg_mock.call_args_list[-1][0][0] assert { 'type': RPCMessageType.SELL_NOTIFICATION, 'trade_id': 1, @@ -746,7 +746,7 @@ def test_forcesell_all_handle(default_conf, update, ticker, fee, mocker) -> None patch_exchange(mocker) mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0) - rpc_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock()) + msg_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) patch_whitelist(mocker, default_conf) mocker.patch.multiple( @@ -761,7 +761,7 @@ def test_forcesell_all_handle(default_conf, update, ticker, fee, mocker) -> None # Create some test data freqtradebot.enter_positions() - rpc_mock.reset_mock() + msg_mock.reset_mock() # /forcesell all context = MagicMock() @@ -769,8 +769,8 @@ def test_forcesell_all_handle(default_conf, update, ticker, fee, mocker) -> None telegram._forcesell(update=update, context=context) # Called for each trade 3 times - assert rpc_mock.call_count == 8 - msg = rpc_mock.call_args_list[1][0][0] + assert msg_mock.call_count == 8 + msg = msg_mock.call_args_list[1][0][0] assert { 'type': RPCMessageType.SELL_NOTIFICATION, 'trade_id': 1, @@ -828,21 +828,14 @@ def test_forcesell_handle_invalid(default_conf, update, mocker) -> None: assert 'invalid argument' in msg_mock.call_args_list[0][0][0] -def test_forcebuy_handle(default_conf, update, markets, mocker) -> None: +def test_forcebuy_handle(default_conf, update, mocker) -> None: mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0) - mocker.patch('freqtrade.rpc.telegram.Telegram._send_msg', MagicMock()) - mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) - patch_exchange(mocker) - mocker.patch.multiple( - 'freqtrade.exchange.Exchange', - markets=PropertyMock(markets), - ) + fbuy_mock = MagicMock(return_value=None) mocker.patch('freqtrade.rpc.RPC._rpc_forcebuy', fbuy_mock) - freqtradebot = FreqtradeBot(default_conf) + telegram, freqtradebot, _ = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) # /forcebuy ETH/BTC context = MagicMock() @@ -867,24 +860,17 @@ def test_forcebuy_handle(default_conf, update, markets, mocker) -> None: assert fbuy_mock.call_args_list[0][0][1] == 0.055 -def test_forcebuy_handle_exception(default_conf, update, markets, mocker) -> None: +def test_forcebuy_handle_exception(default_conf, update, mocker) -> None: mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0) - rpc_mock = mocker.patch('freqtrade.rpc.telegram.Telegram._send_msg', MagicMock()) - mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) - patch_exchange(mocker) - mocker.patch.multiple( - 'freqtrade.exchange.Exchange', - markets=PropertyMock(markets), - ) - freqtradebot = FreqtradeBot(default_conf) + + telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) patch_get_signal(freqtradebot, (True, False)) - telegram = Telegram(freqtradebot) update.message.text = '/forcebuy ETH/Nonepair' telegram._forcebuy(update=update, context=MagicMock()) - assert rpc_mock.call_count == 1 - assert rpc_mock.call_args_list[0][0][0] == 'Forcebuy not enabled.' + assert msg_mock.call_count == 1 + assert msg_mock.call_args_list[0][0][0] == 'Forcebuy not enabled.' def test_performance_handle(default_conf, update, ticker, fee,