From f113b45036c39d216c6cfdca7702d816901b4f3b Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 7 Sep 2020 06:49:30 +0200 Subject: [PATCH] Refactor test to not duplicate order info --- tests/conftest.py | 23 ++++++++++++++++++----- tests/conftest_trades.py | 18 ------------------ tests/test_freqtradebot.py | 20 +++++++------------- 3 files changed, 25 insertions(+), 36 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b966cde5b..a63b4e314 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,4 @@ # pragma pylint: disable=missing-docstring -from tests.conftest_trades import create_mock_trades -from freqtrade.persistence.models import Order import json import logging import re @@ -24,6 +22,8 @@ from freqtrade.freqtradebot import FreqtradeBot 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) logging.getLogger('').setLevel(logging.INFO) @@ -169,9 +169,22 @@ def patch_get_signal(freqtrade: FreqtradeBot, value=(True, False)) -> None: freqtrade.exchange.refresh_latest_ohlcv = lambda p: None -@pytest.fixture(scope='function') -def mock_trades(init_persistence, fee): - return create_mock_trades(fee) +def create_mock_trades(fee): + """ + Create some fake trades ... + """ + # Simulate dry_run entries + trade = mock_trade_1(fee) + Trade.session.add(trade) + + trade = mock_trade_2(fee) + Trade.session.add(trade) + + trade = mock_trade_3(fee) + Trade.session.add(trade) + + trade = mock_trade_4(fee) + Trade.session.add(trade) @pytest.fixture(autouse=True) diff --git a/tests/conftest_trades.py b/tests/conftest_trades.py index b2aff2ee7..1990725ee 100644 --- a/tests/conftest_trades.py +++ b/tests/conftest_trades.py @@ -173,21 +173,3 @@ def mock_trade_4(fee): o = Order.parse_from_ccxt_object(mock_order_4(), 'ETH/BTC', 'buy') trade.orders.append(o) return trade - - -def create_mock_trades(fee): - """ - Create some fake trades ... - """ - # Simulate dry_run entries - trade = mock_trade_1(fee) - Trade.session.add(trade) - - trade = mock_trade_2(fee) - Trade.session.add(trade) - - trade = mock_trade_3(fee) - Trade.session.add(trade) - - trade = mock_trade_4(fee) - Trade.session.add(trade) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 0f340ae8f..0d2e70f1e 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -1,7 +1,6 @@ # pragma pylint: disable=missing-docstring, C0103 # pragma pylint: disable=protected-access, too-many-lines, invalid-name, too-many-arguments -from freqtrade.persistence.models import Order import logging import time from copy import deepcopy @@ -13,11 +12,13 @@ import pytest from freqtrade.constants import (CANCEL_REASON, MATH_CLOSE_PREC, UNLIMITED_STAKE_AMOUNT) -from freqtrade.exceptions import (DependencyException, ExchangeError, InsufficientFundsError, +from freqtrade.exceptions import (DependencyException, ExchangeError, + InsufficientFundsError, InvalidOrderException, OperationalException, PricingError, TemporaryError) from freqtrade.freqtradebot import FreqtradeBot from freqtrade.persistence import Trade +from freqtrade.persistence.models import Order from freqtrade.rpc import RPCMessageType from freqtrade.state import RunMode, State from freqtrade.strategy.interface import SellCheckTuple, SellType @@ -26,6 +27,7 @@ from tests.conftest import (create_mock_trades, get_patched_freqtradebot, get_patched_worker, log_has, log_has_re, patch_edge, patch_exchange, patch_get_signal, patch_wallet, patch_whitelist) +from tests.conftest_trades import mock_order_4 def patch_RPCManager(mocker) -> MagicMock: @@ -4241,18 +4243,10 @@ def test_update_open_orders(mocker, default_conf, fee, caplog): caplog.clear() assert len(Order.get_open_orders()) == 1 - - matching_buy_order = { - 'id': 'prod_buy_12345', - 'symbol': 'ETC/BTC', + matching_buy_order = mock_order_4() + matching_buy_order.update({ 'status': 'closed', - 'side': 'buy', - 'type': 'limit', - 'price': 0.123, - 'amount': 123.0, - 'filled': 123.0, - 'remaining': 0.0, - } + }) mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=matching_buy_order) freqtrade.update_open_orders() assert len(Order.get_open_orders()) == 0