853 lines
28 KiB
Python
853 lines
28 KiB
Python
"""KIS poll_fill terminal-order detection (dcy-013 / REQ-012, issue #9).
|
|
|
|
inquire-daily-ccld marks a cancelled order with cncl_yn="Y" and an
|
|
exchange-rejected order with rjct_qty. These tests stub the HTTP layer (token
|
|
pre-seeded, httpx.get faked) and assert the FillStatus mapping.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
|
|
from trex_core.adapters.broker import kis as kis_mod
|
|
from trex_core.adapters.broker.kis import KISBroker
|
|
|
|
ODNO = "0000123456"
|
|
|
|
|
|
def _broker():
|
|
b = KISBroker(
|
|
"12345678-01",
|
|
app_key="k",
|
|
app_secret="s",
|
|
base_url="https://openapivts.koreainvestment.com:29443", # mock endpoint
|
|
)
|
|
b._token = "tok"
|
|
b._token_exp = datetime.now(tz=UTC) + timedelta(hours=1)
|
|
return b
|
|
|
|
|
|
class _Resp:
|
|
def __init__(self, payload):
|
|
self._payload = payload
|
|
|
|
def json(self):
|
|
return self._payload
|
|
|
|
|
|
def _fake_get(payload):
|
|
def _get(*a, **k):
|
|
return _Resp(payload)
|
|
|
|
return _get
|
|
|
|
|
|
def _row(**kw):
|
|
base = {
|
|
"odno": ODNO,
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "N",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260608",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "005930",
|
|
}
|
|
base.update(kw)
|
|
return {"rt_cd": "0", "output1": [base]}
|
|
|
|
|
|
def test_full_external_cancel_maps_to_cancelled(monkeypatch):
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(_row(tot_ccld_qty="0", cncl_yn="Y")))
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.state == "cancelled" and fs.is_terminal_reject and not fs.is_partial_cancel
|
|
|
|
|
|
def test_zero_fill_not_cancelled_stays_accepted(monkeypatch):
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(_row(tot_ccld_qty="0", cncl_yn="N")))
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.state == "accepted" and not fs.is_terminal_reject
|
|
|
|
|
|
def test_zero_fill_full_rejection_maps_to_rejected(monkeypatch):
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get(
|
|
_row(
|
|
ord_qty="4",
|
|
tot_ccld_qty="0",
|
|
rmn_qty="0",
|
|
rjct_qty="4",
|
|
cncl_yn="",
|
|
)
|
|
),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.state == "rejected" and fs.is_terminal_reject
|
|
|
|
|
|
def test_zero_fill_partial_rejection_does_not_infer_terminal(monkeypatch):
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get(_row(ord_qty="10", tot_ccld_qty="0", rjct_qty="4", cncl_yn="")),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.state == "accepted" and not fs.is_terminal_reject
|
|
|
|
|
|
def test_partial_fill_then_cancel_flags_partial_cancel(monkeypatch):
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get(_row(ord_qty="10", tot_ccld_qty="4", avg_prvs="70000", cncl_yn="Y")),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.state == "partially_filled"
|
|
assert fs.cancelled and fs.is_partial_cancel
|
|
assert fs.filled_qty == 4 and fs.avg_price == 70000.0
|
|
assert fs.execution_id == "kis-daily-ccld:20260608:06010:0000123456:4"
|
|
|
|
|
|
def test_full_fill_unaffected_by_cancel_flag(monkeypatch):
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get(_row(ord_qty="10", tot_ccld_qty="10", avg_prvs="70000", cncl_yn="N")),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="005930", order_date="20260608"
|
|
)
|
|
assert fs.is_filled and fs.filled_qty == 10 and not fs.is_partial_cancel
|
|
|
|
|
|
# ── Regression: KIS child cancel row (orgn_odno) ─────────────────────────────
|
|
|
|
def _payload_with_child_cancel(parent_odno: str, child_odno: str, ord_qty: str = "89", symbol: str = "225190"):
|
|
"""Simulate KIS returning original row (cncl_yn='') + separate cancel child."""
|
|
parent = {
|
|
"odno": parent_odno,
|
|
"orgn_odno": "",
|
|
"ord_qty": ord_qty,
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"rmn_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": symbol,
|
|
}
|
|
child = {
|
|
"odno": child_odno,
|
|
"orgn_odno": parent_odno,
|
|
"ord_qty": ord_qty,
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "Y",
|
|
"rjct_qty": "0",
|
|
"rmn_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": symbol,
|
|
}
|
|
return {"rt_cd": "0", "output1": [parent, child]}
|
|
|
|
|
|
def test_child_cancel_row_orgn_odno_maps_to_cancelled(monkeypatch):
|
|
"""KIS child row with orgn_odno=<original> + cncl_yn=Y → full cancel."""
|
|
payload = _payload_with_child_cancel(ODNO, "0000123457")
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "cancelled", f"expected cancelled, got {fs.state}"
|
|
assert fs.is_terminal_reject
|
|
|
|
|
|
def test_correction_child_row_not_misclassified_as_cancel(monkeypatch):
|
|
"""Correction child (cncl_yn!='Y') must not be treated as a cancel.
|
|
|
|
Issue #81 AC: a correction/replacement child means the order is superseded
|
|
but we cannot safely determine terminal status without the replacement
|
|
identity → fail-closed as 'unknown' (not 'accepted/open_confirmed').
|
|
"""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
correction_child = {
|
|
"odno": "0000123458",
|
|
"orgn_odno": ODNO,
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "N", # correction, not cancel
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent, correction_child]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
# Correction child means replacement identity is unresolved → fail-closed
|
|
assert fs.state == "unknown", f"expected unknown, got {fs.state}"
|
|
raw = fs.raw if isinstance(fs.raw, dict) else {}
|
|
assert raw.get("reason") == "correction_child_present_unresolved"
|
|
# Must NOT be open_confirmed — that would allow new orders on the same slot
|
|
assert not raw.get("order_open_confirmed", False)
|
|
|
|
|
|
def test_correction_child_cancel_confirmed_quantity_maps_to_cancelled(monkeypatch):
|
|
"""KIS cancel-confirm quantity is terminal evidence even when cncl_yn is blank."""
|
|
payload = _payload_with_child_cancel(ODNO, "0000123458", ord_qty="31", symbol="053050")
|
|
parent, child = payload["output1"]
|
|
child.update(cncl_yn="", cnc_cfrm_qty="31")
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="053050", order_date="20260901"
|
|
)
|
|
|
|
assert fs.state == "cancelled"
|
|
assert fs.filled_qty == 0
|
|
assert fs.raw["reason"] == "replacement_chain_terminal_cancel"
|
|
assert "resolved_broker_order_id" not in fs.raw
|
|
|
|
|
|
def test_live_replacement_remains_fail_closed(monkeypatch):
|
|
payload = _payload_with_child_cancel(ODNO, "0000123458")
|
|
_, child = payload["output1"]
|
|
child.update(cncl_yn="", cnc_cfrm_qty="0", rmn_qty="10", ord_qty="10")
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="225190", order_date="20260901"
|
|
)
|
|
|
|
assert fs.state == "unknown"
|
|
assert fs.raw["reason"] == "replacement_live_unresolved"
|
|
assert not fs.raw.get("order_open_confirmed", False)
|
|
|
|
|
|
def test_replacement_partial_fill_then_confirmed_cancel(monkeypatch):
|
|
payload = _payload_with_child_cancel(ODNO, "0000123458", ord_qty="10")
|
|
_, child = payload["output1"]
|
|
child.update(
|
|
cncl_yn="",
|
|
cnc_cfrm_qty="6",
|
|
tot_ccld_qty="4",
|
|
rmn_qty="0",
|
|
avg_prvs="70100",
|
|
)
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="225190", order_date="20260901"
|
|
)
|
|
|
|
assert fs.state == "partially_filled"
|
|
assert fs.filled_qty == 4
|
|
assert fs.avg_price == 70100.0
|
|
assert fs.is_partial_cancel
|
|
assert fs.raw["reason"] == "replacement_chain_terminal_cancel"
|
|
|
|
|
|
def test_repeated_cumulative_fill_across_generations_is_not_ambiguous(monkeypatch):
|
|
payload = _payload_with_child_cancel(ODNO, "0000123458", ord_qty="10")
|
|
parent, child = payload["output1"]
|
|
parent.update(tot_ccld_qty="4", rmn_qty="0", avg_prvs="70100")
|
|
child.update(
|
|
cncl_yn="",
|
|
cnc_cfrm_qty="6",
|
|
tot_ccld_qty="4",
|
|
rmn_qty="0",
|
|
avg_prvs="70100",
|
|
)
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="225190", order_date="20260901"
|
|
)
|
|
|
|
assert fs.state == "partially_filled"
|
|
assert fs.filled_qty == 4
|
|
assert fs.avg_price == 70100.0
|
|
assert fs.is_partial_cancel
|
|
|
|
|
|
def test_multi_generation_replacement_chain_terminal_cancel(monkeypatch):
|
|
payload = _payload_with_child_cancel(ODNO, "0000123458", ord_qty="10")
|
|
parent, replacement = payload["output1"]
|
|
replacement.update(cncl_yn="", cnc_cfrm_qty="0", rmn_qty="0")
|
|
terminal = {
|
|
**replacement,
|
|
"odno": "0000123459",
|
|
"orgn_odno": replacement["odno"],
|
|
"cnc_cfrm_qty": "10",
|
|
}
|
|
payload["output1"] = [terminal, parent, replacement]
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="225190", order_date="20260901"
|
|
)
|
|
|
|
assert fs.state == "cancelled"
|
|
assert fs.raw["replacement_depth"] == 2
|
|
|
|
|
|
def test_replacement_chain_branching_is_ambiguous(monkeypatch):
|
|
payload = _payload_with_child_cancel(ODNO, "0000123458", ord_qty="10")
|
|
parent, child = payload["output1"]
|
|
child.update(cncl_yn="", cnc_cfrm_qty="10")
|
|
sibling = {**child, "odno": "0000123459"}
|
|
payload["output1"] = [parent, child, sibling]
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="225190", order_date="20260901"
|
|
)
|
|
|
|
assert fs.state == "unknown"
|
|
assert fs.raw["reason"] == "replacement_chain_ambiguous"
|
|
|
|
|
|
def test_zero_remaining_without_cancel_confirmation_remains_unknown(monkeypatch):
|
|
payload = _payload_with_child_cancel(ODNO, "0000123458", ord_qty="10")
|
|
_, child = payload["output1"]
|
|
child.update(cncl_yn="", cnc_cfrm_qty="0", rmn_qty="0")
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="225190", order_date="20260901"
|
|
)
|
|
|
|
assert fs.state == "unknown"
|
|
assert fs.raw["reason"] == "correction_child_present_unresolved"
|
|
|
|
|
|
def test_stable_zero_fill_replacement_chain_converges_after_grace(monkeypatch):
|
|
payload = _payload_with_child_cancel(ODNO, "0000123458", ord_qty="31", symbol="053050")
|
|
parent, child = payload["output1"]
|
|
parent.update(rmn_qty="0")
|
|
child.update(cncl_yn="", cnc_cfrm_qty="0", rmn_qty="0")
|
|
monotonic_now = [100.0]
|
|
monkeypatch.setattr(kis_mod, "monotonic", lambda: monotonic_now[0])
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
broker = _broker()
|
|
|
|
first = broker.poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="053050", order_date="20260901"
|
|
)
|
|
assert first.state == "unknown"
|
|
assert first.raw["reason"] == "correction_child_present_unresolved"
|
|
|
|
monotonic_now[0] += 90
|
|
confirmed = broker.poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="053050", order_date="20260901"
|
|
)
|
|
assert confirmed.state == "cancelled"
|
|
assert confirmed.raw["reason"] == "replacement_chain_stable_zero_terminal"
|
|
|
|
|
|
def test_zero_fill_terminal_grace_resets_when_chain_changes(monkeypatch):
|
|
payload = _payload_with_child_cancel(ODNO, "0000123458", ord_qty="31", symbol="053050")
|
|
parent, child = payload["output1"]
|
|
parent.update(rmn_qty="0")
|
|
child.update(cncl_yn="", cnc_cfrm_qty="0", rmn_qty="0")
|
|
monotonic_now = [100.0]
|
|
monkeypatch.setattr(kis_mod, "monotonic", lambda: monotonic_now[0])
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
broker = _broker()
|
|
|
|
first = broker.poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="053050", order_date="20260901"
|
|
)
|
|
assert first.state == "unknown"
|
|
|
|
monotonic_now[0] += 90
|
|
child["rmn_qty"] = "31"
|
|
changed = broker.poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="053050", order_date="20260901"
|
|
)
|
|
assert changed.raw["reason"] == "replacement_live_unresolved"
|
|
|
|
monotonic_now[0] += 90
|
|
child["rmn_qty"] = "0"
|
|
restarted_grace = broker.poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="053050", order_date="20260901"
|
|
)
|
|
assert restarted_grace.state == "unknown"
|
|
|
|
monotonic_now[0] += 90
|
|
confirmed = broker.poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="053050", order_date="20260901"
|
|
)
|
|
assert confirmed.state == "cancelled"
|
|
|
|
|
|
def test_incomplete_cancel_confirmation_remains_unknown(monkeypatch):
|
|
payload = _payload_with_child_cancel(ODNO, "0000123458", ord_qty="10")
|
|
_, child = payload["output1"]
|
|
child.update(cncl_yn="", cnc_cfrm_qty="3", rmn_qty="0")
|
|
monkeypatch.setattr(kis_mod.httpx, "get", _fake_get(payload))
|
|
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="225190", order_date="20260901"
|
|
)
|
|
|
|
assert fs.state == "unknown"
|
|
assert fs.raw["reason"] == "correction_child_present_unresolved"
|
|
|
|
|
|
def test_partial_fill_with_child_cancel_row(monkeypatch):
|
|
"""Partial fill on parent + cancel child → partial_cancel."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "4",
|
|
"avg_prvs": "70000",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
child = {
|
|
"odno": "0000123459",
|
|
"orgn_odno": ODNO,
|
|
"ord_qty": "6",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "Y",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent, child]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="sell", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "partially_filled"
|
|
assert fs.cancelled and fs.is_partial_cancel
|
|
assert fs.filled_qty == 4
|
|
|
|
|
|
# ── Regression: identity mismatch / ambiguous child rows ─────────────────────
|
|
|
|
def test_child_cancel_mismatched_branch_is_ignored(monkeypatch):
|
|
"""Cancel child from a different branch (ord_gno_brno) must not affect parent."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
child_wrong_branch = {
|
|
"odno": "0000199999",
|
|
"orgn_odno": ODNO,
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "Y",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "99999", # different branch
|
|
"pdno": "225190",
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent, child_wrong_branch]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
# Child has wrong branch → not a matching cancel → parent stays accepted
|
|
assert fs.state == "accepted", f"expected accepted (mismatch branch), got {fs.state}"
|
|
|
|
|
|
def test_child_cancel_mismatched_symbol_is_ignored(monkeypatch):
|
|
"""Cancel child with different symbol (pdno) must not affect parent."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
child_wrong_symbol = {
|
|
"odno": "0000199998",
|
|
"orgn_odno": ODNO,
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "Y",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "000660", # different symbol
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent, child_wrong_symbol]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "accepted", f"expected accepted (mismatch symbol), got {fs.state}"
|
|
|
|
|
|
def test_child_cancel_mismatched_date_is_ignored(monkeypatch):
|
|
"""Cancel child with different ord_dt must not affect parent."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
child_wrong_date = {
|
|
"odno": "0000199997",
|
|
"orgn_odno": ODNO,
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "Y",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260831", # different date
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent, child_wrong_date]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "accepted", f"expected accepted (mismatch date), got {fs.state}"
|
|
|
|
|
|
def test_ambiguous_multiple_child_cancel_rows_return_unknown(monkeypatch):
|
|
"""Two matching cancel children → ambiguous identity → fail-closed as unknown."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
child1 = {**parent, "odno": "0000123460", "orgn_odno": ODNO, "cncl_yn": "Y", "ord_qty": "10"}
|
|
child2 = {**parent, "odno": "0000123461", "orgn_odno": ODNO, "cncl_yn": "Y", "ord_qty": "10"}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent, child1, child2]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "unknown", f"expected unknown (ambiguous children), got {fs.state}"
|
|
raw = fs.raw if isinstance(fs.raw, dict) else {}
|
|
assert raw.get("reason") == "child_cancel_ambiguous"
|
|
|
|
|
|
# ── Regression: parent row identity mismatch with request ────────────────────
|
|
|
|
def test_parent_row_wrong_branch_returns_unknown(monkeypatch):
|
|
"""Parent row with wrong ord_gno_brno (branch) relative to broker_order_id → unknown."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "99999", # wrong branch — request has 06010
|
|
"pdno": "225190",
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent]})
|
|
)
|
|
# broker_order_id encodes branch 06010, but row has 99999
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "unknown", f"expected unknown (wrong parent branch), got {fs.state}"
|
|
raw = fs.raw if isinstance(fs.raw, dict) else {}
|
|
assert "mismatch" in raw.get("reason", "")
|
|
|
|
|
|
def test_parent_row_wrong_date_returns_unknown(monkeypatch):
|
|
"""Parent row with wrong ord_dt relative to order_date arg → unknown."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260831", # wrong date — request has 20260901
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "unknown", f"expected unknown (wrong parent date), got {fs.state}"
|
|
raw = fs.raw if isinstance(fs.raw, dict) else {}
|
|
assert "mismatch" in raw.get("reason", "")
|
|
|
|
|
|
def test_parent_row_wrong_symbol_returns_unknown(monkeypatch):
|
|
"""Parent row with wrong pdno (symbol) relative to symbol arg → unknown."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "000660", # wrong symbol — request has 225190
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "unknown", f"expected unknown (wrong parent symbol), got {fs.state}"
|
|
raw = fs.raw if isinstance(fs.raw, dict) else {}
|
|
assert "mismatch" in raw.get("reason", "")
|
|
|
|
|
|
def test_correction_child_wrong_identity_is_not_treated_as_correction(monkeypatch):
|
|
"""Correction child with wrong branch/date/symbol must not trigger correction_child_present_unresolved."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
unrelated_correction = {
|
|
"odno": "0000199990",
|
|
"orgn_odno": ODNO,
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "N",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260831", # different date → not related to our request
|
|
"ord_gno_brno": "99999", # different branch
|
|
"pdno": "000660", # different symbol
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get({"rt_cd": "0", "output1": [parent, unrelated_correction]}),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
# Unrelated correction must not cause correction_child_present_unresolved
|
|
assert fs.state == "accepted", f"expected accepted (unrelated correction), got {fs.state}"
|
|
raw = fs.raw if isinstance(fs.raw, dict) else {}
|
|
assert raw.get("reason") != "correction_child_present_unresolved"
|
|
|
|
|
|
# ── Regression: parent identity boundary cases (missing / wrong-first / ambiguous) ─
|
|
|
|
def test_parent_missing_identity_fields_returns_unknown(monkeypatch):
|
|
"""Parent row with missing pdno and ord_dt → treated as identity mismatch (fail-closed)."""
|
|
parent_no_identity = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "Y", # would be cancelled if accepted
|
|
"rjct_qty": "0",
|
|
# ord_dt, ord_gno_brno, pdno intentionally absent
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent_no_identity]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
# Missing identity → fail-closed, not terminal
|
|
assert fs.state == "unknown", f"expected unknown (missing identity), got {fs.state}"
|
|
assert not fs.is_terminal_reject
|
|
|
|
|
|
def test_child_cancel_missing_identity_fields_not_accepted_as_cancel(monkeypatch):
|
|
"""Cancel child with missing identity fields must NOT be accepted as matching child."""
|
|
parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
child_no_identity = {
|
|
"odno": "0000123461",
|
|
"orgn_odno": ODNO,
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "Y",
|
|
"rjct_qty": "0",
|
|
# ord_dt, ord_gno_brno, pdno intentionally absent
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get({"rt_cd": "0", "output1": [parent, child_no_identity]}),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
# Child has missing identity → not a matching cancel; parent has no fill → accepted
|
|
assert fs.state == "accepted", f"expected accepted (child missing identity), got {fs.state}"
|
|
|
|
|
|
def test_wrong_parent_first_exact_parent_later_finds_cancel(monkeypatch):
|
|
"""When the wrong-branch parent comes first, the exact parent+cancel child later must win."""
|
|
wrong_parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "99999", # wrong branch — comes first in list
|
|
"pdno": "225190",
|
|
}
|
|
exact_parent = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010", # correct branch — comes second
|
|
"pdno": "225190",
|
|
}
|
|
child = {
|
|
"odno": "0000123462",
|
|
"orgn_odno": ODNO,
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "Y",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx,
|
|
"get",
|
|
_fake_get({"rt_cd": "0", "output1": [wrong_parent, exact_parent, child]}),
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
# Must find the exact parent + cancel child even though wrong parent comes first
|
|
assert fs.state == "cancelled", f"expected cancelled (exact parent later), got {fs.state}"
|
|
|
|
|
|
def test_ambiguous_exact_parents_returns_unknown(monkeypatch):
|
|
"""Two rows with identical exact identity → ambiguous parent → unknown."""
|
|
parent1 = {
|
|
"odno": ODNO,
|
|
"orgn_odno": "",
|
|
"ord_qty": "10",
|
|
"tot_ccld_qty": "0",
|
|
"avg_prvs": "0",
|
|
"cncl_yn": "",
|
|
"rjct_qty": "0",
|
|
"ord_dt": "20260901",
|
|
"ord_gno_brno": "06010",
|
|
"pdno": "225190",
|
|
}
|
|
parent2 = {**parent1} # exact duplicate
|
|
monkeypatch.setattr(
|
|
kis_mod.httpx, "get", _fake_get({"rt_cd": "0", "output1": [parent1, parent2]})
|
|
)
|
|
fs = _broker().poll_fill(
|
|
f"06010:{ODNO}", side="buy", symbol="225190", order_date="20260901"
|
|
)
|
|
assert fs.state == "unknown", f"expected unknown (ambiguous parents), got {fs.state}"
|
|
raw = fs.raw if isinstance(fs.raw, dict) else {}
|
|
assert raw.get("reason") == "parent_identity_ambiguous"
|