use log_has helper in tests

This commit is contained in:
Janne Sinivirta 2018-02-24 21:18:53 +02:00
parent 76c5cdc6e3
commit bf54692efb
5 changed files with 24 additions and 45 deletions

View File

@ -10,6 +10,7 @@ from freqtrade import OperationalException
from freqtrade.exchange import init, validate_pairs, buy, sell, get_balance, get_balances, \
get_ticker, get_ticker_history, cancel_order, get_name, get_fee
import freqtrade.exchange as exchange
from freqtrade.tests.conftest import log_has
API_INIT = False
@ -26,10 +27,7 @@ def maybe_init_api(conf, mocker):
def test_init(default_conf, mocker, caplog):
caplog.set_level(logging.INFO)
maybe_init_api(default_conf, mocker)
assert ('freqtrade.exchange',
logging.INFO,
'Instance is running with dry_run enabled'
) in caplog.record_tuples
assert log_has('Instance is running with dry_run enabled', caplog.record_tuples)
def test_init_exception(default_conf):
@ -79,10 +77,8 @@ def test_validate_pairs_exception(default_conf, mocker, caplog):
# with pytest.raises(RequestException, match=r'Unable to validate pairs'):
validate_pairs(default_conf['exchange']['pair_whitelist'])
assert ('freqtrade.exchange',
logging.WARNING,
'Unable to validate pairs (assuming they are correct). Reason: '
) in caplog.record_tuples
assert log_has('Unable to validate pairs (assuming they are correct). Reason: ',
caplog.record_tuples)
def test_buy_dry_run(default_conf, mocker):

View File

@ -10,6 +10,7 @@ from freqtrade.exchange import Bittrex
from freqtrade.optimize import preprocess
from freqtrade.optimize.backtesting import backtest, generate_text_table, get_timeframe
import freqtrade.optimize.backtesting as backtesting
from freqtrade.tests.conftest import log_has
def trim_dictlist(dict_list, num):
@ -322,9 +323,7 @@ def test_backtest_start(default_conf, mocker, caplog):
'Measuring data from 2017-11-14T21:17:00+00:00 '
'up to 2017-11-14T22:59:00+00:00 (0 days)..']
for line in exists:
assert ('freqtrade.optimize.backtesting',
logging.INFO,
line) in caplog.record_tuples
assert log_has(line, caplog.record_tuples)
def test_backtest_start_live(default_strategy, default_conf, mocker, caplog):
@ -348,6 +347,4 @@ def test_backtest_start_live(default_strategy, default_conf, mocker, caplog):
'Measuring data from 2017-11-14T19:32:00+00:00 '
'up to 2017-11-14T22:59:00+00:00 (0 days)..']
for line in exists:
assert ('freqtrade.optimize.backtesting',
logging.INFO,
line) in caplog.record_tuples
assert log_has(line, caplog.record_tuples)

View File

