308d36f2b6
paper_accounts(含owner_id/checkpoint_date/scheduler_job_id/match_session) paper_trades(rejected/reject_reason/blocked_by) paper_positions(scope) paper_daily_balance(is_checkpoint) WAL多进程读写 5 tests passed.
62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
"""persistence 4 表 round-trip 测试(spec §8)。"""
|
|
from sanguo_trader.persistence import (
|
|
init_db, save_account, save_trade, save_daily_balance,
|
|
update_account_status, update_checkpoint, load_checkpoint,
|
|
list_trades, list_daily_balance,
|
|
)
|
|
|
|
|
|
def test_init_and_save_account(tmp_path):
|
|
db = str(tmp_path / "p.db")
|
|
init_db(db)
|
|
aid = save_account(db, {"name": "test", "mode": "replay", "initial_capital": 1_000_000})
|
|
assert aid > 0
|
|
|
|
|
|
def test_save_trade_and_list(tmp_path):
|
|
db = str(tmp_path / "p.db")
|
|
init_db(db)
|
|
aid = save_account(db, {"name": "t"})
|
|
save_trade(db, aid, {"strategy_id": "s1", "symbol": "600000",
|
|
"price": 10.0, "volume": 100, "commission": 5.0})
|
|
trades = list_trades(db, aid)
|
|
assert len(trades) == 1
|
|
assert trades[0]["price"] == 10.0
|
|
assert trades[0]["rejected"] == 0
|
|
|
|
|
|
def test_save_reject_trade(tmp_path):
|
|
db = str(tmp_path / "p.db")
|
|
init_db(db)
|
|
aid = save_account(db, {"name": "t"})
|
|
save_trade(db, aid, {"symbol": "300750"}, rejected=True,
|
|
reject_reason="limit_up_locked")
|
|
trades = list_trades(db, aid)
|
|
assert trades[0]["rejected"] == 1
|
|
assert trades[0]["reject_reason"] == "limit_up_locked"
|
|
|
|
|
|
def test_daily_balance_and_checkpoint(tmp_path):
|
|
db = str(tmp_path / "p.db")
|
|
init_db(db)
|
|
aid = save_account(db, {"name": "t"})
|
|
save_daily_balance(db, aid, "2024-01-01", 999_000, 1000, 1_000_000,
|
|
per_strategy_pnl={"s1": 100}, is_checkpoint=True)
|
|
save_daily_balance(db, aid, "2024-01-02", 998_000, 1100, 999_000,
|
|
is_checkpoint=False)
|
|
update_checkpoint(db, aid, "2024-01-01")
|
|
assert load_checkpoint(db, aid) == "2024-01-01"
|
|
balances = list_daily_balance(db, aid)
|
|
assert len(balances) == 2
|
|
assert balances[0]["is_checkpoint"] == 1
|
|
|
|
|
|
def test_update_account_status(tmp_path):
|
|
db = str(tmp_path / "p.db")
|
|
init_db(db)
|
|
aid = save_account(db, {"name": "t", "status": "pending"})
|
|
update_account_status(db, aid, "done")
|
|
with __import__("sqlite3").connect(db) as conn:
|
|
row = conn.execute("SELECT status FROM paper_accounts WHERE id=?", (aid,)).fetchone()
|
|
assert row[0] == "done"
|