From 3e74d3529904cabfb1a86b68d1def62efd52be0a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 24 Aug 2026 10:25:35 +0000 Subject: [PATCH] test: validate ledger backfill flow Co-authored-by: genichin <4282227+genichin@users.noreply.github.com> --- tests/application/test_ledger_outbox.py | 6 +++--- tests/runtime/test_cli_ledger_backfill.py | 4 ++-- trex_core/runtime/cli.py | 21 ++++++++++++--------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/tests/application/test_ledger_outbox.py b/tests/application/test_ledger_outbox.py index 5867064..5ce7f71 100644 --- a/tests/application/test_ledger_outbox.py +++ b/tests/application/test_ledger_outbox.py @@ -1,7 +1,7 @@ """LedgerOutbox + LedgerWriter direct unit tests (REQ-019).""" from __future__ import annotations -from datetime import datetime, timedelta, timezone +from datetime import UTC, datetime, timedelta from trex_core.adapters.ledger.in_memory import InMemoryLedger from trex_core.application.ledger_outbox import ( @@ -17,7 +17,7 @@ def _trade(execution_id="paper:PAPER-1:10") -> LedgerTrade: return LedgerTrade( asset_id="PF-1", order_id="PAPER-1", execution_id=execution_id, side="buy", qty=10, price=1000.0, - executed_at=datetime(2026, 6, 4, tzinfo=timezone.utc), + executed_at=datetime(2026, 6, 4, tzinfo=UTC), ) @@ -57,7 +57,7 @@ def test_writer_marks_conflict_blocked_and_does_not_retry(): def test_writer_retries_until_success(): # dcy-015: retryable rows now back off; share an injected clock between the # outbox (list_pending "now") and the writer (next_retry_at) to advance past it. - now = {"t": datetime(2026, 6, 4, tzinfo=timezone.utc)} + now = {"t": datetime(2026, 6, 4, tzinfo=UTC)} outbox = InMemoryLedgerOutbox(clock=lambda: now["t"]) ledger = InMemoryLedger(fail_n_then_succeed=1) writer = LedgerWriter(outbox, ledger, clock=lambda: now["t"]) diff --git a/tests/runtime/test_cli_ledger_backfill.py b/tests/runtime/test_cli_ledger_backfill.py index d13eee6..3c38c2c 100644 --- a/tests/runtime/test_cli_ledger_backfill.py +++ b/tests/runtime/test_cli_ledger_backfill.py @@ -147,8 +147,8 @@ def test_ledger_backfill_existing_completed_row_is_idempotent(monkeypatch, tmp_p def process_correlation_id(self, correlation_id): raise AssertionError("completed execution must not be re-processed") - import trex_core.adapters.persistence.postgresql as pg_mod import trex_core.adapters.ledger.portfolio_hub as hub_mod + import trex_core.adapters.persistence.postgresql as pg_mod monkeypatch.setattr(pg_mod, "PostgreSQLLedgerOutbox", lambda dsn: _Outbox()) monkeypatch.setattr(hub_mod, "PortfolioHubLedger", lambda *args, **kwargs: object()) @@ -195,8 +195,8 @@ def test_ledger_backfill_live_enqueue_and_processes_exact_trade(monkeypatch, tmp assert correlation_id == self.seen_outbox.trade.correlation_id return OutboxOutcome.COMPLETED - import trex_core.adapters.persistence.postgresql as pg_mod import trex_core.adapters.ledger.portfolio_hub as hub_mod + import trex_core.adapters.persistence.postgresql as pg_mod monkeypatch.setattr(pg_mod, "PostgreSQLLedgerOutbox", lambda dsn: outbox) monkeypatch.setattr(hub_mod, "PortfolioHubLedger", lambda *args, **kwargs: object()) diff --git a/trex_core/runtime/cli.py b/trex_core/runtime/cli.py index a41e6e9..c05eb83 100644 --- a/trex_core/runtime/cli.py +++ b/trex_core/runtime/cli.py @@ -38,8 +38,8 @@ import json as _json import re import sys import uuid -from pathlib import Path from datetime import datetime, timedelta, timezone +from pathlib import Path from zoneinfo import ZoneInfo from trex_core.adapters.messaging import topology @@ -394,7 +394,8 @@ def _write_ledger_backfill_audit(args, report: dict) -> str: def _lookup_ledger_backfill_evidence(cfg: DaemonConfig, *, trading_date: str, execution_id: str): from trex_core.runtime.broker_provider import ( - BrokerCredentialUnavailable, build_broker_provider, + BrokerCredentialUnavailable, + build_broker_provider, ) broker, source = build_broker_provider(cfg).get_broker_for_read() @@ -664,10 +665,11 @@ def _cmd_sync(args, cfg, confirm) -> int: return 1 # REQ-119/120: resolve and validate broker truth before opening/enumerating DB. - from trex_core.runtime.broker_provider import ( - BrokerCredentialUnavailable, build_broker_provider, - ) from trex_core.application.broker_port import BrokerBalanceError + from trex_core.runtime.broker_provider import ( + BrokerCredentialUnavailable, + build_broker_provider, + ) try: broker_port, _source = build_broker_provider(cfg).get_broker_for_read() broker_port = _CachedBalanceBroker(broker_port, broker_port.get_balance()) @@ -787,10 +789,11 @@ def _cmd_verify(args, cfg) -> int: instance = _resolve_instance(args) # REQ-120 no-write/no-enumeration barrier also applies to verify. - from trex_core.runtime.broker_provider import ( - BrokerCredentialUnavailable, build_broker_provider, - ) from trex_core.application.broker_port import BrokerBalanceError + from trex_core.runtime.broker_provider import ( + BrokerCredentialUnavailable, + build_broker_provider, + ) try: broker_port, _source = build_broker_provider(cfg).get_broker_for_read() broker_port = _CachedBalanceBroker(broker_port, broker_port.get_balance()) @@ -987,9 +990,9 @@ def _cmd_report(args, cfg) -> int: def _cmd_ledger_backfill(args, cfg, confirm) -> int: + from trex_core.adapters.broker.kis import KISEvidenceLookupError from trex_core.adapters.ledger.portfolio_hub import PortfolioHubLedger from trex_core.adapters.persistence.postgresql import PostgreSQLLedgerOutbox - from trex_core.adapters.broker.kis import KISEvidenceLookupError from trex_core.runtime.broker_provider import BrokerCredentialUnavailable instance = _resolve_instance(args)