Skip to content

Commit

Permalink
Use fixture to determine test_data_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Sep 7, 2019
1 parent bde82e9 commit fe631ff
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 117 deletions.
3 changes: 2 additions & 1 deletion freqtrade/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,7 @@ def rpc_balance():
}


def make_testdata_path(datadir) -> Path:
@pytest.fixture
def testdatadir() -> Path:
"""Return the path where testdata files are stored"""
return (Path(__file__).parent / "testdata").resolve()
19 changes: 9 additions & 10 deletions freqtrade/tests/data/test_btanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
load_backtest_data, load_trades,
load_trades_from_db)
from freqtrade.data.history import load_data, load_pair_history
from freqtrade.tests.conftest import make_testdata_path
from freqtrade.tests.test_persistence import create_mock_trades


def test_load_backtest_data():
def test_load_backtest_data(testdatadir):

filename = make_testdata_path(None) / "backtest-result_test.json"
filename = testdatadir / "backtest-result_test.json"
bt_data = load_backtest_data(filename)
assert isinstance(bt_data, DataFrame)
assert list(bt_data.columns) == BT_DATA_COLUMNS + ["profitabs"]
Expand Down Expand Up @@ -52,12 +51,12 @@ def test_load_trades_db(default_conf, fee, mocker):
assert col in trades.columns


def test_extract_trades_of_period():
def test_extract_trades_of_period(testdatadir):
pair = "UNITTEST/BTC"
timerange = TimeRange(None, 'line', 0, -1000)

data = load_pair_history(pair=pair, ticker_interval='1m',
datadir=None, timerange=timerange)
datadir=testdatadir, timerange=timerange)

