tests: extract strategy_safe_wrapper tests

This commit is contained in:
Matthias
2024-11-30 13:46:24 +01:00
parent cbbc386170
commit 88d77658f8
2 changed files with 69 additions and 70 deletions

View File

@@ -27,15 +27,8 @@ from freqtrade.strategy.parameters import (
IntParameter,
RealParameter,
)
from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper
from freqtrade.util import dt_now
from tests.conftest import (
CURRENT_TEST_STRATEGY,
TRADE_SIDES,
create_mock_trades,
log_has,
log_has_re,
)
from tests.conftest import CURRENT_TEST_STRATEGY, TRADE_SIDES, log_has, log_has_re
from .strats.strategy_test_v3 import StrategyTestV3
@@ -900,68 +893,6 @@ def test_is_informative_pairs_callback(default_conf):
assert [] == strategy.gather_informative_pairs()
@pytest.mark.parametrize(
"error",
[
ValueError,
KeyError,
Exception,
],
)
def test_strategy_safe_wrapper_error(caplog, error):
def failing_method():
raise error("This is an error.")
with pytest.raises(StrategyError, match=r"This is an error."):
strategy_safe_wrapper(failing_method, message="DeadBeef")()
assert log_has_re(r"DeadBeef.*", caplog)
ret = strategy_safe_wrapper(failing_method, message="DeadBeef", default_retval=True)()
assert isinstance(ret, bool)
assert ret
caplog.clear()
# Test suppressing error
ret = strategy_safe_wrapper(failing_method, message="DeadBeef", supress_error=True)()
assert log_has_re(r"DeadBeef.*", caplog)
@pytest.mark.parametrize(
"value", [1, 22, 55, True, False, {"a": 1, "b": "112"}, [1, 2, 3, 4], (4, 2, 3, 6)]
)
def test_strategy_safe_wrapper(value):
def working_method(argumentpassedin):
return argumentpassedin
ret = strategy_safe_wrapper(working_method, message="DeadBeef")(value)
assert isinstance(ret, type(value))
assert ret == value
@pytest.mark.usefixtures("init_persistence")
def test_strategy_safe_wrapper_trade_copy(fee):
create_mock_trades(fee)
def working_method(trade):
assert len(trade.orders) > 0
assert trade.orders
trade.orders = []
assert len(trade.orders) == 0
return trade
trade = Trade.get_open_trades()[0]
# Don't assert anything before strategy_wrapper.
# This ensures that relationship loading works correctly.
ret = strategy_safe_wrapper(working_method, message="DeadBeef")(trade=trade)
assert isinstance(ret, Trade)
assert id(trade) != id(ret)
# Did not modify the original order
assert len(trade.orders) > 0
assert len(ret.orders) == 0
def test_hyperopt_parameters():
HyperoptStateContainer.set_state(HyperoptState.INDICATORS)
from skopt.space import Categorical, Integer, Real

View File

@@ -0,0 +1,68 @@
import pytest
from freqtrade.exceptions import StrategyError
from freqtrade.persistence import Trade
from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper
from tests.conftest import create_mock_trades, log_has_re
@pytest.mark.parametrize(
"error",
[
ValueError,
KeyError,
Exception,
],
)
def test_strategy_safe_wrapper_error(caplog, error):
def failing_method():
raise error("This is an error.")
with pytest.raises(StrategyError, match=r"This is an error."):
strategy_safe_wrapper(failing_method, message="DeadBeef")()
assert log_has_re(r"DeadBeef.*", caplog)
ret = strategy_safe_wrapper(failing_method, message="DeadBeef", default_retval=True)()
assert isinstance(ret, bool)
assert ret
caplog.clear()
# Test suppressing error
ret = strategy_safe_wrapper(failing_method, message="DeadBeef", supress_error=True)()
assert log_has_re(r"DeadBeef.*", caplog)
@pytest.mark.parametrize(
"value", [1, 22, 55, True, False, {"a": 1, "b": "112"}, [1, 2, 3, 4], (4, 2, 3, 6)]
)
def test_strategy_safe_wrapper(value):
def working_method(argumentpassedin):
return argumentpassedin
ret = strategy_safe_wrapper(working_method, message="DeadBeef")(value)
assert isinstance(ret, type(value))
assert ret == value
@pytest.mark.usefixtures("init_persistence")
def test_strategy_safe_wrapper_trade_copy(fee):
create_mock_trades(fee)
def working_method(trade):
assert len(trade.orders) > 0
assert trade.orders
trade.orders = []
assert len(trade.orders) == 0
return trade
trade = Trade.get_open_trades()[0]
# Don't assert anything before strategy_wrapper.
# This ensures that relationship loading works correctly.
ret = strategy_safe_wrapper(working_method, message="DeadBeef")(trade=trade)
assert isinstance(ret, Trade)
assert id(trade) != id(ret)
# Did not modify the original order
assert len(trade.orders) > 0
assert len(ret.orders) == 0