rpc: remove tuple return madness
This commit is contained in:
@@ -7,9 +7,11 @@ Unit test file for rpc/rpc.py
|
||||
from datetime import datetime
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from freqtrade.freqtradebot import FreqtradeBot
|
||||
from freqtrade.persistence import Trade
|
||||
from freqtrade.rpc.rpc import RPC
|
||||
from freqtrade.rpc.rpc import RPC, RPCException
|
||||
from freqtrade.state import State
|
||||
from freqtrade.tests.test_freqtradebot import patch_get_signal, patch_coinmarketcap
|
||||
|
||||
@@ -41,19 +43,16 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
freqtradebot.state = State.STOPPED
|
||||
(error, result) = rpc.rpc_trade_status()
|
||||
assert error
|
||||
assert 'trader is not running' in result
|
||||
with pytest.raises(RPCException, match=r'.*trader is not running*'):
|
||||
rpc._rpc_trade_status()
|
||||
|
||||
freqtradebot.state = State.RUNNING
|
||||
(error, result) = rpc.rpc_trade_status()
|
||||
assert error
|
||||
assert 'no active trade' in result
|
||||
with pytest.raises(RPCException, match=r'.*no active trade*'):
|
||||
rpc._rpc_trade_status()
|
||||
|
||||
freqtradebot.create_trade()
|
||||
(error, result) = rpc.rpc_trade_status()
|
||||
assert not error
|
||||
trade = result[0]
|
||||
trades = rpc._rpc_trade_status()
|
||||
trade = trades[0]
|
||||
|
||||
result_message = [
|
||||
'*Trade ID:* `1`\n'
|
||||
@@ -68,7 +67,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
|
||||
'*Current Profit:* `-0.59%`\n'
|
||||
'*Open Order:* `(limit buy rem=0.00000000)`'
|
||||
]
|
||||
assert result == result_message
|
||||
assert trades == result_message
|
||||
assert trade.find('[ETH/BTC]') >= 0
|
||||
|
||||
|
||||
@@ -90,17 +89,15 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None:
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
freqtradebot.state = State.STOPPED
|
||||
(error, result) = rpc.rpc_status_table()
|
||||
assert error
|
||||
assert '*Status:* `trader is not running`' in result
|
||||
with pytest.raises(RPCException, match=r'.*\*Status:\* `trader is not running``*'):
|
||||
rpc._rpc_status_table()
|
||||
|
||||
freqtradebot.state = State.RUNNING
|
||||
(error, result) = rpc.rpc_status_table()
|
||||
assert error
|
||||
assert '*Status:* `no active order`' in result
|
||||
with pytest.raises(RPCException, match=r'.*\*Status:\* `no active order`*'):
|
||||
rpc._rpc_status_table()
|
||||
|
||||
freqtradebot.create_trade()
|
||||
(error, result) = rpc.rpc_status_table()
|
||||
result = rpc._rpc_status_table()
|
||||
assert 'just now' in result['Since'].all()
|
||||
assert 'ETH/BTC' in result['Pair'].all()
|
||||
assert '-0.59%' in result['Profit'].all()
|
||||
@@ -140,8 +137,7 @@ def test_rpc_daily_profit(default_conf, update, ticker, fee,
|
||||
|
||||
# Try valid data
|
||||
update.message.text = '/daily 2'
|
||||
(error, days) = rpc.rpc_daily_profit(7, stake_currency, fiat_display_currency)
|
||||
assert not error
|
||||
days = rpc._rpc_daily_profit(7, stake_currency, fiat_display_currency)
|
||||
assert len(days) == 7
|
||||
for day in days:
|
||||
# [datetime.date(2018, 1, 11), '0.00000000 BTC', '0.000 USD']
|
||||
@@ -154,9 +150,8 @@ def test_rpc_daily_profit(default_conf, update, ticker, fee,
|
||||
assert str(days[0][0]) == str(datetime.utcnow().date())
|
||||
|
||||
# Try invalid data
|
||||
(error, days) = rpc.rpc_daily_profit(0, stake_currency, fiat_display_currency)
|
||||
assert error
|
||||
assert days.find('must be an integer greater than 0') >= 0
|
||||
with pytest.raises(RPCException, match=r'.*must be an integer greater than 0*'):
|
||||
rpc._rpc_daily_profit(0, stake_currency, fiat_display_currency)
|
||||
|
||||
|
||||
def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee,
|
||||
@@ -184,9 +179,8 @@ def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee,
|
||||
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
(error, stats) = rpc.rpc_trade_statistics(stake_currency, fiat_display_currency)
|
||||
assert error
|
||||
assert stats.find('no closed trade') >= 0
|
||||
with pytest.raises(RPCException, match=r'.*no closed trade*'):
|
||||
rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
|
||||
|
||||
# Create some test data
|
||||
freqtradebot.create_trade()
|
||||
@@ -219,8 +213,7 @@ def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee,
|
||||
trade.close_date = datetime.utcnow()
|
||||
trade.is_open = False
|
||||
|
||||
(error, stats) = rpc.rpc_trade_statistics(stake_currency, fiat_display_currency)
|
||||
assert not error
|
||||
stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
|
||||
assert prec_satoshi(stats['profit_closed_coin'], 6.217e-05)
|
||||
assert prec_satoshi(stats['profit_closed_percent'], 6.2)
|
||||
assert prec_satoshi(stats['profit_closed_fiat'], 0.93255)
|
||||
@@ -281,8 +274,7 @@ def test_rpc_trade_statistics_closed(mocker, default_conf, ticker, fee,
|
||||
for trade in Trade.query.order_by(Trade.id).all():
|
||||
trade.open_rate = None
|
||||
|
||||
(error, stats) = rpc.rpc_trade_statistics(stake_currency, fiat_display_currency)
|
||||
assert not error
|
||||
stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
|
||||
assert prec_satoshi(stats['profit_closed_coin'], 0)
|
||||
assert prec_satoshi(stats['profit_closed_percent'], 0)
|
||||
assert prec_satoshi(stats['profit_closed_fiat'], 0)
|
||||
@@ -330,18 +322,16 @@ def test_rpc_balance_handle(default_conf, mocker):
|
||||
freqtradebot = FreqtradeBot(default_conf)
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
(error, res) = rpc.rpc_balance(default_conf['fiat_display_currency'])
|
||||
assert not error
|
||||
(trade, x, y, z) = res
|
||||
assert prec_satoshi(x, 12)
|
||||
assert prec_satoshi(z, 180000)
|
||||
assert 'USD' in y
|
||||
assert len(trade) == 1
|
||||
assert 'BTC' in trade[0]['currency']
|
||||
assert prec_satoshi(trade[0]['available'], 10)
|
||||
assert prec_satoshi(trade[0]['balance'], 12)
|
||||
assert prec_satoshi(trade[0]['pending'], 2)
|
||||
assert prec_satoshi(trade[0]['est_btc'], 12)
|
||||
output, total, symbol, value = rpc._rpc_balance(default_conf['fiat_display_currency'])
|
||||
assert prec_satoshi(total, 12)
|
||||
assert prec_satoshi(value, 180000)
|
||||
assert 'USD' in symbol
|
||||
assert len(output) == 1
|
||||
assert 'BTC' in output[0]['currency']
|
||||
assert prec_satoshi(output[0]['available'], 10)
|
||||
assert prec_satoshi(output[0]['balance'], 12)
|
||||
assert prec_satoshi(output[0]['pending'], 2)
|
||||
assert prec_satoshi(output[0]['est_btc'], 12)
|
||||
|
||||
|
||||
def test_rpc_start(mocker, default_conf) -> None:
|
||||
@@ -361,13 +351,11 @@ def test_rpc_start(mocker, default_conf) -> None:
|
||||
rpc = RPC(freqtradebot)
|
||||
freqtradebot.state = State.STOPPED
|
||||
|
||||
(error, result) = rpc.rpc_start()
|
||||
assert not error
|
||||
result = rpc._rpc_start()
|
||||
assert '`Starting trader ...`' in result
|
||||
assert freqtradebot.state == State.RUNNING
|
||||
|
||||
(error, result) = rpc.rpc_start()
|
||||
assert error
|
||||
result = rpc._rpc_start()
|
||||
assert '*Status:* `already running`' in result
|
||||
assert freqtradebot.state == State.RUNNING
|
||||
|
||||
@@ -389,13 +377,11 @@ def test_rpc_stop(mocker, default_conf) -> None:
|
||||
rpc = RPC(freqtradebot)
|
||||
freqtradebot.state = State.RUNNING
|
||||
|
||||
(error, result) = rpc.rpc_stop()
|
||||
assert not error
|
||||
result = rpc._rpc_stop()
|
||||
assert '`Stopping trader ...`' in result
|
||||
assert freqtradebot.state == State.STOPPED
|
||||
|
||||
(error, result) = rpc.rpc_stop()
|
||||
assert error
|
||||
result = rpc._rpc_stop()
|
||||
assert '*Status:* `already stopped`' in result
|
||||
assert freqtradebot.state == State.STOPPED
|
||||
|
||||
@@ -428,36 +414,26 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker) -> None:
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
freqtradebot.state = State.STOPPED
|
||||
(error, res) = rpc.rpc_forcesell(None)
|
||||
assert error
|
||||
assert res == '`trader is not running`'
|
||||
with pytest.raises(RPCException, match=r'.*`trader is not running`*'):
|
||||
rpc._rpc_forcesell(None)
|
||||
|
||||
freqtradebot.state = State.RUNNING
|
||||
(error, res) = rpc.rpc_forcesell(None)
|
||||
assert error
|
||||
assert res == 'Invalid argument.'
|
||||
with pytest.raises(RPCException, match=r'.*Invalid argument.*'):
|
||||
rpc._rpc_forcesell(None)
|
||||
|
||||
(error, res) = rpc.rpc_forcesell('all')
|
||||
assert not error
|
||||
assert res == ''
|
||||
rpc._rpc_forcesell('all')
|
||||
|
||||
freqtradebot.create_trade()
|
||||
(error, res) = rpc.rpc_forcesell('all')
|
||||
assert not error
|
||||
assert res == ''
|
||||
rpc._rpc_forcesell('all')
|
||||
|
||||
(error, res) = rpc.rpc_forcesell('1')
|
||||
assert not error
|
||||
assert res == ''
|
||||
rpc._rpc_forcesell('1')
|
||||
|
||||
freqtradebot.state = State.STOPPED
|
||||
(error, res) = rpc.rpc_forcesell(None)
|
||||
assert error
|
||||
assert res == '`trader is not running`'
|
||||
with pytest.raises(RPCException, match=r'.*`trader is not running`*'):
|
||||
rpc._rpc_forcesell(None)
|
||||
|
||||
(error, res) = rpc.rpc_forcesell('all')
|
||||
assert error
|
||||
assert res == '`trader is not running`'
|
||||
with pytest.raises(RPCException, match=r'.*`trader is not running`*'):
|
||||
rpc._rpc_forcesell('all')
|
||||
|
||||
freqtradebot.state = State.RUNNING
|
||||
assert cancel_order_mock.call_count == 0
|
||||
@@ -475,9 +451,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker) -> None:
|
||||
)
|
||||
# check that the trade is called, which is done by ensuring exchange.cancel_order is called
|
||||
# and trade amount is updated
|
||||
(error, res) = rpc.rpc_forcesell('1')
|
||||
assert not error
|
||||
assert res == ''
|
||||
rpc._rpc_forcesell('1')
|
||||
assert cancel_order_mock.call_count == 1
|
||||
assert trade.amount == filled_amount
|
||||
|
||||
@@ -495,9 +469,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker) -> None:
|
||||
}
|
||||
)
|
||||
# check that the trade is called, which is done by ensuring exchange.cancel_order is called
|
||||
(error, res) = rpc.rpc_forcesell('2')
|
||||
assert not error
|
||||
assert res == ''
|
||||
rpc._rpc_forcesell('2')
|
||||
assert cancel_order_mock.call_count == 2
|
||||
assert trade.amount == amount
|
||||
|
||||
@@ -511,9 +483,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker) -> None:
|
||||
'side': 'sell'
|
||||
}
|
||||
)
|
||||
(error, res) = rpc.rpc_forcesell('3')
|
||||
assert not error
|
||||
assert res == ''
|
||||
rpc._rpc_forcesell('3')
|
||||
# status quo, no exchange calls
|
||||
assert cancel_order_mock.call_count == 2
|
||||
|
||||
@@ -550,8 +520,7 @@ def test_performance_handle(default_conf, ticker, limit_buy_order, fee,
|
||||
|
||||
trade.close_date = datetime.utcnow()
|
||||
trade.is_open = False
|
||||
(error, res) = rpc.rpc_performance()
|
||||
assert not error
|
||||
res = rpc._rpc_performance()
|
||||
assert len(res) == 1
|
||||
assert res[0]['pair'] == 'ETH/BTC'
|
||||
assert res[0]['count'] == 1
|
||||
@@ -576,14 +545,12 @@ def test_rpc_count(mocker, default_conf, ticker, fee) -> None:
|
||||
freqtradebot = FreqtradeBot(default_conf)
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
(error, trades) = rpc.rpc_count()
|
||||
trades = rpc._rpc_count()
|
||||
nb_trades = len(trades)
|
||||
assert not error
|
||||
assert nb_trades == 0
|
||||
|
||||
# Create some test data
|
||||
freqtradebot.create_trade()
|
||||
(error, trades) = rpc.rpc_count()
|
||||
trades = rpc._rpc_count()
|
||||
nb_trades = len(trades)
|
||||
assert not error
|
||||
assert nb_trades == 1
|
||||
|
@@ -104,7 +104,7 @@ def test_send_msg_telegram_disabled(mocker, default_conf, caplog) -> None:
|
||||
rpc_manager = RPCManager(freqtradebot)
|
||||
rpc_manager.send_msg('test')
|
||||
|
||||
assert log_has('test', caplog.record_tuples)
|
||||
assert log_has('Sending rpc message: test', caplog.record_tuples)
|
||||
assert telegram_mock.call_count == 0
|
||||
|
||||
|
||||
@@ -119,5 +119,5 @@ def test_send_msg_telegram_enabled(mocker, default_conf, caplog) -> None:
|
||||
rpc_manager = RPCManager(freqtradebot)
|
||||
rpc_manager.send_msg('test')
|
||||
|
||||
assert log_has('test', caplog.record_tuples)
|
||||
assert log_has('Sending rpc message: test', caplog.record_tuples)
|
||||
assert telegram_mock.call_count == 1
|
||||
|
@@ -60,9 +60,7 @@ def test__init__(default_conf, mocker) -> None:
|
||||
|
||||
|
||||
def test_init(default_conf, mocker, caplog) -> None:
|
||||
"""
|
||||
Test _init() method
|
||||
"""
|
||||
""" Test _init() method """
|
||||
start_polling = MagicMock()
|
||||
mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock(return_value=start_polling))
|
||||
|
||||
@@ -256,7 +254,7 @@ def test_status(default_conf, update, mocker, fee, ticker) -> None:
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.rpc.telegram.Telegram',
|
||||
_init=MagicMock(),
|
||||
rpc_trade_status=MagicMock(return_value=(False, [1, 2, 3])),
|
||||
_rpc_trade_status=MagicMock(return_value=[1, 2, 3]),
|
||||
_status_table=status_table,
|
||||
_send_msg=msg_mock
|
||||
)
|
||||
@@ -667,7 +665,7 @@ def test_start_handle(default_conf, update, mocker) -> None:
|
||||
assert freqtradebot.state == State.STOPPED
|
||||
telegram._start(bot=MagicMock(), update=update)
|
||||
assert freqtradebot.state == State.RUNNING
|
||||
assert msg_mock.call_count == 0
|
||||
assert msg_mock.call_count == 1
|
||||
|
||||
|
||||
def test_start_handle_already_running(default_conf, update, mocker) -> None:
|
||||
|
Reference in New Issue
Block a user