tests cleanup: Worker --> FreqtradeBot where the Worker object is not really needed

This commit is contained in:
hroff-1902 2019-04-02 22:36:30 +03:00
parent 62141d3d27
commit b0ddb33acc
2 changed files with 64 additions and 107 deletions

View File

@ -16,7 +16,6 @@ from freqtrade.rpc.fiat_convert import CryptoToFiatConverter
from freqtrade.state import State
from freqtrade.tests.conftest import patch_coinmarketcap, patch_exchange
from freqtrade.tests.test_freqtradebot import patch_get_signal
from freqtrade.worker import Worker
# Functions for recurrent object patching
@ -39,8 +38,7 @@ def test_rpc_trade_status(default_conf, ticker, fee, markets, mocker) -> None:
markets=PropertyMock(return_value=markets)
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -97,8 +95,7 @@ def test_rpc_status_table(default_conf, ticker, fee, markets, mocker) -> None:
markets=PropertyMock(return_value=markets)
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -135,8 +132,7 @@ def test_rpc_daily_profit(default_conf, update, ticker, fee,
markets=PropertyMock(return_value=markets)
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
stake_currency = default_conf['stake_currency']
@ -191,8 +187,7 @@ def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee,
markets=PropertyMock(return_value=markets)
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
stake_currency = default_conf['stake_currency']
@ -281,8 +276,7 @@ def test_rpc_trade_statistics_closed(mocker, default_conf, ticker, fee, markets,
markets=PropertyMock(return_value=markets)
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
stake_currency = default_conf['stake_currency']
@ -352,8 +346,7 @@ def test_rpc_balance_handle(default_conf, mocker):
get_ticker=MagicMock(side_effect=TemporaryError('Could not load ticker due to xxx'))
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -382,8 +375,7 @@ def test_rpc_start(mocker, default_conf) -> None:
get_ticker=MagicMock()
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -407,8 +399,7 @@ def test_rpc_stop(mocker, default_conf) -> None:
get_ticker=MagicMock()
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -433,8 +424,7 @@ def test_rpc_stopbuy(mocker, default_conf) -> None:
get_ticker=MagicMock()
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -467,8 +457,7 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker, markets) -> None:
markets=PropertyMock(return_value=markets)
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -561,8 +550,7 @@ def test_performance_handle(default_conf, ticker, limit_buy_order, fee,
markets=PropertyMock(return_value=markets)
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -599,8 +587,7 @@ def test_rpc_count(mocker, default_conf, ticker, fee, markets) -> None:
markets=PropertyMock(return_value=markets)
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -631,8 +618,7 @@ def test_rpcforcebuy(mocker, default_conf, ticker, fee, markets, limit_buy_order
buy=buy_mm
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -659,8 +645,7 @@ def test_rpcforcebuy(mocker, default_conf, ticker, fee, markets, limit_buy_order
# Test not buying
default_conf['stake_amount'] = 0.0000001
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -676,8 +661,7 @@ def test_rpcforcebuy_stopped(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -691,8 +675,7 @@ def test_rpcforcebuy_disabled(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -706,8 +689,7 @@ def test_rpc_whitelist(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
rpc = RPC(freqtradebot)
ret = rpc._rpc_whitelist()
@ -724,8 +706,7 @@ def test_rpc_whitelist_dynamic(mocker, default_conf) -> None:
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True))
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
rpc = RPC(freqtradebot)
ret = rpc._rpc_whitelist()

View File

@ -14,15 +14,15 @@ from telegram.error import NetworkError
from freqtrade import __version__
from freqtrade.edge import PairInfo
from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.persistence import Trade
from freqtrade.rpc import RPCMessageType
from freqtrade.rpc.telegram import Telegram, authorized_only
from freqtrade.state import State
from freqtrade.strategy.interface import SellType
from freqtrade.tests.conftest import (get_patched_freqtradebot, get_patched_worker,
from freqtrade.tests.conftest import (get_patched_freqtradebot,
log_has, patch_coinmarketcap, patch_exchange)
from freqtrade.tests.test_freqtradebot import patch_get_signal
from freqtrade.worker import Worker
class DummyCls(Telegram):
@ -99,8 +99,7 @@ def test_authorized_only(default_conf, mocker, caplog) -> None:
default_conf['telegram']['enabled'] = False
worker = Worker(args=None, config=default_conf)
bot = worker.freqtrade
bot = FreqtradeBot(config=default_conf)
patch_get_signal(bot, (True, False))
dummy = DummyCls(bot)
@ -129,8 +128,7 @@ def test_authorized_only_unauthorized(default_conf, mocker, caplog) -> None:
default_conf['telegram']['enabled'] = False
worker = Worker(args=None, config=default_conf)
bot = worker.freqtrade
bot = FreqtradeBot(config=default_conf)
patch_get_signal(bot, (True, False))
dummy = DummyCls(bot)
@ -159,8 +157,7 @@ def test_authorized_only_exception(default_conf, mocker, caplog) -> None:
default_conf['telegram']['enabled'] = False
worker = Worker(args=None, config=default_conf)
bot = worker.freqtrade
bot = FreqtradeBot(config=default_conf)
patch_get_signal(bot, (True, False))
dummy = DummyCls(bot)
@ -216,8 +213,7 @@ def test_status(default_conf, update, mocker, fee, ticker, markets) -> None:
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -254,21 +250,20 @@ def test_status_handle(default_conf, update, ticker, fee, markets, mocker) -> No
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
worker.state = State.STOPPED
freqtradebot.state = State.STOPPED
# Status is also enabled when stopped
telegram._status(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
assert 'no active trade' in msg_mock.call_args_list[0][0][0]
msg_mock.reset_mock()
worker.state = State.RUNNING
freqtradebot.state = State.RUNNING
telegram._status(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
assert 'no active trade' in msg_mock.call_args_list[0][0][0]
@ -303,21 +298,20 @@ def test_status_table_handle(default_conf, update, ticker, fee, markets, mocker)
default_conf['stake_amount'] = 15.0
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
worker.state = State.STOPPED
freqtradebot.state = State.STOPPED
# Status table is also enabled when stopped
telegram._status_table(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
assert 'no active order' in msg_mock.call_args_list[0][0][0]
msg_mock.reset_mock()
worker.state = State.RUNNING
freqtradebot.state = State.RUNNING
telegram._status_table(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
assert 'no active order' in msg_mock.call_args_list[0][0][0]
@ -359,8 +353,7 @@ def test_daily_handle(default_conf, update, ticker, limit_buy_order, fee,
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -426,15 +419,14 @@ def test_daily_wrong_input(default_conf, update, ticker, mocker) -> None:
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
# Try invalid data
msg_mock.reset_mock()
worker.state = State.RUNNING
freqtradebot.state = State.RUNNING
update.message.text = '/daily -2'
telegram._daily(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
@ -442,7 +434,7 @@ def test_daily_wrong_input(default_conf, update, ticker, mocker) -> None:
# Try invalid data
msg_mock.reset_mock()
worker.state = State.RUNNING
freqtradebot.state = State.RUNNING
update.message.text = '/daily today'
telegram._daily(bot=MagicMock(), update=update)
assert str('Daily Profit over the last 7 days') in msg_mock.call_args_list[0][0][0]
@ -467,8 +459,7 @@ def test_profit_handle(default_conf, update, ticker, ticker_sell_up, fee,
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -614,14 +605,13 @@ def test_start_handle(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
telegram = Telegram(freqtradebot)
worker.state = State.STOPPED
assert worker.state == State.STOPPED
freqtradebot.state = State.STOPPED
assert freqtradebot.state == State.STOPPED
telegram._start(bot=MagicMock(), update=update)
assert worker.state == State.RUNNING
assert freqtradebot.state == State.RUNNING
assert msg_mock.call_count == 1
@ -633,14 +623,13 @@ def test_start_handle_already_running(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
telegram = Telegram(freqtradebot)
worker.state = State.RUNNING
assert worker.state == State.RUNNING
freqtradebot.state = State.RUNNING
assert freqtradebot.state == State.RUNNING
telegram._start(bot=MagicMock(), update=update)
assert worker.state == State.RUNNING
assert freqtradebot.state == State.RUNNING
assert msg_mock.call_count == 1
assert 'already running' in msg_mock.call_args_list[0][0][0]
@ -654,14 +643,13 @@ def test_stop_handle(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
telegram = Telegram(freqtradebot)
worker.state = State.RUNNING
assert worker.state == State.RUNNING
freqtradebot.state = State.RUNNING
assert freqtradebot.state == State.RUNNING
telegram._stop(bot=MagicMock(), update=update)
assert worker.state == State.STOPPED
assert freqtradebot.state == State.STOPPED
assert msg_mock.call_count == 1
assert 'stopping trader' in msg_mock.call_args_list[0][0][0]
@ -675,14 +663,13 @@ def test_stop_handle_already_stopped(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
telegram = Telegram(freqtradebot)
worker.state = State.STOPPED
assert worker.state == State.STOPPED
freqtradebot.state = State.STOPPED
assert freqtradebot.state == State.STOPPED
telegram._stop(bot=MagicMock(), update=update)
assert worker.state == State.STOPPED
assert freqtradebot.state == State.STOPPED
assert msg_mock.call_count == 1
assert 'already stopped' in msg_mock.call_args_list[0][0][0]
@ -696,9 +683,7 @@ def test_stopbuy_handle(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
telegram = Telegram(freqtradebot)
assert freqtradebot.config['max_open_trades'] != 0
@ -718,14 +703,13 @@ def test_reload_conf_handle(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
telegram = Telegram(freqtradebot)
worker.state = State.RUNNING
assert worker.state == State.RUNNING
freqtradebot.state = State.RUNNING
assert freqtradebot.state == State.RUNNING
telegram._reload_conf(bot=MagicMock(), update=update)
assert worker.state == State.RELOAD_CONF
assert freqtradebot.state == State.RELOAD_CONF
assert msg_mock.call_count == 1
assert 'reloading config' in msg_mock.call_args_list[0][0][0]
@ -745,8 +729,7 @@ def test_forcesell_handle(default_conf, update, ticker, fee,
validate_pairs=MagicMock(return_value={})
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -798,8 +781,7 @@ def test_forcesell_down_handle(default_conf, update, ticker, fee,
validate_pairs=MagicMock(return_value={})
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -854,8 +836,7 @@ def test_forcesell_all_handle(default_conf, update, ticker, fee, markets, mocker
validate_pairs=MagicMock(return_value={})
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -899,8 +880,7 @@ def test_forcesell_handle_invalid(default_conf, update, mocker) -> None:
)
patch_exchange(mocker)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -943,8 +923,7 @@ def test_forcebuy_handle(default_conf, update, markets, mocker) -> None:
fbuy_mock = MagicMock(return_value=None)
mocker.patch('freqtrade.rpc.RPC._rpc_forcebuy', fbuy_mock)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -980,8 +959,7 @@ def test_forcebuy_handle_exception(default_conf, update, markets, mocker) -> Non
validate_pairs=MagicMock(return_value={})
)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -1012,8 +990,7 @@ def test_performance_handle(default_conf, update, ticker, fee,
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -1054,8 +1031,7 @@ def test_count_handle(default_conf, update, ticker, fee, markets, mocker) -> Non
)
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
freqtradebot = FreqtradeBot(config=default_conf)
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)