tests adjusted

This commit is contained in:
hroff-1902 2019-03-26 11:07:24 +03:00
parent 5161e1abb3
commit 5ccd618189
5 changed files with 228 additions and 129 deletions

View File

@ -15,6 +15,7 @@ from freqtrade import constants
from freqtrade.data.converter import parse_ticker_dataframe
from freqtrade.exchange import Exchange
from freqtrade.edge import Edge, PairInfo
from freqtrade.worker import Worker
from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.resolvers import ExchangeResolver
@ -88,7 +89,7 @@ def get_patched_edge(mocker, config) -> Edge:
# Functions for recurrent object patching
def get_patched_freqtradebot(mocker, config) -> FreqtradeBot:
def patch_freqtradebot(mocker, config) -> None:
"""
This function patch _init_modules() to not call dependencies
:param mocker: a Mocker object to apply patches
@ -102,9 +103,17 @@ def get_patched_freqtradebot(mocker, config) -> FreqtradeBot:
mocker.patch('freqtrade.freqtradebot.RPCManager._init', MagicMock())
mocker.patch('freqtrade.freqtradebot.RPCManager.send_msg', MagicMock())
def get_patched_freqtradebot(mocker, config) -> FreqtradeBot:
patch_freqtradebot(mocker, config)
return FreqtradeBot(config)
def get_patched_worker(mocker, config) -> Worker:
patch_freqtradebot(mocker, config)
return Worker(args=None, config=config)
def patch_coinmarketcap(mocker, value: Optional[Dict[str, float]] = None) -> None:
"""
Mocker to coinmarketcap to speed up tests

View File

