use mocker for mocking to get rid of deep nesting

This commit is contained in:
Janne Sinivirta 2017-10-01 15:25:10 +03:00
parent add6c875d6
commit ff145b6306
5 changed files with 228 additions and 232 deletions

View File

@ -1,6 +1,4 @@
# pragma pylint: disable=missing-docstring
from unittest.mock import patch
import pytest
import arrow
from pandas import DataFrame
@ -39,10 +37,11 @@ def test_populates_buy_trend(result):
assert 'buy' in dataframe.columns
assert 'buy_price' in dataframe.columns
def test_returns_latest_buy_signal():
def test_returns_latest_buy_signal(mocker):
buydf = DataFrame([{'buy': 1, 'date': arrow.utcnow()}])
with patch('freqtrade.analyze.analyze_ticker', return_value=buydf):
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=buydf)
assert get_buy_signal('BTC-ETH') == True
buydf = DataFrame([{'buy': 0, 'date': arrow.utcnow()}])
with patch('freqtrade.analyze.analyze_ticker', return_value=buydf):
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=buydf)
assert get_buy_signal('BTC-ETH') == False

View File

@ -2,7 +2,6 @@
import json
import logging
import os
from unittest.mock import patch
import pytest
import arrow
@ -41,15 +40,15 @@ def conf():
@pytest.mark.skipif(not os.environ.get('BACKTEST', False), reason="BACKTEST not set")
def test_backtest(conf, pairs):
def test_backtest(conf, pairs, mocker):
trades = []
with patch.dict('freqtrade.main._CONF', conf):
mocker.patch.dict('freqtrade.main._CONF', conf)
for pair in pairs:
with open('tests/testdata/'+pair+'.json') as data_file:
data = json.load(data_file)
with patch('freqtrade.analyze.get_ticker', return_value=data):
with patch('arrow.utcnow', return_value=arrow.get('2017-08-20T14:50:00')):
mocker.patch('freqtrade.analyze.get_ticker', return_value=data)
mocker.patch('arrow.utcnow', return_value=arrow.get('2017-08-20T14:50:00'))
ticker = analyze_ticker(pair)
# for each buy point
for index, row in ticker[ticker.buy == 1].iterrows():

View File

@ -1,5 +1,5 @@
import copy
from unittest.mock import patch, MagicMock, call
from unittest.mock import MagicMock, call
import pytest
from jsonschema import validate
@ -46,17 +46,17 @@ def conf():
validate(configuration, CONF_SCHEMA)
return configuration
def test_create_trade(conf):
with patch.dict('freqtrade.main._CONF', conf):
with patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True) as buy_signal:
with patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()):
with patch.multiple('freqtrade.main.exchange',
def test_create_trade(conf, mocker):
mocker.patch.dict('freqtrade.main._CONF', conf)
buy_signal = mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True)
mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock())
mocker.patch.multiple('freqtrade.main.exchange',
get_ticker=MagicMock(return_value={
'bid': 0.07256061,
'ask': 0.072661,
'last': 0.07256061
}),
buy=MagicMock(return_value='mocked_order_id')):
buy=MagicMock(return_value='mocked_order_id'))
# Save state of current whitelist
whitelist = copy.deepcopy(conf['bittrex']['pair_whitelist'])
@ -79,16 +79,16 @@ def test_create_trade(conf):
[call('BTC_ETH'), call('BTC_TKN'), call('BTC_TRST'), call('BTC_SWT')]
)
def test_handle_trade(conf):
with patch.dict('freqtrade.main._CONF', conf):
with patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock()):
with patch.multiple('freqtrade.main.exchange',
def test_handle_trade(conf, mocker):
mocker.patch.dict('freqtrade.main._CONF', conf)
mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock())
mocker.patch.multiple('freqtrade.main.exchange',
get_ticker=MagicMock(return_value={
'bid': 0.17256061,
'ask': 0.172661,
'last': 0.17256061
}),
buy=MagicMock(return_value='mocked_order_id')):
buy=MagicMock(return_value='mocked_order_id'))
trade = Trade.query.filter(Trade.is_open.is_(True)).first()
assert trade
handle_trade(trade)
@ -97,8 +97,8 @@ def test_handle_trade(conf):
assert trade.close_date is not None
assert trade.open_order_id == 'dry_run'
def test_close_trade(conf):
with patch.dict('freqtrade.main._CONF', conf):
def test_close_trade(conf, mocker):
mocker.patch.dict('freqtrade.main._CONF', conf)
trade = Trade.query.filter(Trade.is_open.is_(True)).first()
assert trade
@ -109,14 +109,14 @@ def test_close_trade(conf):
assert closed
assert trade.is_open == False
def test_balance_fully_ask_side():
with patch.dict('freqtrade.main._CONF', {'bid_strategy': {'ask_last_balance': 0.0}}):
def test_balance_fully_ask_side(mocker):
mocker.patch.dict('freqtrade.main._CONF', {'bid_strategy': {'ask_last_balance': 0.0}})
assert get_target_bid({'ask': 20, 'last': 10}) == 20
def test_balance_fully_last_side():
with patch.dict('freqtrade.main._CONF', {'bid_strategy': {'ask_last_balance': 1.0}}):
def test_balance_fully_last_side(mocker):
mocker.patch.dict('freqtrade.main._CONF', {'bid_strategy': {'ask_last_balance': 1.0}})
assert get_target_bid({'ask': 20, 'last': 10}) == 10
def test_balance_when_last_bigger_than_ask():
with patch.dict('freqtrade.main._CONF', {'bid_strategy': {'ask_last_balance': 1.0}}):
def test_balance_when_last_bigger_than_ask(mocker):
mocker.patch.dict('freqtrade.main._CONF', {'bid_strategy': {'ask_last_balance': 1.0}})
assert get_target_bid({'ask': 5, 'last': 10}) == 5

