69 lines
3.4 KiB
Python
69 lines
3.4 KiB
Python
import json
|
|
|
|
from app.db.strategy_sample_cleanup import CONFIRM_TOKEN, cleanup_legacy_strategy_samples
|
|
|
|
|
|
def test_cleanup_legacy_strategy_samples_removes_only_retired_strategy_data(pg_conn):
|
|
pg_conn.execute(
|
|
"""
|
|
INSERT INTO recommendation (
|
|
id, symbol, rec_time, rec_state, rec_score, entry_price,
|
|
status, execution_status, action_status, display_bucket, entry_triggered,
|
|
strategy_code, entry_plan_json
|
|
) VALUES
|
|
(9001, 'OLD/USDT', '2026-06-04T00:00:00', '爆发', 50, 1,
|
|
'active', 'buy_now', '可即刻买入', 'realtime', 1, 'volume_ignition_1h_v1', '{}'),
|
|
(9002, 'NEW/USDT', '2026-06-04T00:00:00', '爆发', 50, 1,
|
|
'active', 'buy_now', '可即刻买入', 'realtime', 1, 'long_momentum_breakout_15m_1h_v1', '{}')
|
|
"""
|
|
)
|
|
pg_conn.execute(
|
|
"""
|
|
INSERT INTO strategy_signals (
|
|
id, strategy_code, symbol, direction, signal_status, created_at
|
|
) VALUES
|
|
(9101, 'volume_ignition_1h_v1', 'OLD/USDT', 'long', 'candidate', '2026-06-04T00:00:00'),
|
|
(9102, 'long_momentum_breakout_15m_1h_v1', 'NEW/USDT', 'long', 'candidate', '2026-06-04T00:00:00')
|
|
"""
|
|
)
|
|
pg_conn.execute(
|
|
"""
|
|
INSERT INTO paper_orders (
|
|
id, recommendation_id, symbol, side, order_type, status, target_price,
|
|
current_price_at_create, notional_usdt, strategy_code, created_at, updated_at, expires_at
|
|
) VALUES
|
|
(9201, 9001, 'OLD/USDT', 'long', 'limit', 'pending', 1, 1, 5000, 'volume_ignition_1h_v1', '2026-06-04T00:00:00', '2026-06-04T00:00:00', '2026-06-05T00:00:00'),
|
|
(9202, 9002, 'NEW/USDT', 'long', 'limit', 'pending', 1, 1, 5000, 'long_momentum_breakout_15m_1h_v1', '2026-06-04T00:00:00', '2026-06-04T00:00:00', '2026-06-05T00:00:00')
|
|
"""
|
|
)
|
|
pg_conn.execute(
|
|
"""
|
|
INSERT INTO paper_trades (
|
|
id, recommendation_id, symbol, side, status, opened_at, entry_price,
|
|
qty, notional_usdt, strategy_code, created_at, updated_at
|
|
) VALUES
|
|
(9301, 9001, 'OLD/USDT', 'long', 'open', '2026-06-04T00:00:00', 1, 1, 5000, 'volume_ignition_1h_v1', '2026-06-04T00:00:00', '2026-06-04T00:00:00'),
|
|
(9302, 9002, 'NEW/USDT', 'long', 'open', '2026-06-04T00:00:00', 1, 1, 5000, 'long_momentum_breakout_15m_1h_v1', '2026-06-04T00:00:00', '2026-06-04T00:00:00')
|
|
"""
|
|
)
|
|
pg_conn.execute(
|
|
"""
|
|
INSERT INTO paper_trade_events (
|
|
id, trade_id, recommendation_id, symbol, event_type, event_time, detail_json, strategy_code
|
|
) VALUES
|
|
(9401, 9301, 9001, 'OLD/USDT', 'open', '2026-06-04T00:00:00', '{}', 'volume_ignition_1h_v1'),
|
|
(9402, 9302, 9002, 'NEW/USDT', 'open', '2026-06-04T00:00:00', '{}', 'long_momentum_breakout_15m_1h_v1')
|
|
"""
|
|
)
|
|
pg_conn.commit()
|
|
|
|
result = cleanup_legacy_strategy_samples(confirm=CONFIRM_TOKEN, create_backup=False)
|
|
|
|
assert result["deleted"]["recommendation"] == 1
|
|
assert result["deleted"]["strategy_signals"] == 1
|
|
assert result["deleted"]["paper_orders"] == 1
|
|
assert result["deleted"]["paper_trades"] == 1
|
|
assert result["deleted"]["paper_trade_events"] >= 1
|
|
assert pg_conn.execute("SELECT COUNT(*) FROM recommendation WHERE symbol='OLD/USDT'").fetchone()[0] == 0
|
|
assert pg_conn.execute("SELECT COUNT(*) FROM recommendation WHERE symbol='NEW/USDT'").fetchone()[0] == 1
|