Refactor test to not duplicate order info

This commit is contained in:
Matthias 2020-09-07 06:49:30 +02:00
parent da0ceb7d87
commit f113b45036
3 changed files with 25 additions and 36 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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