fix: validate historical trade chain changes

This commit is contained in:
王鹏
2026-07-08 17:36:37 +08:00
parent 6e73e90f99
commit e6794d6e33
2 changed files with 66 additions and 3 deletions

View File

@@ -129,3 +129,51 @@ def test_service_validates_lot_size_and_available_sell_quantity(tmp_path):
)
assert saved_sell.id is not None
def test_service_rejects_historical_changes_that_break_future_sells(tmp_path):
service = TradingService(tmp_path / "grid.db")
service.ensure_defaults()
account = service.get_active_account()
instrument = service.add_instrument(Instrument(id=None, code="159915", name="创业板ETF"))
buy = service.save_trade(
Trade(
id=None,
account_id=account.id,
instrument_id=instrument.id,
trade_date=date(2026, 7, 7),
side=TradeSide.BUY,
price=Decimal("2.00"),
quantity=200,
trade_group=TradeGroup.GRID,
)
)
service.save_trade(
Trade(
id=None,
account_id=account.id,
instrument_id=instrument.id,
trade_date=date(2026, 7, 8),
side=TradeSide.SELL,
price=Decimal("2.10"),
quantity=200,
trade_group=TradeGroup.GRID,
)
)
with pytest.raises(ValueError, match="后续成交"):
service.update_trade(
Trade(
id=buy.id,
account_id=account.id,
instrument_id=instrument.id,
trade_date=date(2026, 7, 7),
side=TradeSide.BUY,
price=Decimal("2.00"),
quantity=100,
trade_group=TradeGroup.GRID,
)
)
with pytest.raises(ValueError, match="后续成交"):
service.delete_trade(buy.id)