From 910259036508b08eedb2721061187d90965319aa Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 14 Nov 2021 19:22:12 +0100 Subject: [PATCH] Improve tests by also having a "Mixed" case --- tests/conftest.py | 18 +++++---- tests/rpc/test_rpc_apiserver.py | 72 ++++++++++++++++++--------------- tests/test_freqtradebot.py | 2 +- 3 files changed, 51 insertions(+), 41 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 60d620639..0fa7daf59 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -248,33 +248,35 @@ def patch_get_signal( freqtrade.exchange.refresh_latest_ohlcv = lambda p: None -def create_mock_trades(fee, is_short: bool = False, use_db: bool = True): +def create_mock_trades(fee, is_short: Optional[bool] = False, use_db: bool = True): """ Create some fake trades ... + :param is_short: Optional bool, None creates a mix of long and short trades. """ def add_trade(trade): if use_db: Trade.query.session.add(trade) else: LocalTrade.add_bt_trade(trade) - + is_short1 = is_short if is_short is not None else True + is_short2 = is_short if is_short is not None else False # Simulate dry_run entries - trade = mock_trade_1(fee, is_short) + trade = mock_trade_1(fee, is_short1) add_trade(trade) - trade = mock_trade_2(fee, is_short) + trade = mock_trade_2(fee, is_short1) add_trade(trade) - trade = mock_trade_3(fee, is_short) + trade = mock_trade_3(fee, is_short2) add_trade(trade) - trade = mock_trade_4(fee, is_short) + trade = mock_trade_4(fee, is_short2) add_trade(trade) - trade = mock_trade_5(fee, is_short) + trade = mock_trade_5(fee, is_short2) add_trade(trade) - trade = mock_trade_6(fee, is_short) + trade = mock_trade_6(fee, is_short1) add_trade(trade) if use_db: diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 1d07ab6b8..597198458 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -699,7 +699,7 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets): assert rc.json() == {"error": "Error querying /api/v1/edge: Edge is not enabled."} -@pytest.mark.parametrize('is_short', [True, False]) +@pytest.mark.parametrize('is_short', [True, False, None]) def test_api_profit(botclient, mocker, ticker, fee, markets, is_short): ftbot, client = botclient patch_get_signal(ftbot) @@ -721,37 +721,44 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, is_short): rc = client_get(client, f"{BASE_URI}/profit") assert_response(rc) # raise ValueError(rc.json()) - assert rc.json() == {'avg_duration': ANY, - 'best_pair': 'ETC/BTC' if is_short else 'XRP/BTC', - 'best_rate': -0.5 if is_short else 1.0, - 'first_trade_date': ANY, - 'first_trade_timestamp': ANY, - 'latest_trade_date': '5 minutes ago', - 'latest_trade_timestamp': ANY, - 'profit_all_coin': 43.61269123 if is_short else -44.0631579, - 'profit_all_fiat': 538398.67323435 if is_short else -543959.6842755, - 'profit_all_percent_mean': 66.41 if is_short else -66.41, - 'profit_all_ratio_mean': 0.664109545 if is_short else -0.6641100666666667, - 'profit_all_percent_sum': 398.47 if is_short else -398.47, - 'profit_all_ratio_sum': 3.98465727 if is_short else -3.9846604, - 'profit_all_percent': 4.36 if is_short else -4.41, - 'profit_all_ratio': 0.043612222872799825 if is_short - else -0.044063014216106644, - 'profit_closed_coin': -0.00673913 if is_short else 0.00073913, - 'profit_closed_fiat': -83.19455985 if is_short else 9.124559849999999, - 'profit_closed_ratio_mean': -0.0075 if is_short else 0.0075, - 'profit_closed_percent_mean': -0.75 if is_short else 0.75, - 'profit_closed_ratio_sum': -0.015 if is_short else 0.015, - 'profit_closed_percent_sum': -1.5 if is_short else 1.5, - 'profit_closed_ratio': -6.739057628404269e-06 if is_short - else 7.391275897987988e-07, - 'profit_closed_percent': -0.0 if is_short else 0.0, - 'trade_count': 6, - 'closed_trade_count': 2, - 'short_trades': 6 if is_short else 0, - 'winning_trades': 0 if is_short else 2, - 'losing_trades': 2 if is_short else 0, - } + assert rc.json() == { + 'avg_duration': ANY, + 'best_pair': 'ETC/BTC' if is_short else 'XRP/BTC', + 'best_rate': -0.5 if is_short else 1.0, + 'first_trade_date': ANY, + 'first_trade_timestamp': ANY, + 'latest_trade_date': '5 minutes ago', + 'latest_trade_timestamp': ANY, + 'profit_all_coin': 43.61269123 if is_short else -14.43790415 + if is_short is None else -44.0631579, + 'profit_all_fiat': 538398.67323435 if is_short else -178235.92673175 + if is_short is None else -543959.6842755, + 'profit_all_percent_mean': 66.41 if is_short else 0.08 if is_short is None else -66.41, + 'profit_all_ratio_mean': 0.664109545 if is_short else 0.000835751666666662 + if is_short is None else -0.6641100666666667, + 'profit_all_percent_sum': 398.47 if is_short else 0.5 if is_short is None else -398.47, + 'profit_all_ratio_sum': 3.98465727 if is_short else 0.005014509999999972 + if is_short is None else -3.9846604, + 'profit_all_percent': 4.36 if is_short else -1.44 if is_short is None else -4.41, + 'profit_all_ratio': 0.043612222872799825 if is_short else -0.014437768014451796 + if is_short is None else -0.044063014216106644, + 'profit_closed_coin': -0.00673913 if is_short else -0.00542913 + if is_short is None else 0.00073913, + 'profit_closed_fiat': -83.19455985 if is_short else -67.02260985 + if is_short is None else 9.124559849999999, + 'profit_closed_ratio_mean': -0.0075 if is_short else 0.0025 if is_short is None else 0.0075, + 'profit_closed_percent_mean': -0.75 if is_short else 0.25 if is_short is None else 0.75, + 'profit_closed_ratio_sum': -0.015 if is_short else 0.005 if is_short is None else 0.015, + 'profit_closed_percent_sum': -1.5 if is_short else 0.5 if is_short is None else 1.5, + 'profit_closed_ratio': -6.739057628404269e-06 if is_short + else -5.429078808526421e-06 if is_short is None else 7.391275897987988e-07, + 'profit_closed_percent': -0.0 if is_short else -0.0 if is_short is None else 0.0, + 'trade_count': 6, + 'closed_trade_count': 2, + 'short_trades': 6 if is_short else 3 if is_short is None else 0, + 'winning_trades': 0 if is_short else 1 if is_short is None else 2, + 'losing_trades': 2 if is_short else 1 if is_short is None else 0, + } @pytest.mark.parametrize('is_short', [True, False]) @@ -976,6 +983,7 @@ def test_api_whitelist(botclient): "method": ["StaticPairList"] } + @pytest.mark.parametrize('is_short', [True, False]) def test_api_forcebuy(botclient, mocker, fee, is_short): ftbot, client = botclient diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index e5a6195f3..019fa42c7 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -4317,7 +4317,7 @@ def test_sync_wallet_dry_run(mocker, default_conf_usdt, ticker_usdt, fee, limit_ @pytest.mark.usefixtures("init_persistence") @pytest.mark.parametrize("is_short,buy_calls,sell_calls", [ (False, 1, 2), - (True, 2, 1), + (True, 1, 2), ]) def test_cancel_all_open_orders(mocker, default_conf_usdt, fee, limit_order, limit_order_open, is_short, buy_calls, sell_calls):