Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Dec 28, 2019
1 parent e4f185f commit 7a6476c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
6 changes: 3 additions & 3 deletions freqtrade/data/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ def _load_cached_data_for_updating(pair: str, timeframe: str, timerange: Optiona
if not data.empty:
if start and start < data.iloc[0]['date']:
# Earlier data than existing data requested, redownload all
return DataFrame(columns=DEFAULT_DATAFRAME_COLUMNS), None

start = data.iloc[-1]['date']
data = DataFrame(columns=DEFAULT_DATAFRAME_COLUMNS)
else:
start = data.iloc[-1]['date']

start_ms = int(start.timestamp() * 1000) if start else None
return data, start_ms
Expand Down
38 changes: 22 additions & 16 deletions tests/data/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

import arrow
from pandas import DataFrame
from pandas.testing import assert_frame_equal

from freqtrade.configuration import TimeRange
from freqtrade.data.converter import parse_ticker_dataframe
from freqtrade.data.datahandlers import get_datahandler
from freqtrade.data.datahandlers.jsondatahandler import (JsonDataHandler,
JsonGzDataHandler)
Expand Down Expand Up @@ -163,50 +165,54 @@ def test_json_pair_trades_filename():
assert fn == Path('freqtrade/hello/world/ETH_BTC-trades.json.gz')


def test_load_cached_data_for_updating(mocker) -> None:
datadir = Path(__file__).parent.parent.joinpath('testdata')
def test_load_cached_data_for_updating(mocker, testdatadir) -> None:

data_handler = get_datahandler(testdatadir, 'json')

test_data = None
test_filename = datadir.joinpath('UNITTEST_BTC-1m.json')
test_filename = testdatadir.joinpath('UNITTEST_BTC-1m.json')
with open(test_filename, "rt") as file:
test_data = json.load(file)

test_data_df = parse_ticker_dataframe(test_data, '1m', 'UNITTEST/BTC',
fill_missing=False, drop_incomplete=False)
# now = last cached item + 1 hour
now_ts = test_data[-1][0] / 1000 + 60 * 60
mocker.patch('arrow.utcnow', return_value=arrow.get(now_ts))

# timeframe starts earlier than the cached data
# should fully update data
timerange = TimeRange('date', None, test_data[0][0] / 1000 - 1, 0)
data, start_ts = _load_cached_data_for_updating(datadir, 'UNITTEST/BTC', '1m', timerange)
assert data == []
data, start_ts = _load_cached_data_for_updating('UNITTEST/BTC', '1m', timerange, data_handler)
assert data.empty
assert start_ts == test_data[0][0] - 1000

# timeframe starts in the center of the cached data
# should return the chached data w/o the last item
timerange = TimeRange('date', None, test_data[0][0] / 1000 + 1, 0)
data, start_ts = _load_cached_data_for_updating(datadir, 'UNITTEST/BTC', '1m', timerange)
assert data == test_data[:-1]
assert test_data[-2][0] < start_ts < test_data[-1][0]
data, start_ts = _load_cached_data_for_updating('UNITTEST/BTC', '1m', timerange, data_handler)

assert_frame_equal(data, test_data_df.iloc[:-1])
assert test_data[-2][0] <= start_ts < test_data[-1][0]

# timeframe starts after the chached data
# should return the chached data w/o the last item
timerange = TimeRange('date', None, test_data[-1][0] / 1000 + 1, 0)
data, start_ts = _load_cached_data_for_updating(datadir, 'UNITTEST/BTC', '1m', timerange)
assert data == test_data[:-1]
assert test_data[-2][0] < start_ts < test_data[-1][0]
timerange = TimeRange('date', None, test_data[-1][0] / 1000 + 100, 0)
data, start_ts = _load_cached_data_for_updating('UNITTEST/BTC', '1m', timerange, data_handler)
assert_frame_equal(data, test_data_df.iloc[:-1])
assert test_data[-2][0] <= start_ts < test_data[-1][0]

# no datafile exist
# should return timestamp start time
timerange = TimeRange('date', None, now_ts - 10000, 0)
data, start_ts = _load_cached_data_for_updating(datadir, 'NONEXIST/BTC', '1m', timerange)
assert data == []
data, start_ts = _load_cached_data_for_updating('NONEXIST/BTC', '1m', timerange, data_handler)
assert data.empty
assert start_ts == (now_ts - 10000) * 1000

# no datafile exist, no timeframe is set
# should return an empty array and None
data, start_ts = _load_cached_data_for_updating(datadir, 'NONEXIST/BTC', '1m', None)
assert data == []
data, start_ts = _load_cached_data_for_updating('NONEXIST/BTC', '1m', None, data_handler)
assert data.empty
assert start_ts is None


Expand Down

0 comments on commit 7a6476c

Please sign in to comment.