T-78: M1.1j — Persist Pass 3/5/6 results into the security master: idempotent PostgreSQL loader with permanent listing_ids and override precedence
This commit is contained in:
parent
d68577843e
commit
3ca206150e
3 changed files with 680 additions and 80 deletions
|
|
@ -119,11 +119,26 @@ function createMockClient(
|
|||
})),
|
||||
} as unknown as SecurityMasterQueryResult<Row>;
|
||||
}
|
||||
if (
|
||||
text.startsWith('INSERT INTO listing_symbol_history') ||
|
||||
text.startsWith('INSERT INTO provider_mapping') ||
|
||||
text.startsWith('INSERT INTO corporate_action_continuity')
|
||||
) {
|
||||
if (text.includes('INTO listing_symbol_history')) {
|
||||
const firstCol = (values?.[0] as unknown[]) ?? [];
|
||||
return {
|
||||
rows: [{ inserted_count: firstCol.length, updated_count: 0 }],
|
||||
} as unknown as SecurityMasterQueryResult<Row>;
|
||||
}
|
||||
if (text.startsWith('INSERT INTO provider_mapping')) {
|
||||
const sources = (values?.[0] as string[]) ?? [];
|
||||
const sourceIds = (values?.[1] as string[]) ?? [];
|
||||
const validFroms = (values?.[2] as string[]) ?? [];
|
||||
return {
|
||||
rows: sources.map((source, i) => ({
|
||||
source,
|
||||
source_id: sourceIds[i],
|
||||
valid_from: validFroms[i],
|
||||
inserted: true,
|
||||
})),
|
||||
} as unknown as SecurityMasterQueryResult<Row>;
|
||||
}
|
||||
if (text.startsWith('INSERT INTO corporate_action_continuity')) {
|
||||
const firstCol = (values?.[0] as unknown[]) ?? [];
|
||||
return {
|
||||
rows: firstCol.map(() => ({ inserted: true })),
|
||||
|
|
@ -131,7 +146,7 @@ function createMockClient(
|
|||
}
|
||||
if (text.includes('WITH enforced AS')) {
|
||||
return {
|
||||
rows: [{ total_overrides: 0, inserted_overrides: 0, updated_overrides: 0 }],
|
||||
rows: [],
|
||||
} as unknown as SecurityMasterQueryResult<Row>;
|
||||
}
|
||||
return { rows: [] } as SecurityMasterQueryResult<Row>;
|
||||
|
|
@ -631,4 +646,159 @@ describe('persistSecurityMaster unit tests', () => {
|
|||
currency: 'USD',
|
||||
})).toBe('US0378331005\u0000XNAS\u0000AAPL\u0000\u0000USD');
|
||||
});
|
||||
|
||||
test('rejects overlapping calls on the same client before issuing BEGIN and allows sequential reuse', async () => {
|
||||
let releaseFirstBegin!: () => void;
|
||||
const firstBeginPaused = new Promise<void>(resolve => {
|
||||
releaseFirstBegin = resolve;
|
||||
});
|
||||
let beginCount = 0;
|
||||
|
||||
const { client, queries } = createMockClient(async text => {
|
||||
if (text === 'BEGIN') {
|
||||
beginCount += 1;
|
||||
if (beginCount === 1) {
|
||||
await firstBeginPaused;
|
||||
}
|
||||
}
|
||||
return { rows: [] };
|
||||
});
|
||||
|
||||
const emptyInput: PersistSecurityMasterInput = {
|
||||
stageA: emptyStageA(),
|
||||
stageB: emptyStageB(),
|
||||
securityTypeByIsin: new Map(),
|
||||
};
|
||||
|
||||
const firstCall = persistSecurityMaster(client, emptyInput);
|
||||
const secondCall = persistSecurityMaster(client, emptyInput);
|
||||
|
||||
await expect(secondCall).rejects.toThrow(/overlapping calls on the same client/);
|
||||
// Second call must reject before issuing a second BEGIN.
|
||||
expect(queries.filter(q => q.text === 'BEGIN')).toHaveLength(1);
|
||||
|
||||
releaseFirstBegin();
|
||||
const firstResult = await firstCall;
|
||||
expect(firstResult.instrumentsInserted).toBe(0);
|
||||
|
||||
// Once the first call completes, sequential reuse of the same client succeeds.
|
||||
const thirdResult = await persistSecurityMaster(client, emptyInput);
|
||||
expect(thirdResult.instrumentsInserted).toBe(0);
|
||||
expect(queries.filter(q => q.text === 'BEGIN')).toHaveLength(2);
|
||||
});
|
||||
|
||||
test('counts each provider_mapping key once across matcher and override outcomes', async () => {
|
||||
const stageA = emptyStageA();
|
||||
stageA.instruments.push({
|
||||
primaryIsin: 'US0378331005',
|
||||
primaryCusip: '037833100',
|
||||
issuerCountry: 'US',
|
||||
canonicalName: 'Apple Inc.',
|
||||
members: [{ source: 'eod', sourceId: 'AAPL.US', currency: 'USD', method: 'tier1_isin' }],
|
||||
});
|
||||
const key = {
|
||||
primaryIsin: 'US0378331005',
|
||||
mic: 'XNAS',
|
||||
tickerRoot: 'AAPL',
|
||||
classSuffix: '',
|
||||
currency: 'USD',
|
||||
};
|
||||
const stageB = emptyStageB();
|
||||
stageB.listings.push({
|
||||
...key,
|
||||
ibConid: null,
|
||||
ibPrimaryExchange: null,
|
||||
listedFrom: '1980-12-12',
|
||||
listedTo: null,
|
||||
});
|
||||
stageB.providerMappings.push(
|
||||
// Existing row skipped by matcher because override exists, then updated by override
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'AAPL.US',
|
||||
listingKey: key,
|
||||
method: 'tier1_isin',
|
||||
validFrom: '1980-12-12',
|
||||
validTo: null,
|
||||
},
|
||||
// Fresh row inserted by matcher, then immediately updated by override -> net outcome: inserted
|
||||
{
|
||||
source: 'ib',
|
||||
sourceId: '265598',
|
||||
listingKey: key,
|
||||
method: 'tier1_isin',
|
||||
validFrom: '1980-12-12',
|
||||
validTo: null,
|
||||
},
|
||||
// Existing row unchanged by matcher and not in override -> unchanged
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'AAPL:US',
|
||||
listingKey: key,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: '2010-01-04',
|
||||
validTo: null,
|
||||
}
|
||||
);
|
||||
|
||||
const { client } = createMockClient((text, values) => {
|
||||
if (text.startsWith('SELECT mic FROM exchange')) {
|
||||
return { rows: [{ mic: 'XNAS' }] };
|
||||
}
|
||||
if (text.startsWith('INSERT INTO instrument (')) {
|
||||
return { rows: [{ instrument_id: 1, primary_isin: 'US0378331005' }] };
|
||||
}
|
||||
if (text.startsWith('INSERT INTO instrument_identifier_history')) {
|
||||
const ids = (values?.[0] as number[]) ?? [];
|
||||
return { rows: ids.map(() => ({ inserted: 1 })) };
|
||||
}
|
||||
if (text.startsWith('INSERT INTO listing (')) {
|
||||
return {
|
||||
rows: [
|
||||
{
|
||||
listing_id: 10,
|
||||
instrument_id: 1,
|
||||
mic: 'XNAS',
|
||||
ticker_root: 'AAPL',
|
||||
class_suffix: '',
|
||||
currency: 'USD',
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
if (text.includes('INTO listing_symbol_history')) {
|
||||
return { rows: [{ inserted_count: 1, updated_count: 0 }] };
|
||||
}
|
||||
if (text.startsWith('INSERT INTO provider_mapping')) {
|
||||
// Only the fresh ib row is inserted by matcher; eod is skipped by override guard; qm unchanged.
|
||||
return {
|
||||
rows: [
|
||||
{ source: 'ib', source_id: '265598', valid_from: '1980-12-12', inserted: true },
|
||||
],
|
||||
};
|
||||
}
|
||||
if (text.includes('WITH enforced AS')) {
|
||||
return {
|
||||
rows: [
|
||||
{ source: 'eod', source_id: 'AAPL.US', valid_from: '1980-12-12', inserted: false },
|
||||
{ source: 'ib', source_id: '265598', valid_from: '1980-12-12', inserted: false },
|
||||
{ source: 'qm', source_id: 'OVR_NEW:US', valid_from: '2021-01-01', inserted: true },
|
||||
{ source: 'qm', source_id: 'OVR_SAME:US', valid_from: '2021-01-01', inserted: null },
|
||||
],
|
||||
};
|
||||
}
|
||||
return { rows: [] };
|
||||
});
|
||||
|
||||
const result = await persistSecurityMaster(client, {
|
||||
stageA,
|
||||
stageB,
|
||||
securityTypeByIsin: new Map<string, SecurityType>([['US0378331005', 'common']]),
|
||||
});
|
||||
|
||||
expect(result.overridesApplied).toBe(4);
|
||||
expect(result.providerMappingsInserted).toBe(2); // ib (fresh+overridden) + OVR_NEW
|
||||
expect(result.providerMappingsUpdated).toBe(1); // eod (existing+overridden)
|
||||
expect(result.providerMappingsUnchanged).toBe(2); // qm AAPL:US + OVR_SAME
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue