Fix tests analyze

This commit is contained in:
xmatthias 2018-06-17 20:48:12 +02:00
parent e8ab76f55b
commit 082b6077e9

View File

@ -14,7 +14,7 @@ from pandas import DataFrame
from freqtrade.analyze import Analyze, SignalType from freqtrade.analyze import Analyze, SignalType
from freqtrade.optimize.__init__ import load_tickerdata_file from freqtrade.optimize.__init__ import load_tickerdata_file
from freqtrade.arguments import TimeRange from freqtrade.arguments import TimeRange
from freqtrade.tests.conftest import log_has from freqtrade.tests.conftest import log_has, get_patched_exchange
# Avoid to reinit the same object again and again # Avoid to reinit the same object again and again
_ANALYZE = Analyze({'strategy': 'DefaultStrategy'}) _ANALYZE = Analyze({'strategy': 'DefaultStrategy'})
@ -66,16 +66,16 @@ def test_populates_sell_trend(result):
assert 'sell' in dataframe.columns assert 'sell' in dataframe.columns
def test_returns_latest_buy_signal(mocker): def test_returns_latest_buy_signal(mocker, default_conf):
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock()) mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=MagicMock())
exchange = get_patched_exchange(mocker, default_conf)
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.analyze.Analyze', 'freqtrade.analyze.Analyze',
analyze_ticker=MagicMock( analyze_ticker=MagicMock(
return_value=DataFrame([{'buy': 1, 'sell': 0, 'date': arrow.utcnow()}]) return_value=DataFrame([{'buy': 1, 'sell': 0, 'date': arrow.utcnow()}])
) )
) )
assert _ANALYZE.get_signal('ETH/BTC', '5m') == (True, False) assert _ANALYZE.get_signal(exchange, 'ETH/BTC', '5m') == (True, False)
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.analyze.Analyze', 'freqtrade.analyze.Analyze',
@ -83,11 +83,12 @@ def test_returns_latest_buy_signal(mocker):
return_value=DataFrame([{'buy': 0, 'sell': 1, 'date': arrow.utcnow()}]) return_value=DataFrame([{'buy': 0, 'sell': 1, 'date': arrow.utcnow()}])
) )
) )
assert _ANALYZE.get_signal('ETH/BTC', '5m') == (False, True) assert _ANALYZE.get_signal(exchange, 'ETH/BTC', '5m') == (False, True)
def test_returns_latest_sell_signal(mocker): def test_returns_latest_sell_signal(mocker, default_conf):
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock()) mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=MagicMock())
exchange = get_patched_exchange(mocker, default_conf)
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.analyze.Analyze', 'freqtrade.analyze.Analyze',
analyze_ticker=MagicMock( analyze_ticker=MagicMock(
@ -95,7 +96,7 @@ def test_returns_latest_sell_signal(mocker):
) )
) )
assert _ANALYZE.get_signal('ETH/BTC', '5m') == (False, True) assert _ANALYZE.get_signal(exchange, 'ETH/BTC', '5m') == (False, True)
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.analyze.Analyze', 'freqtrade.analyze.Analyze',
@ -103,45 +104,49 @@ def test_returns_latest_sell_signal(mocker):
return_value=DataFrame([{'sell': 0, 'buy': 1, 'date': arrow.utcnow()}]) return_value=DataFrame([{'sell': 0, 'buy': 1, 'date': arrow.utcnow()}])
) )
) )
assert _ANALYZE.get_signal('ETH/BTC', '5m') == (True, False) assert _ANALYZE.get_signal(exchange, 'ETH/BTC', '5m') == (True, False)
def test_get_signal_empty(default_conf, mocker, caplog): def test_get_signal_empty(default_conf, mocker, caplog):
caplog.set_level(logging.INFO) caplog.set_level(logging.INFO)
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=None) mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=None)
assert (False, False) == _ANALYZE.get_signal('foo', default_conf['ticker_interval']) exchange = get_patched_exchange(mocker, default_conf)
assert (False, False) == _ANALYZE.get_signal(exchange, 'foo', default_conf['ticker_interval'])
assert log_has('Empty ticker history for pair foo', caplog.record_tuples) assert log_has('Empty ticker history for pair foo', caplog.record_tuples)
def test_get_signal_exception_valueerror(default_conf, mocker, caplog): def test_get_signal_exception_valueerror(default_conf, mocker, caplog):
caplog.set_level(logging.INFO) caplog.set_level(logging.INFO)
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1) mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=1)
exchange = get_patched_exchange(mocker, default_conf)
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.analyze.Analyze', 'freqtrade.analyze.Analyze',
analyze_ticker=MagicMock( analyze_ticker=MagicMock(
side_effect=ValueError('xyz') side_effect=ValueError('xyz')
) )
) )
assert (False, False) == _ANALYZE.get_signal('foo', default_conf['ticker_interval']) assert (False, False) == _ANALYZE.get_signal(exchange, 'foo', default_conf['ticker_interval'])
assert log_has('Unable to analyze ticker for pair foo: xyz', caplog.record_tuples) assert log_has('Unable to analyze ticker for pair foo: xyz', caplog.record_tuples)
def test_get_signal_empty_dataframe(default_conf, mocker, caplog): def test_get_signal_empty_dataframe(default_conf, mocker, caplog):
caplog.set_level(logging.INFO) caplog.set_level(logging.INFO)
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1) mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=1)
exchange = get_patched_exchange(mocker, default_conf)
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.analyze.Analyze', 'freqtrade.analyze.Analyze',
analyze_ticker=MagicMock( analyze_ticker=MagicMock(
return_value=DataFrame([]) return_value=DataFrame([])
) )
) )
assert (False, False) == _ANALYZE.get_signal('xyz', default_conf['ticker_interval']) assert (False, False) == _ANALYZE.get_signal(exchange, 'xyz', default_conf['ticker_interval'])
assert log_has('Empty dataframe for pair xyz', caplog.record_tuples) assert log_has('Empty dataframe for pair xyz', caplog.record_tuples)
def test_get_signal_old_dataframe(default_conf, mocker, caplog): def test_get_signal_old_dataframe(default_conf, mocker, caplog):
caplog.set_level(logging.INFO) caplog.set_level(logging.INFO)
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1) mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=1)
exchange = get_patched_exchange(mocker, default_conf)
# FIX: The get_signal function has hardcoded 10, which we must inturn hardcode # FIX: The get_signal function has hardcoded 10, which we must inturn hardcode
oldtime = arrow.utcnow() - datetime.timedelta(minutes=11) oldtime = arrow.utcnow() - datetime.timedelta(minutes=11)
ticks = DataFrame([{'buy': 1, 'date': oldtime}]) ticks = DataFrame([{'buy': 1, 'date': oldtime}])
@ -151,15 +156,16 @@ def test_get_signal_old_dataframe(default_conf, mocker, caplog):
return_value=DataFrame(ticks) return_value=DataFrame(ticks)
) )
) )
assert (False, False) == _ANALYZE.get_signal('xyz', default_conf['ticker_interval']) assert (False, False) == _ANALYZE.get_signal(exchange, 'xyz', default_conf['ticker_interval'])
assert log_has( assert log_has(
'Outdated history for pair xyz. Last tick is 11 minutes old', 'Outdated history for pair xyz. Last tick is 11 minutes old',
caplog.record_tuples caplog.record_tuples
) )
def test_get_signal_handles_exceptions(mocker): def test_get_signal_handles_exceptions(mocker, default_conf):
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=MagicMock()) mocker.patch('freqtrade.exchange.Exchange.get_ticker_history', return_value=MagicMock())
exchange = get_patched_exchange(mocker, default_conf)
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.analyze.Analyze', 'freqtrade.analyze.Analyze',
analyze_ticker=MagicMock( analyze_ticker=MagicMock(
@ -167,7 +173,7 @@ def test_get_signal_handles_exceptions(mocker):
) )
) )
assert _ANALYZE.get_signal('ETH/BTC', '5m') == (False, False) assert _ANALYZE.get_signal(exchange, 'ETH/BTC', '5m') == (False, False)
def test_parse_ticker_dataframe(ticker_history): def test_parse_ticker_dataframe(ticker_history):