test: cover KIS pagination consumer seams
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import replace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -26,11 +27,6 @@ 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)
|
||||
@@ -84,19 +80,24 @@ def test_ok_when_qty_matches():
|
||||
assert d[str(slot.slot_id)] == "ok"
|
||||
|
||||
|
||||
def test_later_page_snapshot_symbol_keeps_holding_slot_ok():
|
||||
def test_later_page_snapshot_symbol_keeps_holding_slot_ok(kis_balance_broker_factory):
|
||||
repo, svc = _wire()
|
||||
slot = _hold(repo, svc, "pf-later", "000660", 4)
|
||||
broker, calls = kis_balance_broker_factory([
|
||||
{"rows": [], "tr_cont": "F", "fk": "A", "nk": "1"},
|
||||
{"rows": [{"pdno": "000660", "hldg_qty": "4"}], "tr_cont": "E"},
|
||||
])
|
||||
|
||||
decisions = reconcile_core_holdings(
|
||||
slot_repo=repo,
|
||||
slot_service=svc,
|
||||
broker=FakeBroker([BalanceItem(symbol="000660", hldg_qty=4)]),
|
||||
broker=broker,
|
||||
instance_key=INST,
|
||||
)
|
||||
|
||||
assert decisions == {str(slot.slot_id): "ok"}
|
||||
assert repo.fetch_slot(slot.slot_id).status is SlotState.HOLDING
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
def test_B_broker_holds_unmanaged_warns():
|
||||
@@ -173,16 +174,39 @@ def test_other_instance_only_managed_slot_remains_unmanaged_position_without_wri
|
||||
assert repo.list_slots(INST) == []
|
||||
|
||||
|
||||
def test_reconcile_core_holdings_propagates_balance_failure_without_slot_write():
|
||||
repo, svc = _wire()
|
||||
def test_reconcile_core_holdings_propagates_later_page_failure_without_write(
|
||||
kis_balance_broker_factory, monkeypatch
|
||||
):
|
||||
repo = TrackingRepository()
|
||||
svc = SlotService(repo, instance_key=INST)
|
||||
slot = _hold(repo, svc, "pf-a", "005930", 10)
|
||||
writes_before = repo.update_calls
|
||||
warn_store = InMemoryReconcileWarningStore()
|
||||
set_warned = Mock(wraps=warn_store.set_warned)
|
||||
clear_warned = Mock(wraps=warn_store.clear_warned)
|
||||
monkeypatch.setattr(warn_store, "set_warned", set_warned)
|
||||
monkeypatch.setattr(warn_store, "clear_warned", clear_warned)
|
||||
broker, calls = kis_balance_broker_factory([
|
||||
{
|
||||
"rows": [{"pdno": "005930", "hldg_qty": "10"}],
|
||||
"tr_cont": "F",
|
||||
"fk": "A",
|
||||
"nk": "1",
|
||||
},
|
||||
{"rows": [], "tr_cont": "X"},
|
||||
])
|
||||
|
||||
with pytest.raises(BrokerBalanceError, match="malformed_response"):
|
||||
reconcile_core_holdings(
|
||||
slot_repo=repo,
|
||||
slot_service=svc,
|
||||
broker=FailingBroker([]),
|
||||
broker=broker,
|
||||
instance_key=INST,
|
||||
warn_store=warn_store,
|
||||
)
|
||||
|
||||
assert len(calls) == 2
|
||||
assert repo.fetch_slot(slot.slot_id).status is SlotState.HOLDING
|
||||
assert repo.update_calls == writes_before
|
||||
set_warned.assert_not_called()
|
||||
clear_warned.assert_not_called()
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
"""Position reconciliation tests (REQ-031)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from trex_core.application.broker_port import BalanceItem, BrokerBalanceError
|
||||
from trex_core.application.portfolio import PortfolioItem, SnapshotStore
|
||||
from trex_core.application.portfolio import (
|
||||
InMemoryReconcileWarningStore,
|
||||
PortfolioItem,
|
||||
SnapshotStore,
|
||||
)
|
||||
from trex_core.application.reconciliation import reconcile_positions
|
||||
|
||||
|
||||
@@ -18,11 +24,6 @@ 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))
|
||||
@@ -54,18 +55,22 @@ def test_subsequent_match_auto_clears_warning():
|
||||
assert store.warnings() == set()
|
||||
|
||||
|
||||
def test_reconcile_positions_accepts_symbol_from_complete_later_page_snapshot():
|
||||
def test_reconcile_positions_accepts_symbol_from_complete_later_page_snapshot(
|
||||
kis_balance_broker_factory,
|
||||
):
|
||||
store = _store_with(
|
||||
PortfolioItem(portfolio_id="pf-later", symbol="000660", cash_balance=0, asset_quantity=4),
|
||||
)
|
||||
broker, calls = kis_balance_broker_factory([
|
||||
{"rows": [], "tr_cont": "F", "fk": "A", "nk": "1"},
|
||||
{"rows": [{"pdno": "000660", "hldg_qty": "4"}], "tr_cont": "E"},
|
||||
])
|
||||
|
||||
decisions = reconcile_positions(
|
||||
store=store,
|
||||
broker=FakeBroker([BalanceItem("000660", 4)]),
|
||||
)
|
||||
decisions = reconcile_positions(store=store, broker=broker)
|
||||
|
||||
assert decisions == {"pf-later": "ok"}
|
||||
assert store.warnings() == set()
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
def test_multiple_portfolios_same_symbol_aggregate():
|
||||
@@ -109,12 +114,41 @@ def test_paper_broker_returns_deterministic_balance():
|
||||
assert "005930" in syms
|
||||
|
||||
|
||||
def test_reconcile_positions_propagates_balance_failure_without_warning_write():
|
||||
store = _store_with(
|
||||
def test_reconcile_positions_propagates_later_page_failure_without_warning_write(
|
||||
kis_balance_broker_factory, monkeypatch
|
||||
):
|
||||
store = SnapshotStore()
|
||||
store.swap([
|
||||
PortfolioItem(portfolio_id="pf-A", symbol="005930", cash_balance=0, asset_quantity=10),
|
||||
)
|
||||
])
|
||||
store.set_warning("pf-A", True)
|
||||
warning_write = Mock(wraps=store.set_warning)
|
||||
monkeypatch.setattr(store, "set_warning", warning_write)
|
||||
warn_store = InMemoryReconcileWarningStore()
|
||||
set_warned = Mock(wraps=warn_store.set_warned)
|
||||
clear_warned = Mock(wraps=warn_store.clear_warned)
|
||||
monkeypatch.setattr(warn_store, "set_warned", set_warned)
|
||||
monkeypatch.setattr(warn_store, "clear_warned", clear_warned)
|
||||
broker, calls = kis_balance_broker_factory([
|
||||
{
|
||||
"rows": [{"pdno": "005930", "hldg_qty": "10"}],
|
||||
"tr_cont": "F",
|
||||
"fk": "A",
|
||||
"nk": "1",
|
||||
},
|
||||
{"rows": [], "tr_cont": "X"},
|
||||
])
|
||||
|
||||
with pytest.raises(BrokerBalanceError, match="malformed_response"):
|
||||
reconcile_positions(store=store, broker=FailingBroker([]))
|
||||
reconcile_positions(
|
||||
store=store,
|
||||
broker=broker,
|
||||
slot_repo=object(),
|
||||
warn_store=warn_store,
|
||||
)
|
||||
|
||||
assert store.warnings() == set()
|
||||
assert len(calls) == 2
|
||||
assert store.warnings() == {"pf-A"}
|
||||
warning_write.assert_not_called()
|
||||
set_warned.assert_not_called()
|
||||
clear_warned.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user