T-40: M1.1c — Pass 1 exchanges: ISO MIC seed, source-code role classifier and reviewable seed-SQL generator

This commit is contained in:
Studio 2026-11-27 14:56:52 -05:00
parent 2dbfb7e5bc
commit c36f89352c
12 changed files with 1848 additions and 20 deletions

View file

@ -85,6 +85,37 @@ async function expectPgErrorCode(operation: Promise<unknown>, expectedCode: stri
throw new Error(`Expected PostgreSQL error code ${expectedCode}, but query succeeded`);
}
async function ensureExchange(
pool: Pool,
mic: string,
overrides: {
operatingMic?: string;
name?: string;
countryIso?: string;
currency?: string;
timezone?: string;
calendarCode?: string;
boardLotPolicy?: string;
} = {}
): Promise<void> {
await pool.query(
`INSERT INTO exchange (
mic, operating_mic, name, country_iso, currency, timezone, calendar_code, board_lot_policy
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (mic) DO NOTHING`,
[
mic,
overrides.operatingMic ?? mic,
overrides.name ?? `${mic} Exchange`,
overrides.countryIso ?? 'US',
overrides.currency ?? 'USD',
overrides.timezone ?? 'America/New_York',
overrides.calendarCode ?? mic,
overrides.boardLotPolicy ?? 'us_100',
]
);
}
async function seedExchange(
pool: Pool,
mic: string,
@ -144,24 +175,25 @@ describe.skipIf(!url)('security master PostgreSQL schema', () => {
test('accepts valid insert chain across all 11 tables and populates defaults', async () => {
const f = await createIsolatedFixture();
// 1. exchange (omitting board_lot_policy to verify default 'us_100')
// 1. exchange (omitting board_lot_policy on an unseeded synthetic MIC to verify default 'us_100')
const exchangeRow = (
await f.pool.query(
`INSERT INTO exchange (mic, operating_mic, name, country_iso, currency, timezone, calendar_code)
VALUES ('XNYS', 'XNYS', 'New York Stock Exchange', 'US', 'USD', 'America/New_York', 'XNYS')
VALUES ('XTST', 'XTST', 'Synthetic Test Exchange', 'US', 'USD', 'America/New_York', 'XNYS')
RETURNING board_lot_policy`
)
).rows[0];
expect(exchangeRow.board_lot_policy).toBe('us_100');
await seedExchange(f.pool, 'OTCM', { name: 'OTC Markets' });
await ensureExchange(f.pool, 'XNYS', { name: 'New York Stock Exchange' });
await ensureExchange(f.pool, 'OTCM', { name: 'OTC Markets' });
// 2. exchange_source_map (primary_venue with mic, and composite with NULL mic)
await f.pool.query(
`INSERT INTO exchange_source_map (source, source_code, role, mic, country_iso, method)
VALUES
('eod', 'NYSE', 'primary_venue', 'XNYS', 'US', 'manual_seed'),
('eod', 'US', 'composite', NULL, 'US', 'manual_seed')`
('eod', 'TEST_COMP', 'composite', NULL, 'US', 'manual_seed')`
);
// 3. instrument
@ -269,7 +301,7 @@ describe.skipIf(!url)('security master PostgreSQL schema', () => {
test('rejects foreign key violations (23503)', async () => {
const f = await createIsolatedFixture();
await seedExchange(f.pool, 'XNYS');
await ensureExchange(f.pool, 'XNYS');
const instrumentId = (
await f.pool.query(
`INSERT INTO instrument (canonical_name, security_type)
@ -330,7 +362,7 @@ describe.skipIf(!url)('security master PostgreSQL schema', () => {
test('rejects unique and primary key violations (23505) while allowing distinct dates and NULL ISINs', async () => {
const f = await createIsolatedFixture();
await seedExchange(f.pool, 'XNYS');
await ensureExchange(f.pool, 'XNYS');
const instrumentId = (
await f.pool.query(
@ -409,7 +441,7 @@ describe.skipIf(!url)('security master PostgreSQL schema', () => {
test('rejects CHECK constraint violations (23514) and accepts valid boundary values', async () => {
const f = await createIsolatedFixture();
await seedExchange(f.pool, 'XNYS');
await ensureExchange(f.pool, 'XNYS');
const instrumentId = (
await f.pool.query(
`INSERT INTO instrument (canonical_name, security_type)
@ -531,15 +563,15 @@ describe.skipIf(!url)('security master PostgreSQL schema', () => {
test('allows interlisted listings on different mics and ticker reuse across distinct instruments', async () => {
const f = await createIsolatedFixture();
await seedExchange(f.pool, 'XTSE', {
await ensureExchange(f.pool, 'XTSE', {
name: 'Toronto Stock Exchange',
countryIso: 'CA',
currency: 'CAD',
timezone: 'America/Toronto',
boardLotPolicy: 'tsx_tiered',
});
await seedExchange(f.pool, 'XNYS');
await seedExchange(f.pool, 'XNAS', { name: 'NASDAQ' });
await ensureExchange(f.pool, 'XNYS');
await ensureExchange(f.pool, 'XNAS', { name: 'NASDAQ' });
// Interlisted Shopify: one instrument, two listings on XTSE (CAD) and XNYS (USD)
const shopInstrumentId = (