From 0320c8dc92d4e4acb4f61310f202ba6db2be840a Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 13 Mar 2021 15:46:20 +0100 Subject: [PATCH] Improve tests for trades_proxy --- freqtrade/persistence/models.py | 3 +-- tests/conftest_trades.py | 4 ++++ tests/test_persistence.py | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index 41a5a99ff..ed8a2259b 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -611,8 +611,7 @@ class LocalTrade(): else: # Not used during backtesting, but might be used by a strategy - sel_trades = [trade for trade in LocalTrade.trades + LocalTrade.trades_open - if trade.is_open == is_open] + sel_trades = [trade for trade in LocalTrade.trades + LocalTrade.trades_open] if pair: sel_trades = [trade for trade in sel_trades if trade.pair == pair] diff --git a/tests/conftest_trades.py b/tests/conftest_trades.py index 1d775830d..8e4be9165 100644 --- a/tests/conftest_trades.py +++ b/tests/conftest_trades.py @@ -29,6 +29,7 @@ def mock_trade_1(fee): fee_open=fee.return_value, fee_close=fee.return_value, is_open=True, + open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=17), open_rate=0.123, exchange='bittrex', open_order_id='dry_run_buy_12345', @@ -183,6 +184,7 @@ def mock_trade_4(fee): amount_requested=124.0, fee_open=fee.return_value, fee_close=fee.return_value, + open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=14), is_open=True, open_rate=0.123, exchange='bittrex', @@ -234,6 +236,7 @@ def mock_trade_5(fee): amount_requested=124.0, fee_open=fee.return_value, fee_close=fee.return_value, + open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=12), is_open=True, open_rate=0.123, exchange='bittrex', @@ -284,6 +287,7 @@ def mock_trade_6(fee): stake_amount=0.001, amount=2.0, amount_requested=2.0, + open_date=datetime.now(tz=timezone.utc) - timedelta(minutes=5), fee_open=fee.return_value, fee_close=fee.return_value, is_open=True, diff --git a/tests/test_persistence.py b/tests/test_persistence.py index 8c89c98ed..ab900cbb8 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -1,5 +1,6 @@ # pragma pylint: disable=missing-docstring, C0103 import logging +from datetime import datetime, timedelta, timezone from types import FunctionType from unittest.mock import MagicMock @@ -1044,6 +1045,7 @@ def test_fee_updated(fee): def test_total_open_trades_stakes(fee, use_db): Trade.use_db = use_db + Trade.reset_trades() res = Trade.total_open_trades_stakes() assert res == 0 create_mock_trades(fee, use_db) @@ -1053,6 +1055,26 @@ def test_total_open_trades_stakes(fee, use_db): Trade.use_db = True +@pytest.mark.usefixtures("init_persistence") +@pytest.mark.parametrize('use_db', [True, False]) +def test_get_trades_proxy(fee, use_db): + Trade.use_db = use_db + Trade.reset_trades() + create_mock_trades(fee, use_db) + trades = Trade.get_trades_proxy() + assert len(trades) == 6 + + assert isinstance(trades[0], Trade) + + assert len(Trade.get_trades_proxy(is_open=True)) == 4 + assert len(Trade.get_trades_proxy(is_open=False)) == 2 + opendate = datetime.now(tz=timezone.utc) - timedelta(minutes=15) + + assert len(Trade.get_trades_proxy(open_date=opendate)) == 3 + + Trade.use_db = True + + @pytest.mark.usefixtures("init_persistence") def test_get_overall_performance(fee):