Update several tests to remove .query

This commit is contained in:
Matthias 2023-03-16 07:04:15 +01:00
parent e579ff9532
commit 6ed337faa3
5 changed files with 22 additions and 22 deletions

View File

@ -384,13 +384,13 @@ class RPC:
""" Returns the X last trades """ """ Returns the X last trades """
order_by: Any = Trade.id if order_by_id else Trade.close_date.desc() order_by: Any = Trade.id if order_by_id else Trade.close_date.desc()
if limit: if limit:
trades = Trade.session.execute( trades = Trade.session.scalars(
Trade.get_trades_query([Trade.is_open.is_(False)]) Trade.get_trades_query([Trade.is_open.is_(False)])
.order_by(order_by) .order_by(order_by)
.limit(limit) .limit(limit)
.offset(offset)) .offset(offset))
else: else:
trades = Trade.session.execute( trades = Trade.session.scalars(
Trade.get_trades_query([Trade.is_open.is_(False)]) Trade.get_trades_query([Trade.is_open.is_(False)])
.order_by(Trade.close_date.desc())) .order_by(Trade.close_date.desc()))

View File

@ -299,7 +299,7 @@ def create_mock_trades(fee, is_short: Optional[bool] = False, use_db: bool = Tru
""" """
def add_trade(trade): def add_trade(trade):
if use_db: if use_db:
Trade.query.session.add(trade) Trade.session.add(trade)
else: else:
LocalTrade.add_bt_trade(trade) LocalTrade.add_bt_trade(trade)
is_short1 = is_short if is_short is not None else True is_short1 = is_short if is_short is not None else True
@ -332,11 +332,11 @@ def create_mock_trades_with_leverage(fee, use_db: bool = True):
Create some fake trades ... Create some fake trades ...
""" """
if use_db: if use_db:
Trade.query.session.rollback() Trade.session.rollback()
def add_trade(trade): def add_trade(trade):
if use_db: if use_db:
Trade.query.session.add(trade) Trade.session.add(trade)
else: else:
LocalTrade.add_bt_trade(trade) LocalTrade.add_bt_trade(trade)
@ -366,7 +366,7 @@ def create_mock_trades_with_leverage(fee, use_db: bool = True):
add_trade(trade) add_trade(trade)
if use_db: if use_db:
Trade.query.session.flush() Trade.session.flush()
def create_mock_trades_usdt(fee, is_short: Optional[bool] = False, use_db: bool = True): def create_mock_trades_usdt(fee, is_short: Optional[bool] = False, use_db: bool = True):
@ -375,7 +375,7 @@ def create_mock_trades_usdt(fee, is_short: Optional[bool] = False, use_db: bool
""" """
def add_trade(trade): def add_trade(trade):
if use_db: if use_db:
Trade.query.session.add(trade) Trade.session.add(trade)
else: else:
LocalTrade.add_bt_trade(trade) LocalTrade.add_bt_trade(trade)

View File

@ -21,8 +21,8 @@ spot, margin, futures = TradingMode.SPOT, TradingMode.MARGIN, TradingMode.FUTURE
def test_init_create_session(default_conf): def test_init_create_session(default_conf):
# Check if init create a session # Check if init create a session
init_db(default_conf['db_url']) init_db(default_conf['db_url'])
assert hasattr(Trade, '_session') assert hasattr(Trade, 'session')
assert 'scoped_session' in type(Trade._session).__name__ assert 'scoped_session' in type(Trade.session).__name__
def test_init_custom_db_url(default_conf, tmpdir): def test_init_custom_db_url(default_conf, tmpdir):
@ -34,7 +34,7 @@ def test_init_custom_db_url(default_conf, tmpdir):
init_db(default_conf['db_url']) init_db(default_conf['db_url'])
assert Path(filename).is_file() assert Path(filename).is_file()
r = Trade._session.execute(text("PRAGMA journal_mode")) r = Trade.session.execute(text("PRAGMA journal_mode"))
assert r.first() == ('wal',) assert r.first() == ('wal',)

View File

@ -1494,7 +1494,7 @@ def test_stoploss_reinitialization(default_conf, fee):
assert trade.stop_loss_pct == -0.05 assert trade.stop_loss_pct == -0.05
assert trade.initial_stop_loss == 0.95 assert trade.initial_stop_loss == 0.95
assert trade.initial_stop_loss_pct == -0.05 assert trade.initial_stop_loss_pct == -0.05
Trade.query.session.add(trade) Trade.session.add(trade)
Trade.commit() Trade.commit()
# Lower stoploss # Lower stoploss
@ -1556,7 +1556,7 @@ def test_stoploss_reinitialization_leverage(default_conf, fee):
assert trade.stop_loss_pct == -0.1 assert trade.stop_loss_pct == -0.1
assert trade.initial_stop_loss == 0.98 assert trade.initial_stop_loss == 0.98
assert trade.initial_stop_loss_pct == -0.1 assert trade.initial_stop_loss_pct == -0.1
Trade.query.session.add(trade) Trade.session.add(trade)
Trade.commit() Trade.commit()
# Lower stoploss # Lower stoploss
@ -1618,7 +1618,7 @@ def test_stoploss_reinitialization_short(default_conf, fee):
assert trade.stop_loss_pct == -0.1 assert trade.stop_loss_pct == -0.1
assert trade.initial_stop_loss == 1.02 assert trade.initial_stop_loss == 1.02
assert trade.initial_stop_loss_pct == -0.1 assert trade.initial_stop_loss_pct == -0.1
Trade.query.session.add(trade) Trade.session.add(trade)
Trade.commit() Trade.commit()
# Lower stoploss # Lower stoploss
Trade.stoploss_reinitialization(-0.15) Trade.stoploss_reinitialization(-0.15)
@ -2443,8 +2443,8 @@ def test_order_to_ccxt(limit_buy_order_open):
order = Order.parse_from_ccxt_object(limit_buy_order_open, 'mocked', 'buy') order = Order.parse_from_ccxt_object(limit_buy_order_open, 'mocked', 'buy')
order.ft_trade_id = 1 order.ft_trade_id = 1
order.query.session.add(order) order.session.add(order)
Order.query.session.commit() Order.session.commit()
order_resp = Order.order_by_id(limit_buy_order_open['id']) order_resp = Order.order_by_id(limit_buy_order_open['id'])
assert order_resp assert order_resp
@ -2546,7 +2546,7 @@ def test_recalc_trade_from_orders_dca(data) -> None:
leverage=1.0, leverage=1.0,
trading_mode=TradingMode.SPOT trading_mode=TradingMode.SPOT
) )
Trade.query.session.add(trade) Trade.session.add(trade)
for idx, (order, result) in enumerate(data['orders']): for idx, (order, result) in enumerate(data['orders']):
amount = order[1] amount = order[1]