@ -9,6 +9,7 @@ from freqtrade import exchange, optimize
from freqtrade.exchange import Bittrex
from freqtrade.optimize.__init__ import make_testdata_path, download_pairs,\
download_backtesting_testdata, load_tickerdata_file, trim_tickerlist, file_dump_json
from freqtrade.tests.conftest import log_has
# Change this if modifying BTC_UNITEST testdatafile
_BTC_UNITTEST_LENGTH = 13681
@ -56,9 +57,7 @@ def test_load_data_30min_ticker(default_conf, ticker_history, mocker, caplog):
_backup_file(file, copy_file=True)
optimize.load_data(None, pairs=['BTC_UNITTEST'], ticker_interval=30)
assert os.path.isfile(file) is True
assert ('freqtrade.optimize',
logging.INFO,
'Download the pair: "BTC_ETH", Interval: 30 min') not in caplog.record_tuples
assert not log_has('Download the pair: "BTC_ETH", Interval: 30 min', caplog.record_tuples)
_clean_test_file(file)
@ -73,9 +72,7 @@ def test_load_data_5min_ticker(default_conf, ticker_history, mocker, caplog):
_backup_file(file, copy_file=True)
optimize.load_data(None, pairs=['BTC_ETH'], ticker_interval=5)
assert os.path.isfile(file) is True
assert ('freqtrade.optimize',
logging.INFO,
'Download the pair: "BTC_ETH", Interval: 5 min') not in caplog.record_tuples
assert not log_has('Download the pair: "BTC_ETH", Interval: 5 min', caplog.record_tuples)
_clean_test_file(file)
@ -90,9 +87,7 @@ def test_load_data_1min_ticker(default_conf, ticker_history, mocker, caplog):
_backup_file(file, copy_file=True)
optimize.load_data(None, ticker_interval=1, pairs=['BTC_ETH'])
assert os.path.isfile(file) is True
assert ('freqtrade.optimize',
logging.INFO,
'Download the pair: "BTC_ETH", Interval: 1 min') not in caplog.record_tuples
assert not log_has('Download the pair: "BTC_ETH", Interval: 1 min', caplog.record_tuples)
_clean_test_file(file)
@ -107,9 +102,7 @@ def test_load_data_with_new_pair_1min(default_conf, ticker_history, mocker, capl
_backup_file(file)
optimize.load_data(None, ticker_interval=1, pairs=['BTC_MEME'])
assert os.path.isfile(file) is True
assert ('freqtrade.optimize',
logging.INFO,
'Download the pair: "BTC_MEME", Interval: 1 min') in caplog.record_tuples
assert log_has('Download the pair: "BTC_MEME", Interval: 1 min', caplog.record_tuples)
_clean_test_file(file)
@ -174,9 +167,7 @@ def test_download_pairs_exception(default_conf, ticker_history, mocker, caplog):
# clean files freshly downloaded
_clean_test_file(file1_1)
_clean_test_file(file1_5)
assert ('freqtrade.optimize.__init__',
logging.INFO,
'Failed to download the pair: "BTC-MEME", Interval: 1 min') in caplog.record_tuples
assert log_has('Failed to download the pair: "BTC-MEME", Interval: 1 min', caplog.record_tuples)
def test_download_backtesting_testdata(default_conf, ticker_history, mocker):
@ -280,7 +271,6 @@ def test_trim_tickerlist():
def test_file_dump_json():
file = 'freqtrade/tests/testdata/test_{id}.json'.format(id=str(uuid.uuid4()))
data = {'bar': 'foo'}

View File

@ -6,7 +6,7 @@ import arrow
import logging
from pandas import DataFrame
import freqtrade.tests.conftest as tt # test tools
from freqtrade.tests.conftest import log_has
from freqtrade.analyze import (get_signal, parse_ticker_dataframe,
populate_buy_trend, populate_indicators,
populate_sell_trend)
@ -73,8 +73,7 @@ def test_get_signal_empty(default_conf, mocker, caplog):
caplog.set_level(logging.INFO)
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=None)
assert (False, False) == get_signal('foo', int(default_conf['ticker_interval']))
assert tt.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):
@ -83,8 +82,7 @@ def test_get_signal_exception_valueerror(default_conf, mocker, caplog):
mocker.patch('freqtrade.analyze.analyze_ticker',
side_effect=ValueError('xyz'))
assert (False, False) == get_signal('foo', int(default_conf['ticker_interval']))
assert tt.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):
@ -92,8 +90,7 @@ def test_get_signal_empty_dataframe(default_conf, mocker, caplog):
mocker.patch('freqtrade.analyze.get_ticker_history', return_value=1)
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=DataFrame([]))
assert (False, False) == get_signal('xyz', int(default_conf['ticker_interval']))
assert tt.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):
@ -104,8 +101,8 @@ def test_get_signal_old_dataframe(default_conf, mocker, caplog):
ticks = DataFrame([{'buy': 1, 'date': oldtime}])
mocker.patch('freqtrade.analyze.analyze_ticker', return_value=DataFrame(ticks))
assert (False, False) == get_signal('xyz', int(default_conf['ticker_interval']))
assert tt.log_has('Outdated history for pair xyz. Last tick is 11 minutes old',
caplog.record_tuples)
assert log_has('Outdated history for pair xyz. Last tick is 11 minutes old',
caplog.record_tuples)
def test_get_signal_handles_exceptions(mocker):

View File

@ -9,13 +9,13 @@ import requests
from sqlalchemy import create_engine
import freqtrade.main as main
import freqtrade.tests.conftest as tt # test tools
from freqtrade import DependencyException, OperationalException
from freqtrade.exchange import Exchanges
from freqtrade.main import (_process, check_handle_timedout, create_trade,
execute_sell, get_target_bid, handle_trade, init)
from freqtrade.misc import State, get_state
from freqtrade.persistence import Trade
from freqtrade.tests.conftest import log_has
def test_parse_args_backtesting(mocker):
@ -73,7 +73,7 @@ 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(int(default_conf['ticker_interval']))
tt.log_has('Unable to create trade:', caplog.record_tuples)
assert log_has('Unable to create trade: ', caplog.record_tuples)
def test_process_trade_creation(default_conf, ticker, limit_buy_order, health, mocker):
@ -385,11 +385,11 @@ def test_handle_trade_roi(default_conf, ticker, mocker, caplog):
# if ROI is reached we must sell
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (False, True))
assert handle_trade(trade, interval=int(default_conf['ticker_interval']))
assert ('freqtrade', logging.DEBUG, 'Required profit reached. Selling..') in caplog.record_tuples
assert log_has('Required profit reached. Selling..', caplog.record_tuples)
# if ROI is reached we must sell even if sell-signal is not signalled
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (False, True))
assert handle_trade(trade, interval=int(default_conf['ticker_interval']))
assert ('freqtrade', logging.DEBUG, 'Required profit reached. Selling..') in caplog.record_tuples
assert log_has('Required profit reached. Selling..', caplog.record_tuples)
def test_handle_trade_experimental(default_conf, ticker, mocker, caplog):
@ -416,8 +416,7 @@ def test_handle_trade_experimental(default_conf, ticker, mocker, caplog):
assert value_returned is False
mocker.patch('freqtrade.main.get_signal', side_effect=lambda s, t: (False, True))
assert handle_trade(trade, int(default_conf['ticker_interval']))
s = 'Sell signal received. Selling..'
assert ('freqtrade', logging.DEBUG, s) in caplog.record_tuples
assert log_has('Sell signal received. Selling..', caplog.record_tuples)
def test_close_trade(default_conf, ticker, limit_buy_order, limit_sell_order, mocker):