From 432735773a2c6f8480c9ed6d707a2a338d82ddc2 Mon Sep 17 00:00:00 2001 From: Anton Ermak Date: Sat, 27 Jan 2018 13:04:06 +0700 Subject: [PATCH 1/4] Unit test --- freqtrade/rpc/telegram.py | 5 ++++- freqtrade/tests/conftest.py | 9 +++++++++ freqtrade/tests/rpc/test_rpc_telegram.py | 21 ++++++++++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 70d5a78f3..afea04f99 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -398,7 +398,10 @@ def _balance(bot: Bot, update: Update) -> None: if coin == 'BTC': currency["Rate"] = 1.0 else: - currency["Rate"] = exchange.get_ticker('BTC_' + coin, False)['bid'] + if coin == 'USDT': + currency["Rate"] = 1.0 / exchange.get_ticker('USDT_BTC', False)['bid'] + else: + currency["Rate"] = exchange.get_ticker('BTC_' + coin, False)['bid'] currency['BTC'] = currency["Rate"] * currency["Balance"] total = total + currency['BTC'] output += """*Currency*: {Currency} diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index 70249fd7d..f9d14c170 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -71,6 +71,15 @@ def ticker(): }) +@pytest.fixture +def ticker_usdt(): + return MagicMock(return_value={ + 'bid': 10000.00, + 'ask': 10000.00, + 'last': 10000.00, + }) + + @pytest.fixture def ticker_sell_up(): return MagicMock(return_value={ diff --git a/freqtrade/tests/rpc/test_rpc_telegram.py b/freqtrade/tests/rpc/test_rpc_telegram.py index 986f3f8f0..f71e7bc21 100644 --- a/freqtrade/tests/rpc/test_rpc_telegram.py +++ b/freqtrade/tests/rpc/test_rpc_telegram.py @@ -604,7 +604,7 @@ def test_stop_handle_already_stopped(default_conf, update, mocker): assert 'already stopped' in msg_mock.call_args_list[0][0][0] -def test_balance_handle(default_conf, update, mocker): +def test_balance_handle(default_conf, update, ticker_usdt, mocker): mock_balance = [{ 'Currency': 'BTC', 'Balance': 10.0, @@ -617,6 +617,12 @@ def test_balance_handle(default_conf, update, mocker): 'Available': 0.0, 'Pending': 0.0, 'CryptoAddress': 'XXXX', + }, { + 'Currency': 'USDT', + 'Balance': 10000.0, + 'Available': 0.0, + 'Pending': 0.0, + 'CryptoAddress': 'XXXX', }] mocker.patch.dict('freqtrade.main._CONF', default_conf) msg_mock = MagicMock() @@ -625,16 +631,21 @@ def test_balance_handle(default_conf, update, mocker): init=MagicMock(), send_msg=msg_mock) mocker.patch.multiple('freqtrade.main.exchange', - get_balances=MagicMock(return_value=mock_balance)) + get_balances=MagicMock(return_value=mock_balance), + get_ticker=ticker_usdt) mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap', ticker=MagicMock(return_value={'price_usd': 15000.0}), _cache_symbols=MagicMock(return_value={'BTC': 1})) _balance(bot=MagicMock(), update=update) + result = msg_mock.call_args_list[0][0][0] assert msg_mock.call_count == 1 - assert '*Currency*: BTC' in msg_mock.call_args_list[0][0][0] - assert 'Balance' in msg_mock.call_args_list[0][0][0] - assert 'Est. BTC' in msg_mock.call_args_list[0][0][0] + assert '*Currency*: BTC' in result + assert '*Currency*: ETH' not in result + assert '*Currency*: USDT' in result + assert 'Balance' in result + assert 'Est. BTC' in result + assert '*BTC*: 11.00000000' in result def test_help_handle(default_conf, update, mocker): From 81ed7627bfbe628ad137422f26752a7987c2ff35 Mon Sep 17 00:00:00 2001 From: Anton Ermak Date: Sat, 27 Jan 2018 13:04:06 +0700 Subject: [PATCH 2/4] Unit test --- freqtrade/rpc/telegram.py | 5 ++++- freqtrade/tests/conftest.py | 9 +++++++++ freqtrade/tests/rpc/test_rpc_telegram.py | 21 ++++++++++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 70d5a78f3..afea04f99 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -398,7 +398,10 @@ def _balance(bot: Bot, update: Update) -> None: if coin == 'BTC': currency["Rate"] = 1.0 else: - currency["Rate"] = exchange.get_ticker('BTC_' + coin, False)['bid'] + if coin == 'USDT': + currency["Rate"] = 1.0 / exchange.get_ticker('USDT_BTC', False)['bid'] + else: + currency["Rate"] = exchange.get_ticker('BTC_' + coin, False)['bid'] currency['BTC'] = currency["Rate"] * currency["Balance"] total = total + currency['BTC'] output += """*Currency*: {Currency} diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index 053b980f7..ca6c52ba5 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -83,6 +83,15 @@ def ticker(): }) +@pytest.fixture +def ticker_usdt(): + return MagicMock(return_value={ + 'bid': 10000.00, + 'ask': 10000.00, + 'last': 10000.00, + }) + + @pytest.fixture def ticker_sell_up(): return MagicMock(return_value={ diff --git a/freqtrade/tests/rpc/test_rpc_telegram.py b/freqtrade/tests/rpc/test_rpc_telegram.py index 34d92b43e..956c048cb 100644 --- a/freqtrade/tests/rpc/test_rpc_telegram.py +++ b/freqtrade/tests/rpc/test_rpc_telegram.py @@ -605,7 +605,7 @@ def test_stop_handle_already_stopped(default_conf, update, mocker): assert 'already stopped' in msg_mock.call_args_list[0][0][0] -def test_balance_handle(default_conf, update, mocker): +def test_balance_handle(default_conf, update, ticker_usdt, mocker): mock_balance = [{ 'Currency': 'BTC', 'Balance': 10.0, @@ -618,6 +618,12 @@ def test_balance_handle(default_conf, update, mocker): 'Available': 0.0, 'Pending': 0.0, 'CryptoAddress': 'XXXX', + }, { + 'Currency': 'USDT', + 'Balance': 10000.0, + 'Available': 0.0, + 'Pending': 0.0, + 'CryptoAddress': 'XXXX', }] mocker.patch.dict('freqtrade.main._CONF', default_conf) msg_mock = MagicMock() @@ -626,16 +632,21 @@ def test_balance_handle(default_conf, update, mocker): init=MagicMock(), send_msg=msg_mock) mocker.patch.multiple('freqtrade.main.exchange', - get_balances=MagicMock(return_value=mock_balance)) + get_balances=MagicMock(return_value=mock_balance), + get_ticker=ticker_usdt) mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap', ticker=MagicMock(return_value={'price_usd': 15000.0}), _cache_symbols=MagicMock(return_value={'BTC': 1})) _balance(bot=MagicMock(), update=update) + result = msg_mock.call_args_list[0][0][0] assert msg_mock.call_count == 1 - assert '*Currency*: BTC' in msg_mock.call_args_list[0][0][0] - assert 'Balance' in msg_mock.call_args_list[0][0][0] - assert 'Est. BTC' in msg_mock.call_args_list[0][0][0] + assert '*Currency*: BTC' in result + assert '*Currency*: ETH' not in result + assert '*Currency*: USDT' in result + assert 'Balance' in result + assert 'Est. BTC' in result + assert '*BTC*: 11.00000000' in result def test_help_handle(default_conf, update, mocker): From 45239724c6110342c8c877f0315106afd00df8a2 Mon Sep 17 00:00:00 2001 From: Anton Ermak Date: Sun, 28 Jan 2018 16:15:23 +0700 Subject: [PATCH 3/4] Skip convert if balance is zero --- freqtrade/rpc/telegram.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index afea04f99..3d4ab5aa5 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -390,7 +390,8 @@ def _balance(bot: Bot, update: Update) -> None: if c['Balance'] or c['Available'] or c['Pending'] ] if not balances: - output = '`All balances are zero.`' + send_msg('`All balances are zero.`') + return total = 0.0 for currency in balances: From 807c06770197cda3cc2b53ce7fe9eddac7718876 Mon Sep 17 00:00:00 2001 From: Anton Ermak Date: Mon, 29 Jan 2018 10:55:42 +0700 Subject: [PATCH 4/4] More test coverage --- freqtrade/tests/conftest.py | 9 ----- freqtrade/tests/rpc/test_rpc_telegram.py | 45 +++++++++++++++++++++--- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index ca6c52ba5..053b980f7 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -83,15 +83,6 @@ def ticker(): }) -@pytest.fixture -def ticker_usdt(): - return MagicMock(return_value={ - 'bid': 10000.00, - 'ask': 10000.00, - 'last': 10000.00, - }) - - @pytest.fixture def ticker_sell_up(): return MagicMock(return_value={ diff --git a/freqtrade/tests/rpc/test_rpc_telegram.py b/freqtrade/tests/rpc/test_rpc_telegram.py index 956c048cb..69f598e65 100644 --- a/freqtrade/tests/rpc/test_rpc_telegram.py +++ b/freqtrade/tests/rpc/test_rpc_telegram.py @@ -605,7 +605,8 @@ def test_stop_handle_already_stopped(default_conf, update, mocker): assert 'already stopped' in msg_mock.call_args_list[0][0][0] -def test_balance_handle(default_conf, update, ticker_usdt, mocker): +def test_balance_handle(default_conf, update, mocker): + mock_balance = [{ 'Currency': 'BTC', 'Balance': 10.0, @@ -624,7 +625,28 @@ def test_balance_handle(default_conf, update, ticker_usdt, mocker): 'Available': 0.0, 'Pending': 0.0, 'CryptoAddress': 'XXXX', + }, { + 'Currency': 'LTC', + 'Balance': 10.0, + 'Available': 10.0, + 'Pending': 0.0, + 'CryptoAddress': 'XXXX', }] + + def mock_ticker(symbol, refresh): + if symbol == 'USDT_BTC': + return { + 'bid': 10000.00, + 'ask': 10000.00, + 'last': 10000.00, + } + else: + return { + 'bid': 0.1, + 'ask': 0.1, + 'last': 0.1, + } + mocker.patch.dict('freqtrade.main._CONF', default_conf) msg_mock = MagicMock() mocker.patch.multiple('freqtrade.rpc.telegram', @@ -632,11 +654,11 @@ def test_balance_handle(default_conf, update, ticker_usdt, mocker): init=MagicMock(), send_msg=msg_mock) mocker.patch.multiple('freqtrade.main.exchange', - get_balances=MagicMock(return_value=mock_balance), - get_ticker=ticker_usdt) + get_balances=MagicMock(return_value=mock_balance)) mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap', ticker=MagicMock(return_value={'price_usd': 15000.0}), _cache_symbols=MagicMock(return_value={'BTC': 1})) + mocker.patch('freqtrade.main.exchange.get_ticker', side_effect=mock_ticker) _balance(bot=MagicMock(), update=update) result = msg_mock.call_args_list[0][0][0] @@ -646,7 +668,22 @@ def test_balance_handle(default_conf, update, ticker_usdt, mocker): assert '*Currency*: USDT' in result assert 'Balance' in result assert 'Est. BTC' in result - assert '*BTC*: 11.00000000' in result + assert '*BTC*: 12.00000000' in result + + +def test_zero_balance_handle(default_conf, update, mocker): + mocker.patch.dict('freqtrade.main._CONF', default_conf) + msg_mock = MagicMock() + mocker.patch.multiple('freqtrade.rpc.telegram', + _CONF=default_conf, + init=MagicMock(), + send_msg=msg_mock) + mocker.patch.multiple('freqtrade.main.exchange', + get_balances=MagicMock(return_value=[])) + _balance(bot=MagicMock(), update=update) + result = msg_mock.call_args_list[0][0][0] + assert msg_mock.call_count == 1 + assert '`All balances are zero.`' in result def test_help_handle(default_conf, update, mocker):