torchtrade icon indicating copy to clipboard operation
torchtrade copied to clipboard

Seven live-venue branches that no test can reach, because the mock never models the field

Open BY571 opened this issue 3 months ago • 0 comments

Found by a branch-coverage sweep during #414 review (PR #420). All seven are pre-existing; none was introduced by that work.

They share one shape, and it is the reason #414's own gaps hid for so long: the production code reads a payload field the test mock does not supply, so the branch that reads it never executes and a mutation to it survives the entire suite. A MagicMock that auto-creates attributes, or a conftest that omits an optional key, makes the code look covered while the interesting leg is dead.

This is the same defect class as the four in #414 — there, no venue mock carried its notional field, so nothing could distinguish "reads the field" from "hardcodes 0.0".

Ranked by money

# Branch Why it matters Severity
1 okx's mark-price success path — okx/order_executor.py:533 markPx is in the conftest, but the only test calling the real get_mark_price() deliberately omits it to test the raise; everything else stubs trader.get_mark_price. So nothing proves okx reads markPx rather than idxPx. Mark price feeds unrealized_pnl_pct, distance_to_liquidation, and fractional sizing. 8/10
2 bybit's "unusable position side" guard is dead — bybit/order_executor.py:333-338 The conftest always supplies "Buy"/"Sell", so the POSITION_UNKNOWN return never runs and the qty = size if side == "Buy" else -size fallthrough is unguarded. This is the exact defect #341 fixed, and its two sibling tests cover bitget and okx only — invariant 3, untested on one of four venues. 8/10
3 okx posSide == "long" never exercised — okx/order_executor.py:407 "short" and "net" are covered; the parametrize has no "long", and the one fixture with it supplies two positions so POSITION_UNKNOWN returns first. Hedge-mode direction. 7/10
4 alpaca's fractionable-equity rounding leg is dead — alpaca/order_executor.py:159 tests/mocks/alpaca.py:80-82 defaults MockAsset to BTC/USD's crypto values, so if min_qty and step always wins. _round_qty is the quantity lattice for every Alpaca order; the US-equities leg has no test and MAX_EQUITY_QTY_DECIMALS = 9 is asserted nowhere. 7/10
5 okx's round-up-to-minimum is unreachable — okx/order_executor.py:187 tests/envs/okx/conftest.py sets minSz == lotSz == "0.001", so the clamp can never fire. This is precisely the behaviour #414's shared guard docstring names as dangerous ("rounding up allocates more than the action asked for"). SLTP is protected by okx's override; the plain path still routes through here. 6/10
6 bitget's mark-price fallback fails open to 0.0 — bitget/order_executor.py:685 Never executed, because the conftest supplies both info.markPrice and last. Note "0" is truthy, so a zero mark returns 0.0 with no exception and no > 0 validation. Every other venue raises. 6/10
7 bitget hedge mode is untested — bitget/order_executor.py:296 params['tradeSide'] = 'close' if reduce_only else 'open' never gets built. position_mode is user-settable config; bybit has three hedge tests, bitget none. On a hedge account tradeSide is what tells the venue whether an order reduces or opens. 6/10

Suggested approach

Fix the mocks first, then the branches. A conftest that models the real payload — including its optional fields — is what makes the difference between a test that asserts behaviour and one that asserts a default. In #414 that single change turned three "passing" reads into three dying mutants.

(2) and (5) are the two I would take first: (2) is a re-opening of #341 on the one venue its fix missed, and (5) is a live instance of the round-up-instead-of-refuse behaviour #414 just argued against in a docstring.

Credit

Branch-coverage sweep by the pr-review-toolkit:pr-test-analyzer agent during round 1 of PR #420, corroborated against a 175-key payload sweep per venue (~160 keys and ~330 of 393 uncovered lines checked and rejected before these seven remained).

BY571 avatar Aug 31 '26 18:08 BY571