View File

@ -652,7 +652,7 @@ def test_api_trade_single(botclient, mocker, fee, ticker, markets, is_short):
assert_response(rc, 404) assert_response(rc, 404)
assert rc.json()['detail'] == 'Trade not found.' assert rc.json()['detail'] == 'Trade not found.'
Trade.query.session.rollback() Trade.rollback()
create_mock_trades(fee, is_short=is_short) create_mock_trades(fee, is_short=is_short)
rc = client_get(client, f"{BASE_URI}/trade/3") rc = client_get(client, f"{BASE_URI}/trade/3")
@ -943,7 +943,7 @@ def test_api_performance(botclient, fee):
) )
trade.close_profit = trade.calc_profit_ratio(trade.close_rate) trade.close_profit = trade.calc_profit_ratio(trade.close_rate)
trade.close_profit_abs = trade.calc_profit(trade.close_rate) trade.close_profit_abs = trade.calc_profit(trade.close_rate)
Trade.query.session.add(trade) Trade.session.add(trade)
trade = Trade( trade = Trade(
pair='XRP/ETH', pair='XRP/ETH',
@ -960,7 +960,7 @@ def test_api_performance(botclient, fee):
trade.close_profit = trade.calc_profit_ratio(trade.close_rate) trade.close_profit = trade.calc_profit_ratio(trade.close_rate)
trade.close_profit_abs = trade.calc_profit(trade.close_rate) trade.close_profit_abs = trade.calc_profit(trade.close_rate)
Trade.query.session.add(trade) Trade.session.add(trade)
Trade.commit() Trade.commit()
rc = client_get(client, f"{BASE_URI}/performance") rc = client_get(client, f"{BASE_URI}/performance")
@ -1290,7 +1290,7 @@ def test_api_forceexit(botclient, mocker, ticker, fee, markets):
data={"tradeid": "1"}) data={"tradeid": "1"})
assert_response(rc, 502) assert_response(rc, 502)
assert rc.json() == {"error": "Error querying /api/v1/forceexit: invalid argument"} assert rc.json() == {"error": "Error querying /api/v1/forceexit: invalid argument"}
Trade.query.session.rollback() Trade.rollback()
create_mock_trades(fee) create_mock_trades(fee)
trade = Trade.get_trades([Trade.id == 5]).first() trade = Trade.get_trades([Trade.id == 5]).first()
@ -1299,7 +1299,7 @@ def test_api_forceexit(botclient, mocker, ticker, fee, markets):
data={"tradeid": "5", "ordertype": "market", "amount": 23}) data={"tradeid": "5", "ordertype": "market", "amount": 23})
assert_response(rc) assert_response(rc)
assert rc.json() == {'result': 'Created sell order for trade 5.'} assert rc.json() == {'result': 'Created sell order for trade 5.'}
Trade.query.session.rollback() Trade.rollback()
trade = Trade.get_trades([Trade.id == 5]).first() trade = Trade.get_trades([Trade.id == 5]).first()
assert pytest.approx(trade.amount) == 100 assert pytest.approx(trade.amount) == 100
@ -1309,7 +1309,7 @@ def test_api_forceexit(botclient, mocker, ticker, fee, markets):
data={"tradeid": "5"}) data={"tradeid": "5"})
assert_response(rc) assert_response(rc)
assert rc.json() == {'result': 'Created sell order for trade 5.'} assert rc.json() == {'result': 'Created sell order for trade 5.'}
Trade.query.session.rollback() Trade.rollback()
trade = Trade.get_trades([Trade.id == 5]).first() trade = Trade.get_trades([Trade.id == 5]).first()
assert trade.is_open is False assert trade.is_open is False