@ -8,6 +8,7 @@ import pytest
from numpy import isnan
from freqtrade import TemporaryError, DependencyException
from freqtrade.worker import Worker
from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.persistence import Trade
from freqtrade.rpc import RPC, RPCException
@ -37,7 +38,9 @@ def test_rpc_trade_status(default_conf, ticker, fee, markets, mocker) -> None:
markets=PropertyMock(return_value=markets)
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -93,7 +96,9 @@ def test_rpc_status_table(default_conf, ticker, fee, markets, mocker) -> None:
markets=PropertyMock(return_value=markets)
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -129,7 +134,9 @@ def test_rpc_daily_profit(default_conf, update, ticker, fee,
markets=PropertyMock(return_value=markets)
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
stake_currency = default_conf['stake_currency']
fiat_display_currency = default_conf['fiat_display_currency']
@ -183,7 +190,9 @@ def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee,
markets=PropertyMock(return_value=markets)
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
stake_currency = default_conf['stake_currency']
fiat_display_currency = default_conf['fiat_display_currency']
@ -271,7 +280,9 @@ def test_rpc_trade_statistics_closed(mocker, default_conf, ticker, fee, markets,
markets=PropertyMock(return_value=markets)
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
stake_currency = default_conf['stake_currency']
fiat_display_currency = default_conf['fiat_display_currency']
@ -340,7 +351,9 @@ def test_rpc_balance_handle(default_conf, mocker):
get_ticker=MagicMock(side_effect=TemporaryError('Could not load ticker due to xxx'))
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
rpc._fiat_converter = CryptoToFiatConverter()
@ -368,7 +381,9 @@ def test_rpc_start(mocker, default_conf) -> None:
get_ticker=MagicMock()
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
freqtradebot.state = State.STOPPED
@ -391,7 +406,9 @@ def test_rpc_stop(mocker, default_conf) -> None:
get_ticker=MagicMock()
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
freqtradebot.state = State.RUNNING
@ -415,7 +432,9 @@ def test_rpc_stopbuy(mocker, default_conf) -> None:
get_ticker=MagicMock()
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
freqtradebot.state = State.RUNNING
@ -447,7 +466,9 @@ def test_rpc_forcesell(default_conf, ticker, fee, mocker, markets) -> None:
markets=PropertyMock(return_value=markets)
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -539,7 +560,9 @@ def test_performance_handle(default_conf, ticker, limit_buy_order, fee,
markets=PropertyMock(return_value=markets)
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -575,7 +598,9 @@ def test_rpc_count(mocker, default_conf, ticker, fee, markets) -> None:
markets=PropertyMock(return_value=markets)
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
@ -605,7 +630,9 @@ def test_rpcforcebuy(mocker, default_conf, ticker, fee, markets, limit_buy_order
buy=buy_mm
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
pair = 'ETH/BTC'
@ -630,7 +657,10 @@ def test_rpcforcebuy(mocker, default_conf, ticker, fee, markets, limit_buy_order
# Test not buying
default_conf['stake_amount'] = 0.0000001
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
pair = 'TKN/BTC'
@ -645,7 +675,9 @@ def test_rpcforcebuy_stopped(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
pair = 'ETH/BTC'
@ -658,7 +690,9 @@ def test_rpcforcebuy_disabled(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
pair = 'ETH/BTC'
@ -671,7 +705,9 @@ def test_rpc_whitelist(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
rpc = RPC(freqtradebot)
ret = rpc._rpc_whitelist()
assert ret['method'] == 'StaticPairList'
@ -687,7 +723,9 @@ 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())
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
rpc = RPC(freqtradebot)
ret = rpc._rpc_whitelist()
assert ret['method'] == 'VolumePairList'

View File

@ -13,13 +13,14 @@ from telegram import Chat, Message, Update
from telegram.error import NetworkError
from freqtrade import __version__
from freqtrade.worker import Worker
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.strategy.interface import SellType
from freqtrade.state import State
from freqtrade.tests.conftest import (get_patched_freqtradebot, log_has,
from freqtrade.tests.conftest import (get_patched_freqtradebot, get_patched_worker, log_has,
patch_exchange)
from freqtrade.tests.test_freqtradebot import patch_get_signal
from freqtrade.tests.conftest import patch_coinmarketcap
@ -98,7 +99,10 @@ def test_authorized_only(default_conf, mocker, caplog) -> None:
update.message = Message(randint(1, 100), 0, datetime.utcnow(), chat)
default_conf['telegram']['enabled'] = False
bot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
bot = worker.freqtrade
patch_get_signal(bot, (True, False))
dummy = DummyCls(bot)
dummy.dummy_handler(bot=MagicMock(), update=update)
@ -125,7 +129,10 @@ def test_authorized_only_unauthorized(default_conf, mocker, caplog) -> None:
update.message = Message(randint(1, 100), 0, datetime.utcnow(), chat)
default_conf['telegram']['enabled'] = False
bot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
bot = worker.freqtrade
patch_get_signal(bot, (True, False))
dummy = DummyCls(bot)
dummy.dummy_handler(bot=MagicMock(), update=update)
@ -153,7 +160,9 @@ def test_authorized_only_exception(default_conf, mocker, caplog) -> None:
default_conf['telegram']['enabled'] = False
bot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
bot = worker.freqtrade
patch_get_signal(bot, (True, False))
dummy = DummyCls(bot)
@ -208,7 +217,9 @@ def test_status(default_conf, update, mocker, fee, ticker, markets) -> None:
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -244,19 +255,21 @@ def test_status_handle(default_conf, update, ticker, fee, markets, mocker) -> No
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
freqtradebot.state = State.STOPPED
worker.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()
freqtradebot.state = State.RUNNING
worker.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]
@ -290,19 +303,22 @@ def test_status_table_handle(default_conf, update, ticker, fee, markets, mocker)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
default_conf['stake_amount'] = 15.0
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
freqtradebot.state = State.STOPPED
worker.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()
freqtradebot.state = State.RUNNING
worker.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]
@ -344,7 +360,9 @@ def test_daily_handle(default_conf, update, ticker, limit_buy_order, fee,
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -409,13 +427,15 @@ def test_daily_wrong_input(default_conf, update, ticker, mocker) -> None:
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
# Try invalid data
msg_mock.reset_mock()
freqtradebot.state = State.RUNNING
worker.state = State.RUNNING
update.message.text = '/daily -2'
telegram._daily(bot=MagicMock(), update=update)
assert msg_mock.call_count == 1
@ -423,7 +443,7 @@ def test_daily_wrong_input(default_conf, update, ticker, mocker) -> None:
# Try invalid data
msg_mock.reset_mock()
freqtradebot.state = State.RUNNING
worker.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]
@ -448,7 +468,9 @@ def test_profit_handle(default_conf, update, ticker, ticker_sell_up, fee,
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -593,13 +615,14 @@ def test_start_handle(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
telegram = Telegram(freqtradebot)
freqtradebot.state = State.STOPPED
assert freqtradebot.state == State.STOPPED
worker.state = State.STOPPED
assert worker.state == State.STOPPED
telegram._start(bot=MagicMock(), update=update)
assert freqtradebot.state == State.RUNNING
assert worker.state == State.RUNNING
assert msg_mock.call_count == 1
@ -611,13 +634,14 @@ def test_start_handle_already_running(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
telegram = Telegram(freqtradebot)
freqtradebot.state = State.RUNNING
assert freqtradebot.state == State.RUNNING
worker.state = State.RUNNING
assert worker.state == State.RUNNING
telegram._start(bot=MagicMock(), update=update)
assert freqtradebot.state == State.RUNNING
assert worker.state == State.RUNNING
assert msg_mock.call_count == 1
assert 'already running' in msg_mock.call_args_list[0][0][0]
@ -631,13 +655,14 @@ def test_stop_handle(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
telegram = Telegram(freqtradebot)
freqtradebot.state = State.RUNNING
assert freqtradebot.state == State.RUNNING
worker.state = State.RUNNING
assert worker.state == State.RUNNING
telegram._stop(bot=MagicMock(), update=update)
assert freqtradebot.state == State.STOPPED
assert worker.state == State.STOPPED
assert msg_mock.call_count == 1
assert 'stopping trader' in msg_mock.call_args_list[0][0][0]
@ -651,13 +676,14 @@ def test_stop_handle_already_stopped(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
telegram = Telegram(freqtradebot)
freqtradebot.state = State.STOPPED
assert freqtradebot.state == State.STOPPED
worker.state = State.STOPPED
assert worker.state == State.STOPPED
telegram._stop(bot=MagicMock(), update=update)
assert freqtradebot.state == State.STOPPED
assert worker.state == State.STOPPED
assert msg_mock.call_count == 1
assert 'already stopped' in msg_mock.call_args_list[0][0][0]
@ -691,13 +717,14 @@ def test_reload_conf_handle(default_conf, update, mocker) -> None:
_send_msg=msg_mock
)
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
worker = get_patched_worker(mocker, default_conf)
freqtradebot = worker.freqtrade
telegram = Telegram(freqtradebot)
freqtradebot.state = State.RUNNING
assert freqtradebot.state == State.RUNNING
worker.state = State.RUNNING
assert worker.state == State.RUNNING
telegram._reload_conf(bot=MagicMock(), update=update)
assert freqtradebot.state == State.RELOAD_CONF
assert worker.state == State.RELOAD_CONF
assert msg_mock.call_count == 1
assert 'reloading config' in msg_mock.call_args_list[0][0][0]
@ -717,7 +744,9 @@ def test_forcesell_handle(default_conf, update, ticker, fee,
validate_pairs=MagicMock(return_value={})
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -768,7 +797,9 @@ def test_forcesell_down_handle(default_conf, update, ticker, fee,
validate_pairs=MagicMock(return_value={})
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -822,7 +853,9 @@ def test_forcesell_all_handle(default_conf, update, ticker, fee, markets, mocker
validate_pairs=MagicMock(return_value={})
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -865,7 +898,9 @@ def test_forcesell_handle_invalid(default_conf, update, mocker) -> None:
)
patch_exchange(mocker)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -907,7 +942,9 @@ 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)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -941,7 +978,10 @@ def test_forcebuy_handle_exception(default_conf, update, markets, mocker) -> Non
markets=PropertyMock(markets),
validate_pairs=MagicMock(return_value={})
)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -970,7 +1010,10 @@ def test_performance_handle(default_conf, update, ticker, fee,
validate_pairs=MagicMock(return_value={})
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)
@ -1009,7 +1052,10 @@ def test_count_handle(default_conf, update, ticker, fee, markets, mocker) -> Non
markets=PropertyMock(markets)
)
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
freqtradebot = FreqtradeBot(default_conf)
worker = Worker(args=None, config=default_conf)
freqtradebot = worker.freqtrade
patch_get_signal(freqtradebot, (True, False))
telegram = Telegram(freqtradebot)

View File

@ -13,6 +13,7 @@ import requests
from freqtrade import (DependencyException, OperationalException,
TemporaryError, constants)
from freqtrade.worker import Worker
from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.persistence import Trade
from freqtrade.rpc import RPCMessageType
@ -22,9 +23,9 @@ from freqtrade.tests.conftest import log_has, log_has_re, patch_exchange, patch_
# Functions for recurrent object patching
def get_patched_freqtradebot(mocker, config) -> FreqtradeBot:
def patch_freqtradebot(mocker, config) -> None:
"""
This function patch _init_modules() to not call dependencies
This function patches _init_modules() to not call dependencies
:param mocker: a Mocker object to apply patches
:param config: Config to pass to the bot
:return: None
@ -33,9 +34,29 @@ def get_patched_freqtradebot(mocker, config) -> FreqtradeBot:
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
patch_exchange(mocker)
def get_patched_freqtradebot(mocker, config) -> FreqtradeBot:
"""
This function patches _init_modules() to not call dependencies
:param mocker: a Mocker object to apply patches
:param config: Config to pass to the bot
:return: FreqtradeBot
"""
patch_freqtradebot(mocker, config)
return FreqtradeBot(config)
def get_patched_worker(mocker, config) -> Worker:
"""
This function patches _init_modules() to not call dependencies
:param mocker: a Mocker object to apply patches
:param config: Config to pass to the bot
:return: FreqtradeBot
"""
patch_freqtradebot(mocker, config)
return Worker(args=None, config=config)
def patch_get_signal(freqtrade: FreqtradeBot, value=(True, False)) -> None:
"""
:param mocker: mocker to patch IStrategy class
@ -61,12 +82,12 @@ def patch_RPCManager(mocker) -> MagicMock:
def test_freqtradebot(mocker, default_conf, markets) -> None:
mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets))
freqtrade = get_patched_freqtradebot(mocker, default_conf)
assert freqtrade.state is State.RUNNING
worker = get_patched_worker(mocker, default_conf)
assert worker.state is State.RUNNING
default_conf.pop('initial_state')
freqtrade = FreqtradeBot(default_conf)
assert freqtrade.state is State.STOPPED
worker = Worker(args=None, config=default_conf)
assert worker.state is State.STOPPED
def test_cleanup(mocker, default_conf, caplog) -> None:
@ -80,11 +101,11 @@ def test_cleanup(mocker, default_conf, caplog) -> None:
def test_worker_running(mocker, default_conf, caplog) -> None:
mock_throttle = MagicMock()
mocker.patch('freqtrade.freqtradebot.FreqtradeBot._throttle', mock_throttle)
mocker.patch('freqtrade.worker.Worker._throttle', mock_throttle)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
worker = get_patched_worker(mocker, default_conf)
state = freqtrade.worker(old_state=None)
state = worker._worker(old_state=None)
assert state is State.RUNNING
assert log_has('Changing state to: RUNNING', caplog.record_tuples)
assert mock_throttle.call_count == 1
@ -92,12 +113,12 @@ def test_worker_running(mocker, default_conf, caplog) -> None:
def test_worker_stopped(mocker, default_conf, caplog) -> None:
mock_throttle = MagicMock()
mocker.patch('freqtrade.freqtradebot.FreqtradeBot._throttle', mock_throttle)
mocker.patch('freqtrade.worker.Worker._throttle', mock_throttle)
mock_sleep = mocker.patch('time.sleep', return_value=None)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
freqtrade.state = State.STOPPED
state = freqtrade.worker(old_state=State.RUNNING)
worker = get_patched_worker(mocker, default_conf)
worker.state = State.STOPPED
state = worker._worker(old_state=State.RUNNING)
assert state is State.STOPPED
assert log_has('Changing state to: STOPPED', caplog.record_tuples)
assert mock_throttle.call_count == 0
@ -109,17 +130,17 @@ def test_throttle(mocker, default_conf, caplog) -> None:
return 42
caplog.set_level(logging.DEBUG)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
worker = get_patched_worker(mocker, default_conf)
start = time.time()
result = freqtrade._throttle(throttled_func, min_secs=0.1)
result = worker._throttle(throttled_func, min_secs=0.1)
end = time.time()
assert result == 42
assert end - start > 0.1
assert log_has('Throttling throttled_func for 0.10 seconds', caplog.record_tuples)
result = freqtrade._throttle(throttled_func, min_secs=-1)
result = worker._throttle(throttled_func, min_secs=-1)
assert result == 42
@ -127,12 +148,12 @@ def test_throttle_with_assets(mocker, default_conf) -> None:
def throttled_func(nb_assets=-1):
return nb_assets
freqtrade = get_patched_freqtradebot(mocker, default_conf)
worker = get_patched_worker(mocker, default_conf)
result = freqtrade._throttle(throttled_func, min_secs=0.1, nb_assets=666)
result = worker._throttle(throttled_func, min_secs=0.1, nb_assets=666)
assert result == 666
result = freqtrade._throttle(throttled_func, min_secs=0.1)
result = worker._throttle(throttled_func, min_secs=0.1)
assert result == -1
@ -218,7 +239,7 @@ def test_edge_called_in_process(mocker, edge_conf) -> None:
freqtrade = FreqtradeBot(edge_conf)
freqtrade.pairlists._validate_whitelist = _refresh_whitelist
patch_get_signal(freqtrade)
freqtrade._process()
freqtrade.process()
assert freqtrade.active_pair_whitelist == ['NEO/BTC', 'LTC/BTC']
@ -652,7 +673,7 @@ def test_process_trade_creation(default_conf, ticker, limit_buy_order,
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
assert not trades
result = freqtrade._process()
result = freqtrade.process()
assert result is True
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
@ -683,10 +704,10 @@ def test_process_exchange_failures(default_conf, ticker, markets, mocker) -> Non
)
sleep_mock = mocker.patch('time.sleep', side_effect=lambda _: None)
freqtrade = FreqtradeBot(default_conf)
patch_get_signal(freqtrade)
worker = get_patched_worker(mocker, default_conf)
patch_get_signal(worker.freqtrade)
result = freqtrade._process()
result = worker.freqtrade.process()
assert result is False
assert sleep_mock.has_calls()
@ -700,14 +721,14 @@ def test_process_operational_exception(default_conf, ticker, markets, mocker) ->
markets=PropertyMock(return_value=markets),
buy=MagicMock(side_effect=OperationalException)
)
freqtrade = FreqtradeBot(default_conf)
patch_get_signal(freqtrade)
worker = get_patched_worker(mocker, default_conf)
patch_get_signal(worker.freqtrade)
assert freqtrade.state == State.RUNNING
assert worker.state == State.RUNNING
result = freqtrade._process()
result = worker.freqtrade.process()
assert result is False
assert freqtrade.state == State.STOPPED
assert worker.state == State.STOPPED
assert 'OperationalException' in msg_mock.call_args_list[-1][0][0]['status']
@ -728,18 +749,18 @@ def test_process_trade_handling(
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
assert not trades
result = freqtrade._process()
result = freqtrade.process()
assert result is True
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
assert len(trades) == 1
result = freqtrade._process()
result = freqtrade.process()
assert result is False
def test_process_trade_no_whitelist_pair(
default_conf, ticker, limit_buy_order, markets, fee, mocker) -> None:
""" Test _process with trade not in pair list """
""" Test process with trade not in pair list """
patch_RPCManager(mocker)
patch_exchange(mocker)
mocker.patch.multiple(
@ -776,7 +797,7 @@ def test_process_trade_no_whitelist_pair(
))
assert pair not in freqtrade.active_pair_whitelist
result = freqtrade._process()
result = freqtrade.process()
assert pair in freqtrade.active_pair_whitelist
# Make sure each pair is only in the list once
assert len(freqtrade.active_pair_whitelist) == len(set(freqtrade.active_pair_whitelist))
@ -806,7 +827,7 @@ def test_process_informative_pairs_added(default_conf, ticker, markets, mocker)
freqtrade.strategy.informative_pairs = inf_pairs
# patch_get_signal(freqtrade)
freqtrade._process()
freqtrade.process()
assert inf_pairs.call_count == 1
assert refresh_mock.call_count == 1
assert ("BTC/ETH", "1m") in refresh_mock.call_args[0][0]
@ -2992,5 +3013,5 @@ def test_startup_messages(default_conf, mocker):
'config': {'number_assets': 20}
}
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True))
freqtrade = get_patched_freqtradebot(mocker, default_conf)
assert freqtrade.state is State.RUNNING
worker = get_patched_worker(mocker, default_conf)
assert worker.state is State.RUNNING

View File

@ -43,17 +43,14 @@ def test_main_start_hyperopt(mocker) -> None:
def test_main_fatal_exception(mocker, default_conf, caplog) -> None:
patch_exchange(mocker)
mocker.patch.multiple(
'freqtrade.freqtradebot.FreqtradeBot',
_init_modules=MagicMock(),
worker=MagicMock(side_effect=Exception),
cleanup=MagicMock(),
)
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=Exception))
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
args = ['-c', 'config.json.example']
@ -66,17 +63,14 @@ def test_main_fatal_exception(mocker, default_conf, caplog) -> None:
def test_main_keyboard_interrupt(mocker, default_conf, caplog) -> None:
patch_exchange(mocker)
mocker.patch.multiple(
'freqtrade.freqtradebot.FreqtradeBot',
_init_modules=MagicMock(),
worker=MagicMock(side_effect=KeyboardInterrupt),
cleanup=MagicMock(),
)
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=KeyboardInterrupt))
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
args = ['-c', 'config.json.example']
@ -89,17 +83,14 @@ def test_main_keyboard_interrupt(mocker, default_conf, caplog) -> None:
def test_main_operational_exception(mocker, default_conf, caplog) -> None:
patch_exchange(mocker)
mocker.patch.multiple(
'freqtrade.freqtradebot.FreqtradeBot',
_init_modules=MagicMock(),
worker=MagicMock(side_effect=OperationalException('Oh snap!')),
cleanup=MagicMock(),
)
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=OperationalException('Oh snap!')))
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
args = ['-c', 'config.json.example']
@ -112,17 +103,14 @@ def test_main_operational_exception(mocker, default_conf, caplog) -> None:
def test_main_reload_conf(mocker, default_conf, caplog) -> None:
patch_exchange(mocker)
mocker.patch.multiple(
'freqtrade.freqtradebot.FreqtradeBot',
_init_modules=MagicMock(),
worker=MagicMock(return_value=State.RELOAD_CONF),
cleanup=MagicMock(),
)
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
mocker.patch('freqtrade.worker.Worker._worker', MagicMock(return_value=State.RELOAD_CONF))
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
# Raise exception as side effect to avoid endless loop
reconfigure_mock = mocker.patch(
@ -138,17 +126,14 @@ def test_main_reload_conf(mocker, default_conf, caplog) -> None:
def test_reconfigure(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch.multiple(
'freqtrade.freqtradebot.FreqtradeBot',
_init_modules=MagicMock(),
worker=MagicMock(side_effect=OperationalException('Oh snap!')),
cleanup=MagicMock(),
)
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=OperationalException('Oh snap!')))
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
freqtrade = FreqtradeBot(default_conf)