# timerange = 2017-11-14 06:07 - 2017-11-14 22:58:00
trades = DataFrame(
Expand Down Expand Up @@ -108,9 +107,9 @@ def test_load_trades(default_conf, mocker):
assert bt_mock.call_count == 1


def test_combine_tickers_with_mean():
def test_combine_tickers_with_mean(testdatadir):
pairs = ["ETH/BTC", "XLM/BTC"]
tickers = load_data(datadir=None,
tickers = load_data(datadir=testdatadir,
pairs=pairs,
ticker_interval='5m'
)
Expand All @@ -121,13 +120,13 @@ def test_combine_tickers_with_mean():
assert "mean" in df.columns


def test_create_cum_profit():
filename = make_testdata_path(None) / "backtest-result_test.json"
def test_create_cum_profit(testdatadir):
filename = testdatadir / "backtest-result_test.json"
bt_data = load_backtest_data(filename)
timerange = TimeRange.parse_timerange("20180110-20180112")

df = load_pair_history(pair="POWR/BTC", ticker_interval='5m',
datadir=None, timerange=timerange)
datadir=testdatadir, timerange=timerange)

cum_profits = create_cum_profit(df.set_index('date'),
bt_data[bt_data["pair"] == 'POWR/BTC'],
Expand Down
4 changes: 2 additions & 2 deletions freqtrade/tests/data/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def test_parse_ticker_dataframe(ticker_history_list, caplog):
assert log_has('Parsing tickerlist to dataframe', caplog)


def test_ohlcv_fill_up_missing_data(caplog):
data = load_pair_history(datadir=None,
def test_ohlcv_fill_up_missing_data(testdatadir, caplog):
data = load_pair_history(datadir=testdatadir,
ticker_interval='1m',
refresh_pairs=False,
pair='UNITTEST/BTC',
Expand Down
87 changes: 44 additions & 43 deletions freqtrade/tests/data/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
from freqtrade.exchange import timeframe_to_minutes
from freqtrade.misc import file_dump_json
from freqtrade.strategy.default_strategy import DefaultStrategy
from freqtrade.tests.conftest import (get_patched_exchange, log_has,
patch_exchange, make_testdata_path)
from freqtrade.tests.conftest import get_patched_exchange, log_has, log_has_re, patch_exchange

# Change this if modifying UNITTEST/BTC testdatafile
_BTC_UNITTEST_LENGTH = 13681
Expand Down Expand Up @@ -60,17 +59,17 @@ def _clean_test_file(file: str) -> None:
os.rename(file_swp, file)


def test_load_data_30min_ticker(mocker, caplog, default_conf) -> None:
ld = history.load_pair_history(pair='UNITTEST/BTC', ticker_interval='30m', datadir=None)
def test_load_data_30min_ticker(mocker, caplog, default_conf, testdatadir) -> None:
ld = history.load_pair_history(pair='UNITTEST/BTC', ticker_interval='30m', datadir=testdatadir)
assert isinstance(ld, DataFrame)
assert not log_has(
'Download history data for pair: "UNITTEST/BTC", interval: 30m '
'and store in None.', caplog
)


def test_load_data_7min_ticker(mocker, caplog, default_conf) -> None:
ld = history.load_pair_history(pair='UNITTEST/BTC', ticker_interval='7m', datadir=None)
def test_load_data_7min_ticker(mocker, caplog, default_conf, testdatadir) -> None:
ld = history.load_pair_history(pair='UNITTEST/BTC', ticker_interval='7m', datadir=testdatadir)
assert not isinstance(ld, DataFrame)
assert ld is None
assert log_has(
Expand All @@ -80,11 +79,11 @@ def test_load_data_7min_ticker(mocker, caplog, default_conf) -> None:
)


def test_load_data_1min_ticker(ticker_history, mocker, caplog) -> None:
def test_load_data_1min_ticker(ticker_history, mocker, caplog, testdatadir) -> None:
mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ticker_history)
file = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'UNITTEST_BTC-1m.json')
_backup_file(file, copy_file=True)
history.load_data(datadir=None, ticker_interval='1m', pairs=['UNITTEST/BTC'])
history.load_data(datadir=testdatadir, ticker_interval='1m', pairs=['UNITTEST/BTC'])
assert os.path.isfile(file) is True
assert not log_has(
'Download history data for pair: "UNITTEST/BTC", interval: 1m '
Expand All @@ -93,7 +92,8 @@ def test_load_data_1min_ticker(ticker_history, mocker, caplog) -> None:
_clean_test_file(file)


def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, default_conf) -> None:
def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog,
default_conf, testdatadir) -> None:
"""
Test load_pair_history() with 1 min ticker
"""
Expand All @@ -103,7 +103,7 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, defau

_backup_file(file)
# do not download a new pair if refresh_pairs isn't set
history.load_pair_history(datadir=None,
history.load_pair_history(datadir=testdatadir,
ticker_interval='1m',
refresh_pairs=False,
pair='MEME/BTC')
Expand All @@ -115,18 +115,18 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, defau
)

# download a new pair if refresh_pairs is set
history.load_pair_history(datadir=None,
history.load_pair_history(datadir=testdatadir,
ticker_interval='1m',
refresh_pairs=True,
exchange=exchange,
pair='MEME/BTC')
assert os.path.isfile(file) is True
assert log_has(
assert log_has_re(
'Download history data for pair: "MEME/BTC", interval: 1m '
'and store in None.', caplog
'and store in .*', caplog
)
with pytest.raises(OperationalException, match=r'Exchange needs to be initialized when.*'):
history.load_pair_history(datadir=None,
history.load_pair_history(datadir=testdatadir,
ticker_interval='1m',
refresh_pairs=True,
exchange=None,
Expand Down Expand Up @@ -159,8 +159,8 @@ def test_load_data_live_noexchange(default_conf, mocker, caplog) -> None:
)


def test_testdata_path() -> None:
assert str(Path('freqtrade') / 'tests' / 'testdata') in str(make_testdata_path(None))
def test_testdata_path(testdatadir) -> None:
assert str(Path('freqtrade') / 'tests' / 'testdata') in str(testdatadir)


def test_load_cached_data_for_updating(mocker) -> None:
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_load_cached_data_for_updating(mocker) -> None:
assert start_ts is None


def test_download_pair_history(ticker_history_list, mocker, default_conf) -> None:
def test_download_pair_history(ticker_history_list, mocker, default_conf, testdatadir) -> None:
mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ticker_history_list)
exchange = get_patched_exchange(mocker, default_conf)
file1_1 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'MEME_BTC-1m.json')
Expand All @@ -264,10 +264,10 @@ def test_download_pair_history(ticker_history_list, mocker, default_conf) -> Non
assert os.path.isfile(file1_1) is False
assert os.path.isfile(file2_1) is False

assert download_pair_history(datadir=None, exchange=exchange,
assert download_pair_history(datadir=testdatadir, exchange=exchange,
pair='MEME/BTC',
ticker_interval='1m')
assert download_pair_history(datadir=None, exchange=exchange,
assert download_pair_history(datadir=testdatadir, exchange=exchange,
pair='CFI/BTC',
ticker_interval='1m')
assert not exchange._pairs_last_refresh_time
Expand All @@ -281,10 +281,10 @@ def test_download_pair_history(ticker_history_list, mocker, default_conf) -> Non
assert os.path.isfile(file1_5) is False
assert os.path.isfile(file2_5) is False

assert download_pair_history(datadir=None, exchange=exchange,
assert download_pair_history(datadir=testdatadir, exchange=exchange,
pair='MEME/BTC',
ticker_interval='5m')
assert download_pair_history(datadir=None, exchange=exchange,
assert download_pair_history(datadir=testdatadir, exchange=exchange,
pair='CFI/BTC',
ticker_interval='5m')
assert not exchange._pairs_last_refresh_time
Expand All @@ -296,20 +296,21 @@ def test_download_pair_history(ticker_history_list, mocker, default_conf) -> Non
_clean_test_file(file2_5)


def test_download_pair_history2(mocker, default_conf) -> None:
def test_download_pair_history2(mocker, default_conf, testdatadir) -> None:
tick = [
[1509836520000, 0.00162008, 0.00162008, 0.00162008, 0.00162008, 108.14853839],
[1509836580000, 0.00161, 0.00161, 0.00161, 0.00161, 82.390199]
]
json_dump_mock = mocker.patch('freqtrade.misc.file_dump_json', return_value=None)
mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=tick)
exchange = get_patched_exchange(mocker, default_conf)
download_pair_history(None, exchange, pair="UNITTEST/BTC", ticker_interval='1m')
download_pair_history(None, exchange, pair="UNITTEST/BTC", ticker_interval='3m')
download_pair_history(testdatadir, exchange, pair="UNITTEST/BTC", ticker_interval='1m')
download_pair_history(testdatadir, exchange, pair="UNITTEST/BTC", ticker_interval='3m')
assert json_dump_mock.call_count == 2


def test_download_backtesting_data_exception(ticker_history, mocker, caplog, default_conf) -> None:
def test_download_backtesting_data_exception(ticker_history, mocker, caplog,
default_conf, testdatadir) -> None:
mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv',
side_effect=Exception('File Error'))

Expand All @@ -320,7 +321,7 @@ def test_download_backtesting_data_exception(ticker_history, mocker, caplog, def
_backup_file(file1_1)
_backup_file(file1_5)

assert not download_pair_history(datadir=None, exchange=exchange,
assert not download_pair_history(datadir=testdatadir, exchange=exchange,
pair='MEME/BTC',
ticker_interval='1m')
# clean files freshly downloaded
Expand All @@ -332,22 +333,22 @@ def test_download_backtesting_data_exception(ticker_history, mocker, caplog, def
)


def test_load_tickerdata_file() -> None:
def test_load_tickerdata_file(testdatadir) -> None:
# 7 does not exist in either format.
assert not load_tickerdata_file(None, 'UNITTEST/BTC', '7m')
assert not load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '7m')
# 1 exists only as a .json
tickerdata = load_tickerdata_file(None, 'UNITTEST/BTC', '1m')
tickerdata = load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '1m')
assert _BTC_UNITTEST_LENGTH == len(tickerdata)
# 8 .json is empty and will fail if it's loaded. .json.gz is a copy of 1.json
tickerdata = load_tickerdata_file(None, 'UNITTEST/BTC', '8m')
tickerdata = load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '8m')
assert _BTC_UNITTEST_LENGTH == len(tickerdata)


def test_load_partial_missing(caplog) -> None:
def test_load_partial_missing(testdatadir, caplog) -> None:
# Make sure we start fresh - test missing data at start
start = arrow.get('2018-01-01T00:00:00')
end = arrow.get('2018-01-11T00:00:00')
tickerdata = history.load_data(None, '5m', ['UNITTEST/BTC'],
tickerdata = history.load_data(testdatadir, '5m', ['UNITTEST/BTC'],
refresh_pairs=False,
timerange=TimeRange('date', 'date',
start.timestamp, end.timestamp))
Expand All @@ -362,7 +363,7 @@ def test_load_partial_missing(caplog) -> None:
caplog.clear()
start = arrow.get('2018-01-10T00:00:00')
end = arrow.get('2018-02-20T00:00:00')
tickerdata = history.load_data(datadir=None, ticker_interval='5m',
tickerdata = history.load_data(datadir=testdatadir, ticker_interval='5m',
pairs=['UNITTEST/BTC'], refresh_pairs=False,
timerange=TimeRange('date', 'date',
start.timestamp, end.timestamp))
Expand Down Expand Up @@ -502,13 +503,13 @@ def test_file_dump_json_tofile() -> None:
_clean_test_file(file)


def test_get_timeframe(default_conf, mocker) -> None:
def test_get_timeframe(default_conf, mocker, testdatadir) -> None:
patch_exchange(mocker)
strategy = DefaultStrategy(default_conf)

data = strategy.tickerdata_to_dataframe(
history.load_data(
datadir=None,
datadir=testdatadir,
ticker_interval='1m',
pairs=['UNITTEST/BTC']
)
Expand All @@ -518,13 +519,13 @@ def test_get_timeframe(default_conf, mocker) -> None:
assert max_date.isoformat() == '2017-11-14T22:58:00+00:00'


def test_validate_backtest_data_warn(default_conf, mocker, caplog) -> None:
def test_validate_backtest_data_warn(default_conf, mocker, caplog, testdatadir) -> None:
patch_exchange(mocker)
strategy = DefaultStrategy(default_conf)

data = strategy.tickerdata_to_dataframe(
history.load_data(
datadir=None,
datadir=testdatadir,
ticker_interval='1m',
pairs=['UNITTEST/BTC'],
fill_up_missing=False
Expand All @@ -540,14 +541,14 @@ def test_validate_backtest_data_warn(default_conf, mocker, caplog) -> None:
caplog)


def test_validate_backtest_data(default_conf, mocker, caplog) -> None:
def test_validate_backtest_data(default_conf, mocker, caplog, testdatadir) -> None:
patch_exchange(mocker)
strategy = DefaultStrategy(default_conf)

timerange = TimeRange('index', 'index', 200, 250)
data = strategy.tickerdata_to_dataframe(
history.load_data(
datadir=None,
datadir=testdatadir,
ticker_interval='5m',
pairs=['UNITTEST/BTC'],
timerange=timerange
Expand All @@ -561,7 +562,7 @@ def test_validate_backtest_data(default_conf, mocker, caplog) -> None:
assert len(caplog.record_tuples) == 0


def test_refresh_backtest_ohlcv_data(mocker, default_conf, markets, caplog):
def test_refresh_backtest_ohlcv_data(mocker, default_conf, markets, caplog, testdatadir):
dl_mock = mocker.patch('freqtrade.data.history.download_pair_history', MagicMock())
mocker.patch(
'freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets)
Expand All @@ -572,7 +573,7 @@ def test_refresh_backtest_ohlcv_data(mocker, default_conf, markets, caplog):
ex = get_patched_exchange(mocker, default_conf)
timerange = TimeRange.parse_timerange("20190101-20190102")
refresh_backtest_ohlcv_data(exchange=ex, pairs=["ETH/BTC", "XRP/BTC"],
timeframes=["1m", "5m"], dl_path=make_testdata_path(None),
timeframes=["1m", "5m"], dl_path=testdatadir,
timerange=timerange, erase=True
)

Expand All @@ -582,7 +583,7 @@ def test_refresh_backtest_ohlcv_data(mocker, default_conf, markets, caplog):
assert log_has("Downloading pair ETH/BTC, interval 1m.", caplog)


def test_download_data_no_markets(mocker, default_conf, caplog):
def test_download_data_no_markets(mocker, default_conf, caplog, testdatadir):
dl_mock = mocker.patch('freqtrade.data.history.download_pair_history', MagicMock())
mocker.patch(
'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={})
Expand All @@ -591,7 +592,7 @@ def test_download_data_no_markets(mocker, default_conf, caplog):
timerange = TimeRange.parse_timerange("20190101-20190102")
unav_pairs = refresh_backtest_ohlcv_data(exchange=ex, pairs=["ETH/BTC", "XRP/BTC"],
timeframes=["1m", "5m"],
dl_path=make_testdata_path(None),
dl_path=testdatadir,
timerange=timerange, erase=False
)

Expand Down

0 comments on commit fe631ff

Please sign in to comment.