stable/freqtrade/tests/rpc/test_rpc_telegram.py

719 lines
28 KiB
Python
Raw Normal View History

2017-11-18 08:09:19 +00:00
# pragma pylint: disable=missing-docstring, too-many-arguments, too-many-ancestors, C0103
2018-01-28 07:38:41 +00:00
# pragma pylint: disable=unused-argument
2017-11-05 15:26:03 +00:00
import re
from datetime import datetime
2017-11-07 21:27:44 +00:00
from random import randint
from unittest.mock import MagicMock
2017-09-08 17:25:12 +00:00
2017-11-09 22:45:03 +00:00
from sqlalchemy import create_engine
2017-11-09 21:11:02 +00:00
from telegram import Update, Message, Chat
2017-11-09 19:02:41 +00:00
from telegram.error import NetworkError
2017-09-08 17:25:12 +00:00
2017-11-09 22:51:32 +00:00
from freqtrade import __version__
2017-09-28 21:26:28 +00:00
from freqtrade.main import init, create_trade
2017-11-07 19:12:56 +00:00
from freqtrade.misc import update_state, State, get_state
2017-09-28 21:26:28 +00:00
from freqtrade.persistence import Trade
2017-11-09 19:02:41 +00:00
from freqtrade.rpc import telegram
from freqtrade.rpc.telegram import authorized_only, is_enabled, send_msg, _status, _status_table, \
_profit, _forcesell, _performance, _daily, _count, _start, _stop, _balance, _version, _help, \
_exec_forcesell
2017-09-08 17:25:12 +00:00
2017-11-09 19:02:41 +00:00
def test_is_enabled(default_conf, mocker):
mocker.patch.dict('freqtrade.rpc.telegram._CONF', default_conf)
default_conf['telegram']['enabled'] = False
assert is_enabled() is False
def test_init_disabled(default_conf, mocker):
mocker.patch.dict('freqtrade.rpc.telegram._CONF', default_conf)
default_conf['telegram']['enabled'] = False
telegram.init(default_conf)
2017-11-07 21:27:44 +00:00
def test_authorized_only(default_conf, mocker):
mocker.patch.dict('freqtrade.rpc.telegram._CONF', default_conf)
chat = Chat(0, 0)
update = Update(randint(1, 100))
update.message = Message(randint(1, 100), 0, datetime.utcnow(), chat)
state = {'called': False}
@authorized_only
def dummy_handler(*args, **kwargs) -> None:
state['called'] = True
dummy_handler(MagicMock(), update)
assert state['called'] is True
def test_authorized_only_unauthorized(default_conf, mocker):
mocker.patch.dict('freqtrade.rpc.telegram._CONF', default_conf)
chat = Chat(0xdeadbeef, 0)
update = Update(randint(1, 100))
update.message = Message(randint(1, 100), 0, datetime.utcnow(), chat)
state = {'called': False}
@authorized_only
def dummy_handler(*args, **kwargs) -> None:
state['called'] = True
dummy_handler(MagicMock(), update)
assert state['called'] is False
2017-11-09 19:02:41 +00:00
def test_authorized_only_exception(default_conf, mocker):
mocker.patch.dict('freqtrade.rpc.telegram._CONF', default_conf)
update = Update(randint(1, 100))
update.message = Message(randint(1, 100), 0, datetime.utcnow(), Chat(0, 0))
@authorized_only
def dummy_handler(*args, **kwargs) -> None:
raise Exception('test')
dummy_handler(MagicMock(), update)
2017-11-07 19:12:56 +00:00
def test_status_handle(default_conf, update, ticker, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, False))
msg_mock = MagicMock()
mocker.patch('freqtrade.main.rpc.send_msg', MagicMock())
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-04 17:55:41 +00:00
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
2017-10-01 21:28:09 +00:00
validate_pairs=MagicMock(),
2017-11-07 19:12:56 +00:00
get_ticker=ticker)
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
2017-11-09 19:02:41 +00:00
update_state(State.STOPPED)
2017-11-09 21:11:02 +00:00
_status(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
assert 'trader is not running' in msg_mock.call_args_list[0][0][0]
msg_mock.reset_mock()
update_state(State.RUNNING)
2017-11-09 21:11:02 +00:00
_status(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
assert 'no active trade' in msg_mock.call_args_list[0][0][0]
msg_mock.reset_mock()
# Create some test data
create_trade(0.001, int(default_conf['ticker_interval']))
2017-10-31 23:26:32 +00:00
# Trigger status while we have a fulfilled order for the open trade
2017-11-09 21:11:02 +00:00
_status(bot=MagicMock(), update=update)
2017-10-31 23:26:32 +00:00
assert msg_mock.call_count == 1
assert '[BTC_ETH]' in msg_mock.call_args_list[0][0][0]
2017-11-05 15:26:03 +00:00
2017-11-07 19:12:56 +00:00
def test_status_table_handle(default_conf, update, ticker, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, False))
2017-11-05 15:26:03 +00:00
msg_mock = MagicMock()
mocker.patch('freqtrade.main.rpc.send_msg', MagicMock())
2017-11-06 17:01:13 +00:00
mocker.patch.multiple(
'freqtrade.rpc.telegram',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-06 17:01:13 +00:00
init=MagicMock(),
send_msg=msg_mock)
2017-11-05 15:26:03 +00:00
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
2017-11-07 19:12:56 +00:00
get_ticker=ticker,
2017-11-05 15:26:03 +00:00
buy=MagicMock(return_value='mocked_order_id'))
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
2017-11-09 19:02:41 +00:00
update_state(State.STOPPED)
2017-11-09 21:11:02 +00:00
_status_table(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
assert 'trader is not running' in msg_mock.call_args_list[0][0][0]
msg_mock.reset_mock()
update_state(State.RUNNING)
2017-11-09 21:11:02 +00:00
_status_table(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
assert 'no active order' in msg_mock.call_args_list[0][0][0]
msg_mock.reset_mock()
2017-11-05 15:26:03 +00:00
# Create some test data
create_trade(15.0, int(default_conf['ticker_interval']))
2017-11-05 15:26:03 +00:00
2017-11-09 21:11:02 +00:00
_status_table(bot=MagicMock(), update=update)
2017-11-05 15:26:03 +00:00
2017-11-07 19:12:56 +00:00
text = re.sub('</?pre>', '', msg_mock.call_args_list[-1][0][0])
2017-11-05 15:26:03 +00:00
line = text.split("\n")
fields = re.sub('[ ]+', ' ', line[2].strip()).split(' ')
assert int(fields[0]) == 1
assert fields[1] == 'BTC_ETH'
assert msg_mock.call_count == 1
2017-11-05 15:26:03 +00:00
def test_profit_handle(
default_conf, update, ticker, ticker_sell_up, limit_buy_order, limit_sell_order, mocker):
2017-11-07 19:12:56 +00:00
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, False))
msg_mock = MagicMock()
mocker.patch('freqtrade.main.rpc.send_msg', MagicMock())
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-04 17:55:41 +00:00
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
2017-10-01 21:28:09 +00:00
validate_pairs=MagicMock(),
2017-11-07 19:12:56 +00:00
get_ticker=ticker)
2017-12-25 07:51:41 +00:00
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.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
2017-11-09 21:11:02 +00:00
_profit(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
assert 'no closed trade' in msg_mock.call_args_list[0][0][0]
msg_mock.reset_mock()
# Create some test data
create_trade(0.001, int(default_conf['ticker_interval']))
trade = Trade.query.first()
2017-10-31 23:26:32 +00:00
# Simulate fulfilled LIMIT_BUY order for trade
2017-11-07 19:12:56 +00:00
trade.update(limit_buy_order)
2017-11-09 19:02:41 +00:00
2017-11-09 21:11:02 +00:00
_profit(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
2017-11-09 19:02:41 +00:00
assert 'no closed trade' in msg_mock.call_args_list[-1][0][0]
msg_mock.reset_mock()
2017-12-17 21:07:56 +00:00
# Update the ticker with a market going up
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker_sell_up)
2017-11-07 19:12:56 +00:00
trade.update(limit_sell_order)
2017-10-31 23:26:32 +00:00
trade.close_date = datetime.utcnow()
trade.is_open = False
2017-11-09 21:11:02 +00:00
_profit(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
2017-12-25 07:51:41 +00:00
assert '*ROI:* Close trades' in msg_mock.call_args_list[-1][0][0]
assert '∙ `0.00006217 BTC (6.20%)`' in msg_mock.call_args_list[-1][0][0]
assert '∙ `0.933 USD`' in msg_mock.call_args_list[-1][0][0]
assert '*ROI:* All trades' in msg_mock.call_args_list[-1][0][0]
assert '∙ `0.00006217 BTC (6.20%)`' in msg_mock.call_args_list[-1][0][0]
assert '∙ `0.933 USD`' in msg_mock.call_args_list[-1][0][0]
assert '*Best Performing:* `BTC_ETH: 6.20%`' in msg_mock.call_args_list[-1][0][0]
2017-12-17 21:07:56 +00:00
def test_forcesell_handle(default_conf, update, ticker, ticker_sell_up, mocker):
2017-11-07 19:12:56 +00:00
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, False))
rpc_mock = mocker.patch('freqtrade.main.rpc.send_msg', MagicMock())
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-04 17:55:41 +00:00
init=MagicMock(),
send_msg=MagicMock())
mocker.patch.multiple('freqtrade.main.exchange',
2017-10-01 21:28:09 +00:00
validate_pairs=MagicMock(),
2017-11-07 19:12:56 +00:00
get_ticker=ticker)
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
# Create some test data
create_trade(0.001, int(default_conf['ticker_interval']))
2017-10-31 23:26:32 +00:00
trade = Trade.query.first()
assert trade
2017-12-17 21:07:56 +00:00
# Increase the price and sell it
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker_sell_up)
update.message.text = '/forcesell 1'
2017-11-09 21:11:02 +00:00
_forcesell(bot=MagicMock(), update=update)
assert rpc_mock.call_count == 2
assert 'Selling' in rpc_mock.call_args_list[-1][0][0]
assert '[BTC_ETH]' in rpc_mock.call_args_list[-1][0][0]
assert 'Amount' in rpc_mock.call_args_list[-1][0][0]
2017-12-25 07:51:41 +00:00
assert '0.00001172' in rpc_mock.call_args_list[-1][0][0]
assert 'profit: 6.11%, 0.00006126' in rpc_mock.call_args_list[-1][0][0]
2017-12-25 07:51:41 +00:00
assert '0.919 USD' in rpc_mock.call_args_list[-1][0][0]
2017-12-17 21:07:56 +00:00
def test_forcesell_down_handle(default_conf, update, ticker, ticker_sell_down, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, False))
2017-12-17 21:07:56 +00:00
rpc_mock = mocker.patch('freqtrade.main.rpc.send_msg', MagicMock())
mocker.patch.multiple('freqtrade.rpc.telegram',
_CONF=default_conf,
init=MagicMock(),
send_msg=MagicMock())
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker)
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
2017-12-17 21:07:56 +00:00
init(default_conf, create_engine('sqlite://'))
# Create some test data
create_trade(0.001, int(default_conf['ticker_interval']))
2017-12-17 21:07:56 +00:00
# Decrease the price and sell it
2017-12-17 21:07:56 +00:00
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker_sell_down)
trade = Trade.query.first()
assert trade
update.message.text = '/forcesell 1'
_forcesell(bot=MagicMock(), update=update)
assert rpc_mock.call_count == 2
assert 'Selling' in rpc_mock.call_args_list[-1][0][0]
assert '[BTC_ETH]' in rpc_mock.call_args_list[-1][0][0]
assert 'Amount' in rpc_mock.call_args_list[-1][0][0]
2017-12-25 07:51:41 +00:00
assert '0.00001044' in rpc_mock.call_args_list[-1][0][0]
assert 'loss: -5.48%, -0.00005492' in rpc_mock.call_args_list[-1][0][0]
2017-12-25 07:51:41 +00:00
assert '-0.824 USD' in rpc_mock.call_args_list[-1][0][0]
def test_exec_forcesell_open_orders(default_conf, ticker, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
cancel_order_mock = MagicMock()
mocker.patch.multiple('freqtrade.main.exchange',
get_ticker=ticker,
get_order=MagicMock(return_value={
'closed': None,
'type': 'LIMIT_BUY',
}),
cancel_order=cancel_order_mock)
trade = Trade(
pair='BTC_ETH',
open_rate=1,
exchange='BITTREX',
open_order_id='123456789',
amount=1,
fee=0.0,
)
_exec_forcesell(trade)
assert cancel_order_mock.call_count == 1
assert trade.is_open is False
2017-11-09 21:08:28 +00:00
def test_forcesell_all_handle(default_conf, update, ticker, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, False))
rpc_mock = mocker.patch('freqtrade.main.rpc.send_msg', MagicMock())
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-09 21:08:28 +00:00
_CONF=default_conf,
init=MagicMock(),
send_msg=MagicMock())
2017-11-09 21:08:28 +00:00
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker)
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
2017-11-09 21:08:28 +00:00
# Create some test data
for _ in range(4):
create_trade(0.001, int(default_conf['ticker_interval']))
rpc_mock.reset_mock()
2017-11-09 21:08:28 +00:00
update.message.text = '/forcesell all'
2017-11-09 21:11:02 +00:00
_forcesell(bot=MagicMock(), update=update)
2017-11-09 21:08:28 +00:00
assert rpc_mock.call_count == 4
for args in rpc_mock.call_args_list:
2017-12-25 07:51:41 +00:00
assert '0.00001098' in args[0][0]
assert 'loss: -0.59%, -0.00000591 BTC' in args[0][0]
2017-12-25 07:51:41 +00:00
assert '-0.089 USD' in args[0][0]
2017-11-09 21:08:28 +00:00
2017-11-09 19:02:41 +00:00
def test_forcesell_handle_invalid(default_conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, True))
2017-11-09 19:02:41 +00:00
msg_mock = MagicMock()
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-09 19:02:41 +00:00
_CONF=default_conf,
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock())
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
2017-11-09 19:02:41 +00:00
# Trader is not running
update_state(State.STOPPED)
update.message.text = '/forcesell 1'
2017-11-09 21:11:02 +00:00
_forcesell(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
assert 'not running' in msg_mock.call_args_list[0][0][0]
# No argument
msg_mock.reset_mock()
update_state(State.RUNNING)
update.message.text = '/forcesell'
2017-11-09 21:11:02 +00:00
_forcesell(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
assert 'Invalid argument' in msg_mock.call_args_list[0][0][0]
# Invalid argument
msg_mock.reset_mock()
update_state(State.RUNNING)
update.message.text = '/forcesell 123456'
2017-11-09 21:11:02 +00:00
_forcesell(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
2017-11-09 21:08:28 +00:00
assert 'Invalid argument.' in msg_mock.call_args_list[0][0][0]
2017-11-09 19:02:41 +00:00
2017-11-09 20:47:47 +00:00
def test_performance_handle(
default_conf, update, ticker, limit_buy_order, limit_sell_order, mocker):
2017-11-07 19:12:56 +00:00
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, False))
msg_mock = MagicMock()
mocker.patch('freqtrade.main.rpc.send_msg', MagicMock())
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-04 17:55:41 +00:00
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
2017-10-01 21:28:09 +00:00
validate_pairs=MagicMock(),
2017-11-07 19:12:56 +00:00
get_ticker=ticker)
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
# Create some test data
create_trade(0.001, int(default_conf['ticker_interval']))
trade = Trade.query.first()
assert trade
2017-10-31 23:26:32 +00:00
# Simulate fulfilled LIMIT_BUY order for trade
2017-11-07 19:12:56 +00:00
trade.update(limit_buy_order)
2017-10-31 23:26:32 +00:00
# Simulate fulfilled LIMIT_SELL order for trade
2017-11-07 19:12:56 +00:00
trade.update(limit_sell_order)
2017-10-31 23:26:32 +00:00
trade.close_date = datetime.utcnow()
trade.is_open = False
2017-11-09 21:11:02 +00:00
_performance(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
assert 'Performance' in msg_mock.call_args_list[0][0][0]
assert '<code>BTC_ETH\t6.20% (1)</code>' in msg_mock.call_args_list[0][0][0]
2017-12-17 21:07:56 +00:00
def test_daily_handle(default_conf, update, ticker, limit_buy_order, limit_sell_order, mocker):
2017-12-10 06:32:40 +00:00
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, False))
2017-12-10 06:32:40 +00:00
msg_mock = MagicMock()
mocker.patch('freqtrade.main.rpc.send_msg', MagicMock())
mocker.patch.multiple('freqtrade.rpc.telegram',
_CONF=default_conf,
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker)
2017-12-25 07:51:41 +00:00
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.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
2017-12-10 06:32:40 +00:00
init(default_conf, create_engine('sqlite://'))
2017-12-16 02:39:47 +00:00
2017-12-10 06:32:40 +00:00
# Create some test data
create_trade(0.001, int(default_conf['ticker_interval']))
2017-12-10 06:32:40 +00:00
trade = Trade.query.first()
assert trade
# Simulate fulfilled LIMIT_BUY order for trade
trade.update(limit_buy_order)
2017-12-10 06:32:40 +00:00
# Simulate fulfilled LIMIT_SELL order for trade
trade.update(limit_sell_order)
trade.close_date = datetime.utcnow()
trade.is_open = False
2017-12-17 21:07:56 +00:00
# Try valid data
update.message.text = '/daily 2'
2017-12-10 06:32:40 +00:00
_daily(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
assert 'Daily' in msg_mock.call_args_list[0][0][0]
2017-12-25 07:51:41 +00:00
assert str(datetime.utcnow().date()) in msg_mock.call_args_list[0][0][0]
assert str(' 0.00006217 BTC') in msg_mock.call_args_list[0][0][0]
assert str(' 0.933 USD') in msg_mock.call_args_list[0][0][0]
2018-01-20 06:09:28 +00:00
assert str(' 1 trade') in msg_mock.call_args_list[0][0][0]
assert str(' 0 trade') in msg_mock.call_args_list[0][0][0]
# Reset msg_mock
msg_mock.reset_mock()
# Add two other trades
create_trade(0.001, int(default_conf['ticker_interval']))
create_trade(0.001, int(default_conf['ticker_interval']))
2018-01-20 06:09:28 +00:00
trades = Trade.query.all()
for trade in trades:
trade.update(limit_buy_order)
trade.update(limit_sell_order)
trade.close_date = datetime.utcnow()
trade.is_open = False
update.message.text = '/daily 1'
_daily(bot=MagicMock(), update=update)
assert str(' 0.00018651 BTC') in msg_mock.call_args_list[0][0][0]
assert str(' 2.798 USD') in msg_mock.call_args_list[0][0][0]
assert str(' 3 trades') in msg_mock.call_args_list[0][0][0]
2017-12-16 02:39:47 +00:00
def test_daily_wrong_input(default_conf, update, ticker, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, False))
msg_mock = MagicMock()
mocker.patch('freqtrade.main.rpc.send_msg', MagicMock())
mocker.patch.multiple('freqtrade.rpc.telegram',
_CONF=default_conf,
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker)
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.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
init(default_conf, create_engine('sqlite://'))
2017-12-17 21:07:56 +00:00
# Try invalid data
msg_mock.reset_mock()
update_state(State.RUNNING)
update.message.text = '/daily -2'
_daily(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
2017-12-16 02:39:47 +00:00
assert 'must be an integer greater than 0' in msg_mock.call_args_list[0][0][0]
# Try invalid data
msg_mock.reset_mock()
update_state(State.RUNNING)
update.message.text = '/daily today'
_daily(bot=MagicMock(), update=update)
assert str('Daily Profit over the last 7 days') in msg_mock.call_args_list[0][0][0]
2017-12-16 02:39:47 +00:00
2017-11-07 19:12:56 +00:00
def test_count_handle(default_conf, update, ticker, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, False))
2017-11-05 15:26:03 +00:00
msg_mock = MagicMock()
2017-11-06 17:01:13 +00:00
mocker.patch.multiple(
'freqtrade.rpc.telegram',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-06 17:01:13 +00:00
init=MagicMock(),
send_msg=msg_mock)
2017-11-05 15:26:03 +00:00
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
2017-11-07 19:12:56 +00:00
get_ticker=ticker,
2017-11-05 15:26:03 +00:00
buy=MagicMock(return_value='mocked_order_id'))
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
2017-11-09 19:02:41 +00:00
update_state(State.STOPPED)
2017-11-09 21:11:02 +00:00
_count(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
assert 'not running' in msg_mock.call_args_list[0][0][0]
msg_mock.reset_mock()
update_state(State.RUNNING)
2017-11-05 15:26:03 +00:00
# Create some test data
create_trade(0.001, int(default_conf['ticker_interval']))
2017-11-09 22:45:03 +00:00
msg_mock.reset_mock()
2017-11-09 21:11:02 +00:00
_count(bot=MagicMock(), update=update)
2017-11-09 22:45:03 +00:00
msg = '<pre> current max\n--------- -----\n 1 {}</pre>'.format(
default_conf['max_open_trades']
)
assert msg in msg_mock.call_args_list[0][0][0]
2017-11-05 15:26:03 +00:00
2017-11-09 19:02:41 +00:00
def test_performance_handle_invalid(default_conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (True, True))
2017-11-09 19:02:41 +00:00
msg_mock = MagicMock()
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-09 19:02:41 +00:00
_CONF=default_conf,
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock())
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
2017-11-09 19:02:41 +00:00
# Trader is not running
update_state(State.STOPPED)
2017-11-09 21:11:02 +00:00
_performance(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
assert 'not running' in msg_mock.call_args_list[0][0][0]
2017-11-07 19:12:56 +00:00
def test_start_handle(default_conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
msg_mock = MagicMock()
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-04 17:55:41 +00:00
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-04 17:55:41 +00:00
init=MagicMock())
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
update_state(State.STOPPED)
assert get_state() == State.STOPPED
2017-11-09 21:11:02 +00:00
_start(bot=MagicMock(), update=update)
assert get_state() == State.RUNNING
assert msg_mock.call_count == 0
2017-10-30 23:36:35 +00:00
2017-11-09 19:02:41 +00:00
def test_start_handle_already_running(default_conf, update, mocker):
2017-11-07 19:12:56 +00:00
mocker.patch.dict('freqtrade.main._CONF', default_conf)
msg_mock = MagicMock()
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-04 17:55:41 +00:00
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-04 17:55:41 +00:00
init=MagicMock())
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
2017-11-09 19:02:41 +00:00
update_state(State.RUNNING)
assert get_state() == State.RUNNING
2017-11-09 21:11:02 +00:00
_start(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert get_state() == State.RUNNING
assert msg_mock.call_count == 1
assert 'already running' in msg_mock.call_args_list[0][0][0]
2017-11-09 19:02:41 +00:00
def test_stop_handle(default_conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
msg_mock = MagicMock()
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-09 19:02:41 +00:00
_CONF=default_conf,
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
_CONF=default_conf,
init=MagicMock())
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
update_state(State.RUNNING)
assert get_state() == State.RUNNING
2017-11-09 21:11:02 +00:00
_stop(bot=MagicMock(), update=update)
assert get_state() == State.STOPPED
assert msg_mock.call_count == 1
assert 'Stopping trader' in msg_mock.call_args_list[0][0][0]
2017-10-29 08:10:00 +00:00
2017-10-30 23:36:35 +00:00
2017-11-09 19:02:41 +00:00
def test_stop_handle_already_stopped(default_conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
msg_mock = MagicMock()
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-09 19:02:41 +00:00
_CONF=default_conf,
init=MagicMock(),
send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
_CONF=default_conf,
init=MagicMock())
2017-11-09 22:45:03 +00:00
init(default_conf, create_engine('sqlite://'))
2017-11-09 19:02:41 +00:00
update_state(State.STOPPED)
assert get_state() == State.STOPPED
2017-11-09 21:11:02 +00:00
_stop(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert get_state() == State.STOPPED
assert msg_mock.call_count == 1
assert 'already stopped' in msg_mock.call_args_list[0][0][0]
2017-11-07 19:12:56 +00:00
def test_balance_handle(default_conf, update, mocker):
2017-10-29 08:10:00 +00:00
mock_balance = [{
'Currency': 'BTC',
'Balance': 10.0,
'Available': 12.0,
'Pending': 0.0,
2017-11-09 19:02:41 +00:00
'CryptoAddress': 'XXXX',
}, {
'Currency': 'ETH',
'Balance': 0.0,
'Available': 0.0,
'Pending': 0.0,
'CryptoAddress': 'XXXX',
}]
2017-11-07 19:12:56 +00:00
mocker.patch.dict('freqtrade.main._CONF', default_conf)
2017-10-29 08:10:00 +00:00
msg_mock = MagicMock()
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-07 19:12:56 +00:00
_CONF=default_conf,
2017-11-04 17:55:41 +00:00
init=MagicMock(),
send_msg=msg_mock)
2017-10-29 08:10:00 +00:00
mocker.patch.multiple('freqtrade.main.exchange',
get_balances=MagicMock(return_value=mock_balance))
2018-01-15 05:07:01 +00:00
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
ticker=MagicMock(return_value={'price_usd': 15000.0}),
_cache_symbols=MagicMock(return_value={'BTC': 1}))
2017-10-29 08:10:00 +00:00
2017-11-09 21:11:02 +00:00
_balance(bot=MagicMock(), update=update)
2017-10-29 08:10:00 +00:00
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]
2018-01-15 05:07:01 +00:00
assert 'Est. BTC' in msg_mock.call_args_list[0][0][0]
2017-11-09 19:02:41 +00:00
def test_help_handle(default_conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
msg_mock = MagicMock()
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-09 19:02:41 +00:00
_CONF=default_conf,
init=MagicMock(),
send_msg=msg_mock)
2017-11-09 21:11:02 +00:00
_help(bot=MagicMock(), update=update)
2017-11-09 19:02:41 +00:00
assert msg_mock.call_count == 1
assert '*/help:* `This help message`' in msg_mock.call_args_list[0][0][0]
2017-11-09 22:51:32 +00:00
def test_version_handle(default_conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
msg_mock = MagicMock()
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-09 22:51:32 +00:00
_CONF=default_conf,
init=MagicMock(),
send_msg=msg_mock)
_version(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
assert '*Version:* `{}`'.format(__version__) in msg_mock.call_args_list[0][0][0]
2017-11-09 19:02:41 +00:00
def test_send_msg(default_conf, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-09 19:02:41 +00:00
_CONF=default_conf,
init=MagicMock())
bot = MagicMock()
send_msg('test', bot)
2017-11-18 08:09:19 +00:00
assert not bot.method_calls
2017-11-09 19:02:41 +00:00
bot.reset_mock()
default_conf['telegram']['enabled'] = True
send_msg('test', bot)
assert len(bot.method_calls) == 1
def test_send_msg_network_error(default_conf, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch.multiple('freqtrade.rpc.telegram',
2017-11-09 19:02:41 +00:00
_CONF=default_conf,
init=MagicMock())
default_conf['telegram']['enabled'] = True
bot = MagicMock()
bot.send_message = MagicMock(side_effect=NetworkError('Oh snap'))
2017-11-17 18:47:29 +00:00
send_msg('test', bot)
2017-11-09 19:02:41 +00:00
# Bot should've tried to send it twice
assert len(bot.method_calls) == 2