test: cover KIS pagination consumer seams

This commit is contained in:
Hermes Agent
2026-08-10 00:27:10 +09:00
parent 0fa8f39be9
commit c0f36946a3
4 changed files with 175 additions and 31 deletions
+50 -16
View File
@@ -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()