View File

@ -1,11 +1,9 @@
from unittest.mock import patch
from freqtrade.exchange import Exchange
from freqtrade.persistence import Trade
def test_exec_sell_order():
with patch('freqtrade.main.exchange.sell', side_effect='mocked_order_id') as api_mock:
def test_exec_sell_order(mocker):
api_mock = mocker.patch('freqtrade.main.exchange.sell', side_effect='mocked_order_id')
trade = Trade(
pair='BTC_ETH',
stake_amount=1.00,

View File

@ -1,5 +1,5 @@
from datetime import datetime
from unittest.mock import patch, MagicMock
from unittest.mock import MagicMock
import pytest
from jsonschema import validate
@ -56,18 +56,18 @@ class MagicBot(MagicMock, Bot):
pass
def test_status_handle(conf, update):
with patch.dict('freqtrade.main._CONF', conf):
with patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True):
def test_status_handle(conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', conf)
mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True)
msg_mock = MagicMock()
with patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock):
with patch.multiple('freqtrade.main.exchange',
mocker.patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
get_ticker=MagicMock(return_value={
'bid': 0.07256061,
'ask': 0.072661,
'last': 0.07256061
}),
buy=MagicMock(return_value='mocked_order_id')):
buy=MagicMock(return_value='mocked_order_id'))
init(conf, 'sqlite://')
# Create some test data
@ -80,18 +80,18 @@ def test_status_handle(conf, update):
assert msg_mock.call_count == 2
assert '[BTC_ETH]' in msg_mock.call_args_list[-1][0][0]
def test_profit_handle(conf, update):
with patch.dict('freqtrade.main._CONF', conf):
with patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True):
def test_profit_handle(conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', conf)
mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True)
msg_mock = MagicMock()
with patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock):
with patch.multiple('freqtrade.main.exchange',
mocker.patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
get_ticker=MagicMock(return_value={
'bid': 0.07256061,
'ask': 0.072661,
'last': 0.07256061
}),
buy=MagicMock(return_value='mocked_order_id')):
buy=MagicMock(return_value='mocked_order_id'))
init(conf, 'sqlite://')
# Create some test data
@ -109,18 +109,18 @@ def test_profit_handle(conf, update):
assert msg_mock.call_count == 2
assert '(100.00%)' in msg_mock.call_args_list[-1][0][0]
def test_forcesell_handle(conf, update):
with patch.dict('freqtrade.main._CONF', conf):
with patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True):
def test_forcesell_handle(conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', conf)
mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True)
msg_mock = MagicMock()
with patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock):
with patch.multiple('freqtrade.main.exchange',
mocker.patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
get_ticker=MagicMock(return_value={
'bid': 0.07256061,
'ask': 0.072661,
'last': 0.07256061
}),
buy=MagicMock(return_value='mocked_order_id')):
buy=MagicMock(return_value='mocked_order_id'))
init(conf, 'sqlite://')
# Create some test data
@ -136,18 +136,18 @@ def test_forcesell_handle(conf, update):
assert 'Selling [BTC/ETH]' in msg_mock.call_args_list[-1][0][0]
assert '0.072561' in msg_mock.call_args_list[-1][0][0]
def test_performance_handle(conf, update):
with patch.dict('freqtrade.main._CONF', conf):
with patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True):
def test_performance_handle(conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', conf)
mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True)
msg_mock = MagicMock()
with patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock):
with patch.multiple('freqtrade.main.exchange',
mocker.patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock)
mocker.patch.multiple('freqtrade.main.exchange',
get_ticker=MagicMock(return_value={
'bid': 0.07256061,
'ask': 0.072661,
'last': 0.07256061
}),
buy=MagicMock(return_value='mocked_order_id')):
buy=MagicMock(return_value='mocked_order_id'))
init(conf, 'sqlite://')
# Create some test data
@ -166,10 +166,10 @@ def test_performance_handle(conf, update):
assert 'Performance' in msg_mock.call_args_list[-1][0][0]
assert 'BTC_ETH 100.00%' in msg_mock.call_args_list[-1][0][0]
def test_start_handle(conf, update):
with patch.dict('freqtrade.main._CONF', conf):
def test_start_handle(conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', conf)
msg_mock = MagicMock()
with patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock):
mocker.patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock)
init(conf, 'sqlite://')
update_state(State.STOPPED)
@ -178,10 +178,10 @@ def test_start_handle(conf, update):
assert get_state() == State.RUNNING
assert msg_mock.call_count == 0
def test_stop_handle(conf, update):
with patch.dict('freqtrade.main._CONF', conf):
def test_stop_handle(conf, update, mocker):
mocker.patch.dict('freqtrade.main._CONF', conf)
msg_mock = MagicMock()
with patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock):
mocker.patch.multiple('freqtrade.main.telegram', _CONF=conf, init=MagicMock(), send_msg=msg_mock)
init(conf, 'sqlite://')
update_state(State.RUNNING)