From a031a9fa8bd26000588833cfa5bada8f8c3da8d8 Mon Sep 17 00:00:00 2001 From: goodboy Date: Sun, 29 Nov 2026 16:28:38 -0500 Subject: [PATCH] Test IB fill exchange lookup during ledger conversion Exercise the exchange fallback from dd32746c through `update_ledger_from_api_trades()`. An exchange-only fill formerly requested its contract again. Cover a populated primary exchange and the two-empty-fields lookup as controls. Check the stored execution venue and normalized transaction ID, price and size with a synthetic fill and an offline broker lookup. (this patch was generated in some part by `Codex` using `GPT-6` (`OpenAI`)) --- tests/test_ib_trade_events.py | 60 ++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/tests/test_ib_trade_events.py b/tests/test_ib_trade_events.py index 60019d21..4460b890 100644 --- a/tests/test_ib_trade_events.py +++ b/tests/test_ib_trade_events.py @@ -4,6 +4,7 @@ Execution identity across IB fill and commission callbacks. ''' import asyncio from types import SimpleNamespace +from unittest.mock import AsyncMock from bidict import bidict from eventkit import Event @@ -22,7 +23,10 @@ import trio from piker.brokers.ib import broker from piker.brokers.ib.api import NonShittyIB -from piker.brokers.ib.ledger import api_trades_to_ledger_entries +from piker.brokers.ib.ledger import ( + api_trades_to_ledger_entries, + update_ledger_from_api_trades, +) from piker.clearing import OrderDialogs @@ -39,6 +43,60 @@ def make_fill(tid: str) -> Fill: ) +@pytest.mark.parametrize( + ('primary', 'exchange', 'lookup'), + [ + ('CME', 'GLOBEX', False), + ('', 'CME', False), + ('', '', True), + ], +) +def test_fill_contract_exchange_lookup( + primary: str, + exchange: str, + lookup: bool, +) -> None: + ''' + Qualify venue lookup in `update_ledger_from_api_trades()`. + + - Either contract exchange field avoids a broker request. + - Two empty fields retain the contract lookup. + - Ledger conversion preserves the execution and its venue. + + ''' + fill = make_fill('fill-1') + fill.contract.primaryExchange = primary + fill.contract.exchange = exchange + fill.contract.currency = 'USD' + fill.execution.exchange = 'GLOBEX' + fill.execution.side = 'BOT' + fill.execution.price = 100 + fill.execution.shares = 1 + client = AsyncMock() + client.get_con.return_value = [] + + transactions, entries = trio.run( + update_ledger_from_api_trades, + [fill], + client, + bidict({'DU_TEST': 'paper'}), + ) + if lookup: + client.get_con.assert_awaited_once_with( + conid=fill.contract.conId, + ) + + else: + client.get_con.assert_not_awaited() + + entry = entries['paper']['fill-1'] + assert entry['exchange'] == 'GLOBEX' + assert entry['primaryExchange'] == primary + txn = transactions['paper']['fill-1'] + assert txn.tid == 'fill-1' + assert (txn.price, txn.size) == (100, 1) + + @pytest.mark.parametrize('code', [2181, 10349, 202]) def test_order_notice_does_not_synthesize_cancellation( code: int,