small refactor splitting the _process()

This commit is contained in:
kryofly
2018-01-10 09:09:33 +01:00
parent ad2328bbd8
commit f8cc08e2a1
4 changed files with 88 additions and 38 deletions

View File

@@ -1,6 +1,7 @@
# pragma pylint: disable=missing-docstring
from datetime import datetime
from unittest.mock import MagicMock
from functools import reduce
import pytest
import arrow
@@ -10,6 +11,14 @@ from telegram import Message, Chat, Update
from freqtrade.misc import CONF_SCHEMA
def log_has(line, logs):
# caplog mocker returns log as a tuple: ('freqtrade.analyze', logging.WARNING, 'foobar')
# and we want to match line against foobar in the tuple
return reduce(lambda a, b: a or b,
filter(lambda x: x[2] == line, logs),
False)
@pytest.fixture(scope="module")
def default_conf():
""" Returns validated configuration suitable for most tests """

View File

@@ -1,7 +1,7 @@
# pragma pylint: disable=missing-docstring,W0621
import json
from functools import reduce
from unittest.mock import MagicMock
import freqtrade.tests.conftest as tt # test tools
import arrow
import datetime
@@ -12,14 +12,6 @@ from freqtrade.analyze import parse_ticker_dataframe, populate_buy_trend, popula
get_signal, SignalType, populate_sell_trend
def log_has(line, logs):
# caplog mocker returns log as a tuple: ('freqtrade.analyze', logging.WARNING, 'foobar')
# and we want to match line against foobar in the tuple
return reduce(lambda a, b: a or b,
filter(lambda x: x[2] == line, logs),
False)
@pytest.fixture
def result():
with open('freqtrade/tests/testdata/BTC_ETH-1.json') as data_file:
@@ -78,8 +70,8 @@ def test_returns_latest_sell_signal(mocker):
def test_get_signal_empty(mocker, caplog):
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=None)
assert not get_signal('foo', SignalType.BUY)
assert log_has('Empty ticker history for pair foo',
caplog.record_tuples)
assert tt.log_has('Empty ticker history for pair foo',
caplog.record_tuples)
def test_get_signal_execption_valueerror(mocker, caplog):
@@ -87,8 +79,8 @@ def test_get_signal_execption_valueerror(mocker, caplog):
mocker.patch('freqtrade.analyze.analyze_ticker',
side_effect=ValueError('xyz'))
assert not get_signal('foo', SignalType.BUY)
assert log_has('Unable to analyze ticker for pair foo: xyz',
caplog.record_tuples)
assert tt.log_has('Unable to analyze ticker for pair foo: xyz',
caplog.record_tuples)
# This error should never occur becase analyze_ticker is run first,
@@ -97,8 +89,8 @@ def test_get_signal_empty_dataframe(mocker, caplog):
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1)
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=DataFrame([]))
assert not get_signal('xyz', SignalType.BUY)
assert log_has('Empty dataframe for pair xyz',
caplog.record_tuples)
assert tt.log_has('Empty dataframe for pair xyz',
caplog.record_tuples)
def test_get_signal_old_dataframe(mocker, caplog):
@@ -108,8 +100,8 @@ def test_get_signal_old_dataframe(mocker, caplog):
ticks = DataFrame([{'buy': 1, 'date': oldtime}])
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=DataFrame(ticks))
assert not get_signal('xyz', SignalType.BUY)
assert log_has('Too old dataframe for pair xyz',
caplog.record_tuples)
assert tt.log_has('Too old dataframe for pair xyz',
caplog.record_tuples)
def test_get_signal_handles_exceptions(mocker):

View File

@@ -1,6 +1,7 @@
# pragma pylint: disable=missing-docstring,C0103
import copy
from unittest.mock import MagicMock
import freqtrade.tests.conftest as tt # test tools
import pytest
import requests
@@ -50,6 +51,34 @@ def test_main_start_hyperopt(mocker):
assert call_args.func is not None
def test_process_maybe_execute_buy(default_conf, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.create_trade', return_value=True)
assert main.process_maybe_execute_buy(default_conf)
mocker.patch('freqtrade.main.create_trade', return_value=False)
assert not main.process_maybe_execute_buy(default_conf)
def test_process_maybe_execute_sell(default_conf, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.handle_trade', return_value=True)
mocker.patch('freqtrade.exchange.get_order', return_value=1)
trade = MagicMock()
trade.open_order_id = '123'
assert not main.process_maybe_execute_sell(trade)
trade.is_open = True
trade.open_order_id = None
# Assert we call handle_trade() if trade is feasible for execution
assert main.process_maybe_execute_sell(trade)
def test_process_maybe_execute_buy_exception(default_conf, mocker, caplog):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch('freqtrade.main.create_trade', MagicMock(side_effect=DependencyException))
main.process_maybe_execute_buy(default_conf)
tt.log_has('Unable to create trade:', caplog.record_tuples)
def test_process_trade_creation(default_conf, ticker, limit_buy_order, health, mocker):
mocker.patch.dict('freqtrade.main._CONF', default_conf)
mocker.patch.multiple('freqtrade.rpc', init=MagicMock(), send_msg=MagicMock())