From 1634297685489bd1dd47802801bef7daae1a7238 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 18 Feb 2020 20:12:10 +0100 Subject: [PATCH 1/6] Move strategies to test subfolder --- tests/commands/test_commands.py | 4 ++-- tests/conftest.py | 1 + .../strategy/strats}/default_strategy.py | 0 tests/strategy/{ => strats}/failing_strategy.py | 0 tests/strategy/{ => strats}/legacy_strategy.py | 0 tests/strategy/test_default_strategy.py | 2 +- tests/strategy/test_interface.py | 2 +- tests/strategy/test_strategy.py | 14 +++++++------- 8 files changed, 12 insertions(+), 11 deletions(-) rename {freqtrade/strategy => tests/strategy/strats}/default_strategy.py (100%) rename tests/strategy/{ => strats}/failing_strategy.py (100%) rename tests/strategy/{ => strats}/legacy_strategy.py (100%) diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index ee1db5db5..55bd7306d 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -640,7 +640,7 @@ def test_start_list_strategies(mocker, caplog, capsys): args = [ "list-strategies", "--strategy-path", - str(Path(__file__).parent.parent / "strategy"), + str(Path(__file__).parent.parent / "strategy" / "strats"), "-1" ] pargs = get_args(args) @@ -655,7 +655,7 @@ def test_start_list_strategies(mocker, caplog, capsys): args = [ "list-strategies", "--strategy-path", - str(Path(__file__).parent.parent / "strategy"), + str(Path(__file__).parent.parent / "strategy" / "strats"), ] pargs = get_args(args) # pargs['config'] = None diff --git a/tests/conftest.py b/tests/conftest.py index e897dbccd..acb730330 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -257,6 +257,7 @@ def default_conf(testdatadir): "db_url": "sqlite://", "user_data_dir": Path("user_data"), "verbosity": 3, + "strategy_path": str(Path(__file__).parent / "strategy" / "strats"), "strategy": "DefaultStrategy" } return configuration diff --git a/freqtrade/strategy/default_strategy.py b/tests/strategy/strats/default_strategy.py similarity index 100% rename from freqtrade/strategy/default_strategy.py rename to tests/strategy/strats/default_strategy.py diff --git a/tests/strategy/failing_strategy.py b/tests/strategy/strats/failing_strategy.py similarity index 100% rename from tests/strategy/failing_strategy.py rename to tests/strategy/strats/failing_strategy.py diff --git a/tests/strategy/legacy_strategy.py b/tests/strategy/strats/legacy_strategy.py similarity index 100% rename from tests/strategy/legacy_strategy.py rename to tests/strategy/strats/legacy_strategy.py diff --git a/tests/strategy/test_default_strategy.py b/tests/strategy/test_default_strategy.py index 17d6b8ee0..0b8ea9f85 100644 --- a/tests/strategy/test_default_strategy.py +++ b/tests/strategy/test_default_strategy.py @@ -1,6 +1,6 @@ from pandas import DataFrame -from freqtrade.strategy.default_strategy import DefaultStrategy +from .strats.default_strategy import DefaultStrategy def test_default_strategy_structure(): diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index a28519383..def64425e 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -11,7 +11,7 @@ from freqtrade.data.converter import parse_ticker_dataframe from freqtrade.data.history import load_tickerdata_file from freqtrade.persistence import Trade from freqtrade.resolvers import StrategyResolver -from freqtrade.strategy.default_strategy import DefaultStrategy +from .strats.default_strategy import DefaultStrategy from tests.conftest import get_patched_exchange, log_has # Avoid to reinit the same object again and again diff --git a/tests/strategy/test_strategy.py b/tests/strategy/test_strategy.py index 379260599..5c6de8260 100644 --- a/tests/strategy/test_strategy.py +++ b/tests/strategy/test_strategy.py @@ -31,21 +31,21 @@ def test_search_strategy(): def test_search_all_strategies_no_failed(): - directory = Path(__file__).parent + directory = Path(__file__).parent / "strats" strategies = StrategyResolver.search_all_objects(directory, enum_failed=False) assert isinstance(strategies, list) - assert len(strategies) == 3 + assert len(strategies) == 2 assert isinstance(strategies[0], dict) def test_search_all_strategies_with_failed(): - directory = Path(__file__).parent + directory = Path(__file__).parent / "strats" strategies = StrategyResolver.search_all_objects(directory, enum_failed=True) assert isinstance(strategies, list) - assert len(strategies) == 4 + assert len(strategies) == 3 # with enum_failed=True search_all_objects() shall find 3 good strategies # and 1 which fails to load - assert len([x for x in strategies if x['class'] is not None]) == 3 + assert len([x for x in strategies if x['class'] is not None]) == 2 assert len([x for x in strategies if x['class'] is None]) == 1 @@ -326,7 +326,7 @@ def test_strategy_override_use_sell_profit_only(caplog, default_conf): @pytest.mark.filterwarnings("ignore:deprecated") def test_deprecate_populate_indicators(result, default_conf): - default_location = path.join(path.dirname(path.realpath(__file__))) + default_location = Path(__file__).parent / "strats" default_conf.update({'strategy': 'TestStrategyLegacy', 'strategy_path': default_location}) strategy = StrategyResolver.load_strategy(default_conf) @@ -360,7 +360,7 @@ def test_deprecate_populate_indicators(result, default_conf): @pytest.mark.filterwarnings("ignore:deprecated") def test_call_deprecated_function(result, monkeypatch, default_conf): - default_location = path.join(path.dirname(path.realpath(__file__))) + default_location = Path(__file__).parent / "strats" default_conf.update({'strategy': 'TestStrategyLegacy', 'strategy_path': default_location}) strategy = StrategyResolver.load_strategy(default_conf) From d91b9d125314c62119b75142fb833ffd0a829bc1 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 18 Feb 2020 20:26:20 +0100 Subject: [PATCH 2/6] Fix some tests, don't default to freqtrade/strategy for imports --- freqtrade/resolvers/iresolver.py | 4 +++- freqtrade/resolvers/strategy_resolver.py | 2 +- tests/strategy/test_strategy.py | 10 ++++------ tests/test_configuration.py | 1 + 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/freqtrade/resolvers/iresolver.py b/freqtrade/resolvers/iresolver.py index 922a2700a..764759289 100644 --- a/freqtrade/resolvers/iresolver.py +++ b/freqtrade/resolvers/iresolver.py @@ -28,7 +28,9 @@ class IResolver: def build_search_paths(cls, config: Dict[str, Any], user_subdir: Optional[str] = None, extra_dir: Optional[str] = None) -> List[Path]: - abs_paths: List[Path] = [cls.initial_search_path] + abs_paths: List[Path] = [] + if cls.initial_search_path: + abs_paths.append(cls.initial_search_path) if user_subdir: abs_paths.insert(0, config['user_data_dir'].joinpath(user_subdir)) diff --git a/freqtrade/resolvers/strategy_resolver.py b/freqtrade/resolvers/strategy_resolver.py index bb8ff870e..cddc7c9cd 100644 --- a/freqtrade/resolvers/strategy_resolver.py +++ b/freqtrade/resolvers/strategy_resolver.py @@ -27,7 +27,7 @@ class StrategyResolver(IResolver): object_type = IStrategy object_type_str = "Strategy" user_subdir = USERPATH_STRATEGIES - initial_search_path = Path(__file__).parent.parent.joinpath('strategy').resolve() + initial_search_path = None @staticmethod def load_strategy(config: Dict[str, Any] = None) -> IStrategy: diff --git a/tests/strategy/test_strategy.py b/tests/strategy/test_strategy.py index 5c6de8260..27bbb2d3b 100644 --- a/tests/strategy/test_strategy.py +++ b/tests/strategy/test_strategy.py @@ -2,7 +2,6 @@ import logging import warnings from base64 import urlsafe_b64encode -from os import path from pathlib import Path import pytest @@ -15,7 +14,7 @@ from tests.conftest import log_has, log_has_re def test_search_strategy(): - default_location = Path(__file__).parent.parent.joinpath('strategy').resolve() + default_location = Path(__file__).parent / 'strats' s, _ = StrategyResolver._search_object( directory=default_location, @@ -72,13 +71,12 @@ def test_load_strategy_base64(result, caplog, default_conf): def test_load_strategy_invalid_directory(result, caplog, default_conf): default_conf['strategy'] = 'DefaultStrategy' extra_dir = Path.cwd() / 'some/path' - strategy = StrategyResolver._load_strategy('DefaultStrategy', config=default_conf, - extra_dir=extra_dir) + with pytest.raises(OperationalException): + StrategyResolver._load_strategy('DefaultStrategy', config=default_conf, + extra_dir=extra_dir) assert log_has_re(r'Path .*' + r'some.*path.*' + r'.* does not exist', caplog) - assert 'rsi' in strategy.advise_indicators(result, {'pair': 'ETH/BTC'}) - def test_load_not_found_strategy(default_conf): default_conf['strategy'] = 'NotFoundStrategy' diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 74de166c1..d810305db 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -212,6 +212,7 @@ def test_load_config_file_exception(mocker) -> None: def test_load_config(default_conf, mocker) -> None: + del default_conf['strategy_path'] patched_configuration_load_config_file(mocker, default_conf) args = Arguments(['trade']).get_parsed_arg() From 09d89fbfb39b5aa5f75a589b82bdba11800656b0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 19 Feb 2020 07:15:55 +0100 Subject: [PATCH 3/6] Fix last test --- tests/optimize/test_backtesting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index bba15c156..9cfd662c1 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -759,7 +759,7 @@ def test_backtest_start_multi_strat(default_conf, mocker, caplog, testdatadir): 'backtesting', '--config', 'config.json', '--datadir', str(testdatadir), - '--strategy-path', str(Path(__file__).parents[2] / 'freqtrade/templates'), + '--strategy-path', str(Path(__file__).parents[1] / 'strategy/strats'), '--ticker-interval', '1m', '--timerange', '1510694220-1510700340', '--enable-position-stacking', From a7342bd9106ac1028fc12b3391c2ab633b9b3919 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 19 Feb 2020 19:42:04 +0100 Subject: [PATCH 4/6] Fix non-existing strategy loading --- tests/optimize/test_backtesting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index 9cfd662c1..337194ab1 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -766,7 +766,7 @@ def test_backtest_start_multi_strat(default_conf, mocker, caplog, testdatadir): '--disable-max-market-positions', '--strategy-list', 'DefaultStrategy', - 'SampleStrategy', + 'TestStrategyLegacy', ] args = get_args(args) start_backtesting(args) @@ -789,7 +789,7 @@ def test_backtest_start_multi_strat(default_conf, mocker, caplog, testdatadir): 'up to 2017-11-14T22:58:00+00:00 (0 days)..', 'Parameter --enable-position-stacking detected ...', 'Running backtesting for Strategy DefaultStrategy', - 'Running backtesting for Strategy SampleStrategy', + 'Running backtesting for Strategy TestStrategyLegacy', ] for line in exists: From 5adbe3c2d3ac75eb20b08b93d8902cf3ab25a643 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 19 Feb 2020 19:50:01 +0100 Subject: [PATCH 5/6] initial search path is optional ... --- freqtrade/resolvers/iresolver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/resolvers/iresolver.py b/freqtrade/resolvers/iresolver.py index 764759289..52d944f2c 100644 --- a/freqtrade/resolvers/iresolver.py +++ b/freqtrade/resolvers/iresolver.py @@ -22,7 +22,7 @@ class IResolver: object_type: Type[Any] object_type_str: str user_subdir: Optional[str] = None - initial_search_path: Path + initial_search_path: Optional[Path] @classmethod def build_search_paths(cls, config: Dict[str, Any], user_subdir: Optional[str] = None, From 10668bb2490c2ed56f47f5536fdf0892c2173648 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 20 Feb 2020 06:22:36 +0100 Subject: [PATCH 6/6] Update tests/strategy/test_strategy.py Co-Authored-By: hroff-1902 <47309513+hroff-1902@users.noreply.github.com> --- tests/strategy/test_strategy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/strategy/test_strategy.py b/tests/strategy/test_strategy.py index 27bbb2d3b..13ca68bf0 100644 --- a/tests/strategy/test_strategy.py +++ b/tests/strategy/test_strategy.py @@ -42,7 +42,7 @@ def test_search_all_strategies_with_failed(): strategies = StrategyResolver.search_all_objects(directory, enum_failed=True) assert isinstance(strategies, list) assert len(strategies) == 3 - # with enum_failed=True search_all_objects() shall find 3 good strategies + # with enum_failed=True search_all_objects() shall find 2 good strategies # and 1 which fails to load assert len([x for x in strategies if x['class'] is not None]) == 2 assert len([x for x in strategies if x['class'] is None]) == 1