From 98840eef3cd9d7f1008b95f4dad8b36a9d3ab66b Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 9 Sep 2020 07:01:43 +0200 Subject: [PATCH] Add 5th mock trade --- tests/commands/test_commands.py | 2 +- tests/conftest.py | 5 +++- tests/conftest_trades.py | 51 +++++++++++++++++++++++++++++++++ tests/data/test_btanalysis.py | 2 +- tests/test_freqtradebot.py | 13 +++++---- tests/test_persistence.py | 4 +-- 6 files changed, 66 insertions(+), 11 deletions(-) diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 26875fb7f..290a3fa77 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -1116,7 +1116,7 @@ def test_show_trades(mocker, fee, capsys, caplog): pargs = get_args(args) pargs['config'] = None start_show_trades(pargs) - assert log_has("Printing 4 Trades: ", caplog) + assert log_has("Printing 5 Trades: ", caplog) captured = capsys.readouterr() assert "Trade(id=1" in captured.out assert "Trade(id=2" in captured.out diff --git a/tests/conftest.py b/tests/conftest.py index a63b4e314..1ef495301 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -23,7 +23,7 @@ from freqtrade.persistence import Trade from freqtrade.resolvers import ExchangeResolver from freqtrade.worker import Worker from tests.conftest_trades import (mock_trade_1, mock_trade_2, mock_trade_3, - mock_trade_4) + mock_trade_4, mock_trade_5) logging.getLogger('').setLevel(logging.INFO) @@ -186,6 +186,9 @@ def create_mock_trades(fee): trade = mock_trade_4(fee) Trade.session.add(trade) + trade = mock_trade_5(fee) + Trade.session.add(trade) + @pytest.fixture(autouse=True) def patch_coingekko(mocker) -> None: diff --git a/tests/conftest_trades.py b/tests/conftest_trades.py index c990f6cdc..d3e19603b 100644 --- a/tests/conftest_trades.py +++ b/tests/conftest_trades.py @@ -172,3 +172,54 @@ def mock_trade_4(fee): o = Order.parse_from_ccxt_object(mock_order_4(), 'ETH/BTC', 'buy') trade.orders.append(o) return trade + + +def mock_order_5(): + return { + 'id': 'prod_buy_3455', + 'symbol': 'XRP/BTC', + 'status': 'closed', + 'side': 'buy', + 'type': 'limit', + 'price': 0.123, + 'amount': 123.0, + 'filled': 123.0, + 'remaining': 0.0, + } + + +def mock_order_5_stoploss(): + return { + 'id': 'prod_stoploss_3455', + 'symbol': 'XRP/BTC', + 'status': 'open', + 'side': 'sell', + 'type': 'stop_loss_limit', + 'price': 0.123, + 'amount': 123.0, + 'filled': 0.0, + 'remaining': 123.0, + } + + +def mock_trade_5(fee): + """ + Simulate prod entry with stoploss + """ + trade = Trade( + pair='XRP/BTC', + stake_amount=0.001, + amount=123.0, + amount_requested=124.0, + fee_open=fee.return_value, + fee_close=fee.return_value, + open_rate=0.123, + exchange='bittrex', + strategy='SampleStrategy', + stoploss_order_id='prod_stoploss_3455' + ) + o = Order.parse_from_ccxt_object(mock_order_5(), 'ETH/BTC', 'buy') + trade.orders.append(o) + o = Order.parse_from_ccxt_object(mock_order_5_stoploss(), 'ETH/BTC', 'sell') + trade.orders.append(o) + return trade diff --git a/tests/data/test_btanalysis.py b/tests/data/test_btanalysis.py index e2ca66bd8..3b5672e65 100644 --- a/tests/data/test_btanalysis.py +++ b/tests/data/test_btanalysis.py @@ -110,7 +110,7 @@ def test_load_trades_from_db(default_conf, fee, mocker): trades = load_trades_from_db(db_url=default_conf['db_url']) assert init_mock.call_count == 1 - assert len(trades) == 4 + assert len(trades) == 5 assert isinstance(trades, DataFrame) assert "pair" in trades.columns assert "open_date" in trades.columns diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index c95a085a6..883c63737 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -4213,7 +4213,7 @@ def test_cancel_all_open_orders(mocker, default_conf, fee, limit_buy_order, limi freqtrade = get_patched_freqtradebot(mocker, default_conf) create_mock_trades(fee) trades = Trade.query.all() - assert len(trades) == 4 + assert len(trades) == 5 freqtrade.cancel_all_open_orders() assert buy_mock.call_count == 1 assert sell_mock.call_count == 1 @@ -4244,14 +4244,15 @@ def test_update_open_orders(mocker, default_conf, fee, caplog): assert log_has_re(r"Error updating Order .*", caplog) caplog.clear() - assert len(Order.get_open_orders()) == 1 + assert len(Order.get_open_orders()) == 2 matching_buy_order = mock_order_4() matching_buy_order.update({ 'status': 'closed', }) mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=matching_buy_order) freqtrade.update_open_orders() - assert len(Order.get_open_orders()) == 0 + # Only stoploss order is kept open + assert len(Order.get_open_orders()) == 1 @pytest.mark.usefixtures("init_persistence") @@ -4276,7 +4277,7 @@ def test_update_closed_trades_without_assigned_fees(mocker, default_conf, fee): create_mock_trades(fee) trades = Trade.get_trades().all() - assert len(trades) == 4 + assert len(trades) == 5 for trade in trades: assert trade.fee_open_cost is None assert trade.fee_open_currency is None @@ -4285,8 +4286,8 @@ def test_update_closed_trades_without_assigned_fees(mocker, default_conf, fee): freqtrade.update_closed_trades_without_assigned_fees() - # trades = Trade.get_trades().all() - assert len(trades) == 4 + trades = Trade.get_trades().all() + assert len(trades) == 5 for trade in trades: if trade.is_open: diff --git a/tests/test_persistence.py b/tests/test_persistence.py index 788debace..48b918128 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -734,7 +734,7 @@ def test_adjust_min_max_rates(fee): def test_get_open(default_conf, fee): create_mock_trades(fee) - assert len(Trade.get_open_trades()) == 2 + assert len(Trade.get_open_trades()) == 3 @pytest.mark.usefixtures("init_persistence") @@ -1004,7 +1004,7 @@ def test_total_open_trades_stakes(fee): assert res == 0 create_mock_trades(fee) res = Trade.total_open_trades_stakes() - assert res == 0.002 + assert res == 0.003 @pytest.mark.usefixtures("init_persistence")