2017-09-08 17:25:12 +00:00
|
|
|
import unittest
|
|
|
|
from unittest.mock import patch, MagicMock
|
2017-09-08 21:10:22 +00:00
|
|
|
from datetime import datetime
|
2017-09-08 17:25:12 +00:00
|
|
|
|
|
|
|
from jsonschema import validate
|
|
|
|
from telegram import Bot, Update, Message, Chat
|
|
|
|
|
|
|
|
import exchange
|
2017-09-08 22:31:40 +00:00
|
|
|
from main import init, create_trade
|
|
|
|
from misc import CONF_SCHEMA, update_state, State, get_state
|
2017-09-08 17:25:12 +00:00
|
|
|
from persistence import Trade
|
2017-09-08 18:03:45 +00:00
|
|
|
from rpc.telegram import _status, _profit, _forcesell, _performance, _start, _stop
|
2017-09-08 17:25:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MagicBot(MagicMock, Bot):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TestTelegram(unittest.TestCase):
|
|
|
|
|
|
|
|
conf = {
|
|
|
|
"max_open_trades": 3,
|
|
|
|
"stake_currency": "BTC",
|
|
|
|
"stake_amount": 0.05,
|
|
|
|
"dry_run": True,
|
|
|
|
"minimal_roi": {
|
|
|
|
"2880": 0.005,
|
|
|
|
"720": 0.01,
|
|
|
|
"0": 0.02
|
|
|
|
},
|
2017-09-17 20:21:46 +00:00
|
|
|
"bid_strategy": {
|
|
|
|
"ask_last_balance": 0.0
|
|
|
|
},
|
2017-09-08 17:25:12 +00:00
|
|
|
"bittrex": {
|
|
|
|
"enabled": True,
|
|
|
|
"key": "key",
|
|
|
|
"secret": "secret",
|
|
|
|
"pair_whitelist": [
|
|
|
|
"BTC_ETH"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"telegram": {
|
|
|
|
"enabled": True,
|
|
|
|
"token": "token",
|
|
|
|
"chat_id": "0"
|
2017-09-08 22:31:40 +00:00
|
|
|
},
|
|
|
|
"initial_state": "running"
|
2017-09-08 17:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def test_1_status_handle(self):
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.dict('main._CONF', self.conf):
|
2017-09-08 17:25:12 +00:00
|
|
|
with patch('main.get_buy_signal', side_effect=lambda _: True):
|
|
|
|
msg_mock = MagicMock()
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.multiple('main.telegram', _CONF=self.conf, init=MagicMock(), send_msg=msg_mock):
|
2017-09-08 17:25:12 +00:00
|
|
|
with patch.multiple('main.exchange',
|
|
|
|
get_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.07256061,
|
|
|
|
'ask': 0.072661,
|
|
|
|
'last': 0.07256061
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value='mocked_order_id')):
|
2017-09-08 19:39:31 +00:00
|
|
|
init(self.conf, 'sqlite://')
|
2017-09-08 17:25:12 +00:00
|
|
|
|
|
|
|
# Create some test data
|
|
|
|
trade = create_trade(15.0, exchange.Exchange.BITTREX)
|
2017-09-08 18:03:45 +00:00
|
|
|
self.assertTrue(trade)
|
2017-09-08 17:25:12 +00:00
|
|
|
Trade.session.add(trade)
|
|
|
|
Trade.session.flush()
|
|
|
|
|
|
|
|
_status(bot=MagicBot(), update=self.update)
|
|
|
|
self.assertEqual(msg_mock.call_count, 2)
|
|
|
|
self.assertIn('[BTC_ETH]', msg_mock.call_args_list[-1][0][0])
|
|
|
|
|
|
|
|
def test_2_profit_handle(self):
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.dict('main._CONF', self.conf):
|
2017-09-08 17:25:12 +00:00
|
|
|
with patch('main.get_buy_signal', side_effect=lambda _: True):
|
|
|
|
msg_mock = MagicMock()
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.multiple('main.telegram', _CONF=self.conf, init=MagicMock(), send_msg=msg_mock):
|
2017-09-08 17:25:12 +00:00
|
|
|
with patch.multiple('main.exchange',
|
|
|
|
get_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.07256061,
|
|
|
|
'ask': 0.072661,
|
|
|
|
'last': 0.07256061
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value='mocked_order_id')):
|
2017-09-08 19:39:31 +00:00
|
|
|
init(self.conf, 'sqlite://')
|
2017-09-08 17:25:12 +00:00
|
|
|
|
|
|
|
# Create some test data
|
|
|
|
trade = create_trade(15.0, exchange.Exchange.BITTREX)
|
2017-09-08 18:03:45 +00:00
|
|
|
self.assertTrue(trade)
|
2017-09-08 17:25:12 +00:00
|
|
|
trade.close_rate = 0.07256061
|
2017-09-08 18:03:45 +00:00
|
|
|
trade.close_profit = 100.00
|
2017-09-08 17:25:12 +00:00
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.is_open = False
|
|
|
|
Trade.session.add(trade)
|
|
|
|
Trade.session.flush()
|
|
|
|
|
|
|
|
_profit(bot=MagicBot(), update=self.update)
|
|
|
|
self.assertEqual(msg_mock.call_count, 2)
|
2017-09-08 18:03:45 +00:00
|
|
|
self.assertIn('(100.00%)', msg_mock.call_args_list[-1][0][0])
|
|
|
|
|
|
|
|
def test_3_forcesell_handle(self):
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.dict('main._CONF', self.conf):
|
2017-09-08 18:03:45 +00:00
|
|
|
with patch('main.get_buy_signal', side_effect=lambda _: True):
|
|
|
|
msg_mock = MagicMock()
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.multiple('main.telegram', _CONF=self.conf, init=MagicMock(), send_msg=msg_mock):
|
2017-09-08 18:03:45 +00:00
|
|
|
with patch.multiple('main.exchange',
|
|
|
|
get_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.07256061,
|
|
|
|
'ask': 0.072661,
|
|
|
|
'last': 0.07256061
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value='mocked_order_id')):
|
2017-09-08 19:39:31 +00:00
|
|
|
init(self.conf, 'sqlite://')
|
2017-09-08 18:03:45 +00:00
|
|
|
|
|
|
|
# Create some test data
|
|
|
|
trade = create_trade(15.0, exchange.Exchange.BITTREX)
|
|
|
|
self.assertTrue(trade)
|
|
|
|
Trade.session.add(trade)
|
|
|
|
Trade.session.flush()
|
|
|
|
|
|
|
|
self.update.message.text = '/forcesell 1'
|
|
|
|
_forcesell(bot=MagicBot(), update=self.update)
|
|
|
|
|
|
|
|
self.assertEqual(msg_mock.call_count, 2)
|
|
|
|
self.assertIn('Selling [BTC/ETH]', msg_mock.call_args_list[-1][0][0])
|
|
|
|
self.assertIn('0.072561', msg_mock.call_args_list[-1][0][0])
|
|
|
|
|
|
|
|
def test_4_performance_handle(self):
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.dict('main._CONF', self.conf):
|
2017-09-08 18:03:45 +00:00
|
|
|
with patch('main.get_buy_signal', side_effect=lambda _: True):
|
|
|
|
msg_mock = MagicMock()
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.multiple('main.telegram', _CONF=self.conf, init=MagicMock(), send_msg=msg_mock):
|
2017-09-08 18:03:45 +00:00
|
|
|
with patch.multiple('main.exchange',
|
|
|
|
get_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.07256061,
|
|
|
|
'ask': 0.072661,
|
|
|
|
'last': 0.07256061
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value='mocked_order_id')):
|
2017-09-08 19:39:31 +00:00
|
|
|
init(self.conf, 'sqlite://')
|
2017-09-08 18:03:45 +00:00
|
|
|
|
|
|
|
# Create some test data
|
|
|
|
trade = create_trade(15.0, exchange.Exchange.BITTREX)
|
|
|
|
self.assertTrue(trade)
|
|
|
|
trade.close_rate = 0.07256061
|
|
|
|
trade.close_profit = 100.00
|
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.is_open = False
|
|
|
|
Trade.session.add(trade)
|
|
|
|
Trade.session.flush()
|
|
|
|
|
|
|
|
_performance(bot=MagicBot(), update=self.update)
|
|
|
|
self.assertEqual(msg_mock.call_count, 2)
|
|
|
|
self.assertIn('Performance', msg_mock.call_args_list[-1][0][0])
|
|
|
|
self.assertIn('BTC_ETH 100.00%', msg_mock.call_args_list[-1][0][0])
|
|
|
|
|
|
|
|
def test_5_start_handle(self):
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.dict('main._CONF', self.conf):
|
2017-09-08 18:03:45 +00:00
|
|
|
msg_mock = MagicMock()
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.multiple('main.telegram', _CONF=self.conf, init=MagicMock(), send_msg=msg_mock):
|
2017-09-08 19:39:31 +00:00
|
|
|
init(self.conf, 'sqlite://')
|
2017-09-08 18:03:45 +00:00
|
|
|
|
2017-09-08 22:31:40 +00:00
|
|
|
update_state(State.STOPPED)
|
|
|
|
self.assertEqual(get_state(), State.STOPPED)
|
2017-09-08 18:03:45 +00:00
|
|
|
_start(bot=MagicBot(), update=self.update)
|
|
|
|
self.assertEqual(get_state(), State.RUNNING)
|
|
|
|
self.assertEqual(msg_mock.call_count, 0)
|
|
|
|
|
|
|
|
def test_6_stop_handle(self):
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.dict('main._CONF', self.conf):
|
2017-09-08 18:03:45 +00:00
|
|
|
msg_mock = MagicMock()
|
2017-09-08 21:10:22 +00:00
|
|
|
with patch.multiple('main.telegram', _CONF=self.conf, init=MagicMock(), send_msg=msg_mock):
|
2017-09-08 19:39:31 +00:00
|
|
|
init(self.conf, 'sqlite://')
|
2017-09-08 18:03:45 +00:00
|
|
|
|
|
|
|
update_state(State.RUNNING)
|
|
|
|
self.assertEqual(get_state(), State.RUNNING)
|
|
|
|
_stop(bot=MagicBot(), update=self.update)
|
2017-09-08 22:31:40 +00:00
|
|
|
self.assertEqual(get_state(), State.STOPPED)
|
2017-09-08 18:03:45 +00:00
|
|
|
self.assertEqual(msg_mock.call_count, 1)
|
|
|
|
self.assertIn('Stopping trader', msg_mock.call_args_list[0][0][0])
|
2017-09-08 17:25:12 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.update = Update(0)
|
2017-09-08 18:03:45 +00:00
|
|
|
self.update.message = Message(0, 0, datetime.utcnow(), Chat(0, 0))
|
2017-09-08 17:25:12 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-09-08 21:10:22 +00:00
|
|
|
validate(cls.conf, CONF_SCHEMA)
|
2017-09-08 17:25:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|