fix: enforce actual venue persistence contract
This commit is contained in:
@@ -8,9 +8,14 @@ from __future__ import annotations
|
||||
|
||||
import psycopg
|
||||
import pytest
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from trex_core.adapters.persistence.migrate import apply_migrations, migration_files
|
||||
from trex_core.adapters.persistence.postgresql import PostgreSQLSlotRepository
|
||||
from trex_core.adapters.persistence.postgresql import (
|
||||
PostgreSQLLedgerOutbox,
|
||||
PostgreSQLSlotRepository,
|
||||
)
|
||||
from trex_core.application.ledger_port import LedgerTrade
|
||||
from trex_core.domain.slot import EXPECTED_SLOT_FIELDS
|
||||
|
||||
_EXPECTED_TABLES = {
|
||||
@@ -91,3 +96,64 @@ def test_migration_replay_converts_existing_auto_venue_row_to_sor(pg_clean):
|
||||
"UPDATE daily_slot SET venue = 'AUTO' WHERE slot_id = %s",
|
||||
(slot.slot_id,),
|
||||
)
|
||||
|
||||
|
||||
def test_migration_024_separates_legacy_sor_and_is_replay_safe(pg_clean):
|
||||
outbox = PostgreSQLLedgerOutbox(pg_clean)
|
||||
normal = LedgerTrade(
|
||||
asset_id="PF-NORMAL",
|
||||
order_id="O-NORMAL",
|
||||
execution_id="E-NORMAL",
|
||||
side="buy",
|
||||
qty=1,
|
||||
price=1000,
|
||||
executed_at=datetime(2026, 8, 19, tzinfo=timezone.utc),
|
||||
venue="NXT",
|
||||
order_venue="KRX",
|
||||
)
|
||||
legacy = LedgerTrade(
|
||||
asset_id="PF-LEGACY",
|
||||
order_id="O-LEGACY",
|
||||
execution_id="E-LEGACY",
|
||||
side="buy",
|
||||
qty=1,
|
||||
price=1000,
|
||||
executed_at=datetime(2026, 8, 19, tzinfo=timezone.utc),
|
||||
venue="KRX",
|
||||
order_venue="SOR",
|
||||
)
|
||||
outbox.enqueue(normal)
|
||||
outbox.enqueue(legacy)
|
||||
|
||||
# Recreate a pre-024 SOR row: one venue column carried routing provenance,
|
||||
# and no separate order_venue was available.
|
||||
with psycopg.connect(pg_clean) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("ALTER TABLE ledger_outbox DROP CONSTRAINT ledger_outbox_venue_check")
|
||||
cur.execute("ALTER TABLE ledger_outbox ALTER COLUMN order_venue DROP NOT NULL")
|
||||
cur.execute(
|
||||
"UPDATE ledger_outbox SET venue='SOR', order_venue=NULL "
|
||||
"WHERE correlation_id=%s",
|
||||
(legacy.correlation_id,),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
apply_migrations(pg_clean)
|
||||
apply_migrations(pg_clean)
|
||||
|
||||
normal_row = outbox.get(normal.correlation_id)
|
||||
legacy_row = outbox.get(legacy.correlation_id)
|
||||
assert normal_row is not None
|
||||
assert normal_row.trade.venue == "NXT"
|
||||
assert normal_row.trade.order_venue == "KRX"
|
||||
assert legacy_row is not None
|
||||
assert legacy_row.trade.venue is None
|
||||
assert legacy_row.trade.order_venue == "SOR"
|
||||
|
||||
with psycopg.connect(pg_clean) as conn:
|
||||
with conn.cursor() as cur:
|
||||
with pytest.raises(psycopg.errors.CheckViolation):
|
||||
cur.execute(
|
||||
"UPDATE ledger_outbox SET venue='SOR' WHERE correlation_id=%s",
|
||||
(normal.correlation_id,),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user