From f37c5b70ba3ea81f09ba6f24f61c7047f6b9542e Mon Sep 17 00:00:00 2001 From: xmatthias Date: Tue, 5 Jun 2018 23:53:49 +0200 Subject: [PATCH] Fix tests - read optional argument --- freqtrade/optimize/__init__.py | 2 +- freqtrade/tests/optimize/test_backtesting.py | 6 ++-- freqtrade/tests/optimize/test_optimize.py | 33 ++++++++++---------- freqtrade/tests/test_analyze.py | 3 +- freqtrade/tests/test_arguments.py | 1 - 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/freqtrade/optimize/__init__.py b/freqtrade/optimize/__init__.py index dc48a64ec..00f05cc46 100644 --- a/freqtrade/optimize/__init__.py +++ b/freqtrade/optimize/__init__.py @@ -50,7 +50,7 @@ def trim_tickerlist(tickerlist: List[Dict], timerange: TimeRange) -> List[Dict]: def load_tickerdata_file( datadir: str, pair: str, ticker_interval: str, - timerange: TimeRange) -> Optional[List[Dict]]: + timerange: Optional[TimeRange] = None) -> Optional[List[Dict]]: """ Load a pair from file, :return dict OR empty if unsuccesful diff --git a/freqtrade/tests/optimize/test_backtesting.py b/freqtrade/tests/optimize/test_backtesting.py index 65820ac09..efcee3839 100644 --- a/freqtrade/tests/optimize/test_backtesting.py +++ b/freqtrade/tests/optimize/test_backtesting.py @@ -13,7 +13,7 @@ from arrow import Arrow from freqtrade import optimize from freqtrade.analyze import Analyze -from freqtrade.arguments import Arguments +from freqtrade.arguments import Arguments, TimeRange from freqtrade.optimize.backtesting import Backtesting, start, setup_configuration from freqtrade.tests.conftest import log_has @@ -30,7 +30,7 @@ def trim_dictlist(dict_list, num): def load_data_test(what): - timerange = ((None, 'line'), None, -100) + timerange = TimeRange(None, 'line', 0, -100) data = optimize.load_data(None, ticker_interval='1m', pairs=['UNITTEST/BTC'], timerange=timerange) pair = data['UNITTEST/BTC'] @@ -311,7 +311,7 @@ def test_tickerdata_to_dataframe(default_conf, mocker) -> None: Test Backtesting.tickerdata_to_dataframe() method """ mocker.patch('freqtrade.exchange.validate_pairs', MagicMock(return_value=True)) - timerange = ((None, 'line'), None, -100) + timerange = TimeRange(None, 'line', 0, -100) tick = optimize.load_tickerdata_file(None, 'UNITTEST/BTC', '1m', timerange=timerange) tickerlist = {'UNITTEST/BTC': tick} diff --git a/freqtrade/tests/optimize/test_optimize.py b/freqtrade/tests/optimize/test_optimize.py index 349fa3be3..3f358cfb8 100644 --- a/freqtrade/tests/optimize/test_optimize.py +++ b/freqtrade/tests/optimize/test_optimize.py @@ -11,6 +11,7 @@ from freqtrade.misc import file_dump_json from freqtrade.optimize.__init__ import make_testdata_path, download_pairs, \ download_backtesting_testdata, load_tickerdata_file, trim_tickerlist, \ load_cached_data_for_updating +from freqtrade.arguments import TimeRange from freqtrade.tests.conftest import log_has # Change this if modifying UNITTEST/BTC testdatafile @@ -176,7 +177,7 @@ def test_load_cached_data_for_updating(mocker) -> None: # timeframe starts earlier than the cached data # should fully update data - timerange = (('date', None), test_data[0][0] / 1000 - 1, None) + timerange = TimeRange('date', None, test_data[0][0] / 1000 - 1, 0) data, start_ts = load_cached_data_for_updating(test_filename, '1m', timerange) @@ -187,13 +188,13 @@ def test_load_cached_data_for_updating(mocker) -> None: num_lines = (test_data[-1][0] - test_data[1][0]) / 1000 / 60 + 120 data, start_ts = load_cached_data_for_updating(test_filename, '1m', - ((None, 'line'), None, -num_lines)) + TimeRange(None, 'line', 0, -num_lines)) assert data == [] assert start_ts < test_data[0][0] - 1 # timeframe starts in the center of the cached data # should return the chached data w/o the last item - timerange = (('date', None), test_data[0][0] / 1000 + 1, None) + timerange = TimeRange('date', None, test_data[0][0] / 1000 + 1, 0) data, start_ts = load_cached_data_for_updating(test_filename, '1m', timerange) @@ -202,7 +203,7 @@ def test_load_cached_data_for_updating(mocker) -> None: # same with 'line' timeframe num_lines = (test_data[-1][0] - test_data[1][0]) / 1000 / 60 + 30 - timerange = ((None, 'line'), None, -num_lines) + timerange = TimeRange(None, 'line', 0, -num_lines) data, start_ts = load_cached_data_for_updating(test_filename, '1m', timerange) @@ -211,7 +212,7 @@ def test_load_cached_data_for_updating(mocker) -> None: # timeframe starts after the chached data # should return the chached data w/o the last item - timerange = (('date', None), test_data[-1][0] / 1000 + 1, None) + timerange = TimeRange('date', None, test_data[-1][0] / 1000 + 1, 0) data, start_ts = load_cached_data_for_updating(test_filename, '1m', timerange) @@ -220,7 +221,7 @@ def test_load_cached_data_for_updating(mocker) -> None: # same with 'line' timeframe num_lines = 30 - timerange = ((None, 'line'), None, -num_lines) + timerange = TimeRange(None, 'line', 0, -num_lines) data, start_ts = load_cached_data_for_updating(test_filename, '1m', timerange) @@ -230,7 +231,7 @@ def test_load_cached_data_for_updating(mocker) -> None: # no timeframe is set # should return the chached data w/o the last item num_lines = 30 - timerange = ((None, 'line'), None, -num_lines) + timerange = TimeRange(None, 'line', 0, -num_lines) data, start_ts = load_cached_data_for_updating(test_filename, '1m', timerange) @@ -239,7 +240,7 @@ def test_load_cached_data_for_updating(mocker) -> None: # no datafile exist # should return timestamp start time - timerange = (('date', None), now_ts - 10000, None) + timerange = TimeRange('date', None, now_ts - 10000, 0) data, start_ts = load_cached_data_for_updating(test_filename + 'unexist', '1m', timerange) @@ -248,7 +249,7 @@ def test_load_cached_data_for_updating(mocker) -> None: # same with 'line' timeframe num_lines = 30 - timerange = ((None, 'line'), None, -num_lines) + timerange = TimeRange(None, 'line', 0, -num_lines) data, start_ts = load_cached_data_for_updating(test_filename + 'unexist', '1m', timerange) @@ -343,7 +344,7 @@ def test_trim_tickerlist() -> None: # Test the pattern ^(-\d+)$ # This pattern uses the latest N elements - timerange = ((None, 'line'), None, -5) + timerange = TimeRange(None, 'line', 0, -5) ticker = trim_tickerlist(ticker_list, timerange) ticker_len = len(ticker) @@ -353,7 +354,7 @@ def test_trim_tickerlist() -> None: # Test the pattern ^(\d+)-$ # This pattern keep X element from the end - timerange = (('line', None), 5, None) + timerange = TimeRange('line', None, 5, 0) ticker = trim_tickerlist(ticker_list, timerange) ticker_len = len(ticker) @@ -363,7 +364,7 @@ def test_trim_tickerlist() -> None: # Test the pattern ^(\d+)-(\d+)$ # This pattern extract a window - timerange = (('index', 'index'), 5, 10) + timerange = TimeRange('index', 'index', 5, 10) ticker = trim_tickerlist(ticker_list, timerange) ticker_len = len(ticker) @@ -374,7 +375,7 @@ def test_trim_tickerlist() -> None: # Test the pattern ^(\d{8})-(\d{8})$ # This pattern extract a window between the dates - timerange = (('date', 'date'), ticker_list[5][0] / 1000, ticker_list[10][0] / 1000 - 1) + timerange = TimeRange('date', 'date', ticker_list[5][0] / 1000, ticker_list[10][0] / 1000 - 1) ticker = trim_tickerlist(ticker_list, timerange) ticker_len = len(ticker) @@ -385,7 +386,7 @@ def test_trim_tickerlist() -> None: # Test the pattern ^-(\d{8})$ # This pattern extracts elements from the start to the date - timerange = ((None, 'date'), None, ticker_list[10][0] / 1000 - 1) + timerange = TimeRange(None, 'date', 0, ticker_list[10][0] / 1000 - 1) ticker = trim_tickerlist(ticker_list, timerange) ticker_len = len(ticker) @@ -395,7 +396,7 @@ def test_trim_tickerlist() -> None: # Test the pattern ^(\d{8})-$ # This pattern extracts elements from the date to now - timerange = (('date', None), ticker_list[10][0] / 1000 - 1, None) + timerange = TimeRange('date', None, ticker_list[10][0] / 1000 - 1, None) ticker = trim_tickerlist(ticker_list, timerange) ticker_len = len(ticker) @@ -405,7 +406,7 @@ def test_trim_tickerlist() -> None: # Test a wrong pattern # This pattern must return the list unchanged - timerange = ((None, None), None, 5) + timerange = TimeRange(None, None, None, 5) ticker = trim_tickerlist(ticker_list, timerange) ticker_len = len(ticker) diff --git a/freqtrade/tests/test_analyze.py b/freqtrade/tests/test_analyze.py index 01033ce7d..418f31851 100644 --- a/freqtrade/tests/test_analyze.py +++ b/freqtrade/tests/test_analyze.py @@ -13,6 +13,7 @@ from pandas import DataFrame from freqtrade.analyze import Analyze, SignalType from freqtrade.optimize.__init__ import load_tickerdata_file +from freqtrade.arguments import TimeRange from freqtrade.tests.conftest import log_has # Avoid to reinit the same object again and again @@ -183,7 +184,7 @@ def test_tickerdata_to_dataframe(default_conf) -> None: """ analyze = Analyze(default_conf) - timerange = ((None, 'line'), None, -100) + timerange = TimeRange(None, 'line', 0, -100) tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m', timerange=timerange) tickerlist = {'UNITTEST/BTC': tick} data = analyze.tickerdata_to_dataframe(tickerlist) diff --git a/freqtrade/tests/test_arguments.py b/freqtrade/tests/test_arguments.py index 9c24a6789..6c3ecb913 100644 --- a/freqtrade/tests/test_arguments.py +++ b/freqtrade/tests/test_arguments.py @@ -126,7 +126,6 @@ def test_parse_timerange_incorrect() -> None: timerange = Arguments.parse_timerange('1231006505000-1233360000000') assert TimeRange('date', 'date', 1231006505, 1233360000) != timerange - with pytest.raises(Exception, match=r'Incorrect syntax.*'): Arguments.parse_timerange('-')