fix(kis): collect all parent candidates before child evaluation; boundary case tests
Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
This commit is contained in:
co-authored by
genichin
parent
584e08eb36
commit
f3fbc179b0
@@ -7,7 +7,7 @@ pre-seeded, httpx.get faked) and assert the FillStatus mapping.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from trex_core.adapters.broker import kis as kis_mod
|
||||
from trex_core.adapters.broker.kis import KISBroker
|
||||
@@ -23,7 +23,7 @@ def _broker():
|
||||
base_url="https://openapivts.koreainvestment.com:29443", # mock endpoint
|
||||
)
|
||||
b._token = "tok"
|
||||
b._token_exp = datetime.now(tz=timezone.utc) + timedelta(hours=1)
|
||||
b._token_exp = datetime.now(tz=UTC) + timedelta(hours=1)
|
||||
return b
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ def _row(**kw):
|
||||
"rjct_qty": "0",
|
||||
"ord_dt": "20260608",
|
||||
"ord_gno_brno": "06010",
|
||||
"pdno": "005930",
|
||||
}
|
||||
base.update(kw)
|
||||
return {"rt_cd": "0", "output1": [base]}
|
||||
@@ -509,3 +510,140 @@ def test_correction_child_wrong_identity_is_not_treated_as_correction(monkeypatc
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user