feat: add sqlite persistence
This commit is contained in:
105
tests/test_repositories.py
Normal file
105
tests/test_repositories.py
Normal file
@@ -0,0 +1,105 @@
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
|
||||
from grid_trading.domain.models import Account, Instrument, StrategyTemplate, Trade, TradeGroup, TradeSide
|
||||
from grid_trading.storage.repositories import Repository
|
||||
|
||||
|
||||
def test_database_round_trips_account_instrument_template_and_trade(tmp_path):
|
||||
db_path = tmp_path / "grid.db"
|
||||
repo = Repository(db_path)
|
||||
repo.initialize()
|
||||
|
||||
account = repo.save_account(Account(id=None, name="主账户", initial_cash=Decimal("100000"), notes="first"))
|
||||
template = repo.save_strategy_template(
|
||||
StrategyTemplate(
|
||||
id=None,
|
||||
name="默认模板",
|
||||
grid_spacing_pct=Decimal("0.03"),
|
||||
amount_per_grid=Decimal("5000"),
|
||||
base_target_amount=Decimal("20000"),
|
||||
max_position_amount=Decimal("80000"),
|
||||
min_lot=100,
|
||||
is_default=True,
|
||||
)
|
||||
)
|
||||
instrument = repo.save_instrument(
|
||||
Instrument(
|
||||
id=None,
|
||||
code="510300",
|
||||
name="沪深300ETF",
|
||||
market="ETF",
|
||||
lot_size=100,
|
||||
manual_price=Decimal("3.95"),
|
||||
)
|
||||
)
|
||||
trade = repo.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date(2026, 7, 8),
|
||||
side=TradeSide.BUY,
|
||||
price=Decimal("3.90"),
|
||||
quantity=1000,
|
||||
commission=Decimal("5"),
|
||||
transfer_fee=Decimal("0.04"),
|
||||
trade_group=TradeGroup.GRID,
|
||||
notes="first buy",
|
||||
)
|
||||
)
|
||||
repo.close()
|
||||
|
||||
reopened = Repository(db_path)
|
||||
reopened.initialize()
|
||||
|
||||
assert reopened.list_accounts() == [account]
|
||||
assert reopened.get_default_strategy_template() == template
|
||||
assert reopened.list_instruments() == [instrument]
|
||||
assert reopened.list_trades(account_id=account.id) == [trade]
|
||||
|
||||
|
||||
def test_trade_update_and_delete_are_persistent(tmp_path):
|
||||
repo = Repository(tmp_path / "grid.db")
|
||||
repo.initialize()
|
||||
account = repo.save_account(Account(id=None, name="主账户", initial_cash=Decimal("50000")))
|
||||
instrument = repo.save_instrument(Instrument(id=None, code="600000", name="浦发银行"))
|
||||
trade = repo.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date(2026, 7, 7),
|
||||
side=TradeSide.BUY,
|
||||
price=Decimal("10"),
|
||||
quantity=100,
|
||||
commission=Decimal("5"),
|
||||
trade_group=TradeGroup.BASE,
|
||||
)
|
||||
)
|
||||
|
||||
updated = repo.save_trade(
|
||||
Trade(
|
||||
id=trade.id,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date(2026, 7, 7),
|
||||
side=TradeSide.BUY,
|
||||
price=Decimal("9.8"),
|
||||
quantity=200,
|
||||
commission=Decimal("5"),
|
||||
trade_group=TradeGroup.BASE,
|
||||
notes="corrected",
|
||||
)
|
||||
)
|
||||
|
||||
assert repo.get_trade(trade.id) == updated
|
||||
|
||||
repo.close()
|
||||
reopened = Repository(tmp_path / "grid.db")
|
||||
reopened.initialize()
|
||||
assert reopened.get_trade(trade.id) == updated
|
||||
|
||||
reopened.delete_trade(trade.id)
|
||||
assert reopened.get_trade(trade.id) is None
|
||||
assert reopened.list_trades(account_id=account.id) == []
|
||||
Reference in New Issue
Block a user