feat: paginate KIS balance inquiries
This commit is contained in:
@@ -3,7 +3,9 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import replace
|
||||
|
||||
from trex_core.application.broker_port import BalanceItem
|
||||
import pytest
|
||||
|
||||
from trex_core.application.broker_port import BalanceItem, BrokerBalanceError
|
||||
from trex_core.application.portfolio import InMemoryReconcileWarningStore
|
||||
from trex_core.application.reconciliation import reconcile_core_holdings
|
||||
from trex_core.domain.slot import SlotService, SlotState
|
||||
@@ -24,6 +26,11 @@ class FakeBroker:
|
||||
return list(self._b)
|
||||
|
||||
|
||||
class FailingBroker(FakeBroker):
|
||||
def get_balance(self):
|
||||
raise BrokerBalanceError("malformed_response")
|
||||
|
||||
|
||||
def _hold(repo, svc, pid, symbol, qty):
|
||||
repo.reconcile_universe(INST, {pid: "SYM"})
|
||||
slot = repo.find_slot_by_portfolio(INST, pid)
|
||||
@@ -77,6 +84,21 @@ def test_ok_when_qty_matches():
|
||||
assert d[str(slot.slot_id)] == "ok"
|
||||
|
||||
|
||||
def test_later_page_snapshot_symbol_keeps_holding_slot_ok():
|
||||
repo, svc = _wire()
|
||||
slot = _hold(repo, svc, "pf-later", "000660", 4)
|
||||
|
||||
decisions = reconcile_core_holdings(
|
||||
slot_repo=repo,
|
||||
slot_service=svc,
|
||||
broker=FakeBroker([BalanceItem(symbol="000660", hldg_qty=4)]),
|
||||
instance_key=INST,
|
||||
)
|
||||
|
||||
assert decisions == {str(slot.slot_id): "ok"}
|
||||
assert repo.fetch_slot(slot.slot_id).status is SlotState.HOLDING
|
||||
|
||||
|
||||
def test_B_broker_holds_unmanaged_warns():
|
||||
repo, svc = _wire()
|
||||
broker = FakeBroker([BalanceItem(symbol="000660", hldg_qty=5)]) # core has none
|
||||
@@ -149,3 +171,18 @@ def test_other_instance_only_managed_slot_remains_unmanaged_position_without_wri
|
||||
assert repo.update_calls == writes_before
|
||||
assert warn_store.list_warned() == set()
|
||||
assert repo.list_slots(INST) == []
|
||||
|
||||
|
||||
def test_reconcile_core_holdings_propagates_balance_failure_without_slot_write():
|
||||
repo, svc = _wire()
|
||||
slot = _hold(repo, svc, "pf-a", "005930", 10)
|
||||
|
||||
with pytest.raises(BrokerBalanceError, match="malformed_response"):
|
||||
reconcile_core_holdings(
|
||||
slot_repo=repo,
|
||||
slot_service=svc,
|
||||
broker=FailingBroker([]),
|
||||
instance_key=INST,
|
||||
)
|
||||
|
||||
assert repo.fetch_slot(slot.slot_id).status is SlotState.HOLDING
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
"""Position reconciliation tests (REQ-031)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from trex_core.application.broker_port import BalanceItem
|
||||
import pytest
|
||||
|
||||
from trex_core.application.broker_port import BalanceItem, BrokerBalanceError
|
||||
from trex_core.application.portfolio import PortfolioItem, SnapshotStore
|
||||
from trex_core.application.reconciliation import reconcile_positions
|
||||
|
||||
@@ -16,6 +18,11 @@ class FakeBroker:
|
||||
def get_balance(self): return list(self._b)
|
||||
|
||||
|
||||
class FailingBroker(FakeBroker):
|
||||
def get_balance(self):
|
||||
raise BrokerBalanceError("malformed_response")
|
||||
|
||||
|
||||
def _store_with(*items):
|
||||
store = SnapshotStore()
|
||||
store.swap(list(items))
|
||||
@@ -47,6 +54,20 @@ def test_subsequent_match_auto_clears_warning():
|
||||
assert store.warnings() == set()
|
||||
|
||||
|
||||
def test_reconcile_positions_accepts_symbol_from_complete_later_page_snapshot():
|
||||
store = _store_with(
|
||||
PortfolioItem(portfolio_id="pf-later", symbol="000660", cash_balance=0, asset_quantity=4),
|
||||
)
|
||||
|
||||
decisions = reconcile_positions(
|
||||
store=store,
|
||||
broker=FakeBroker([BalanceItem("000660", 4)]),
|
||||
)
|
||||
|
||||
assert decisions == {"pf-later": "ok"}
|
||||
assert store.warnings() == set()
|
||||
|
||||
|
||||
def test_multiple_portfolios_same_symbol_aggregate():
|
||||
store = _store_with(
|
||||
PortfolioItem(portfolio_id="pf-A", symbol="005930",
|
||||
@@ -86,3 +107,14 @@ def test_paper_broker_returns_deterministic_balance():
|
||||
syms = {b.symbol for b in broker.get_balance()}
|
||||
assert "000660" not in syms # net 0 → dropped
|
||||
assert "005930" in syms
|
||||
|
||||
|
||||
def test_reconcile_positions_propagates_balance_failure_without_warning_write():
|
||||
store = _store_with(
|
||||
PortfolioItem(portfolio_id="pf-A", symbol="005930", cash_balance=0, asset_quantity=10),
|
||||
)
|
||||
|
||||
with pytest.raises(BrokerBalanceError, match="malformed_response"):
|
||||
reconcile_positions(store=store, broker=FailingBroker([]))
|
||||
|
||||
assert store.warnings() == set()
|
||||
|
||||
Reference in New Issue
Block a user