T-91: M1.1o — Security-master persist: remove the stale 1900-01-01 fallback provider_mapping row when a rerun supplies a real start date
This commit is contained in:
parent
297a7615bb
commit
50e6da57f0
5 changed files with 864 additions and 0 deletions
|
|
@ -219,6 +219,7 @@ describe('persistSecurityMaster unit tests', () => {
|
|||
expect(result.instrumentsInserted).toBe(0);
|
||||
expect(result.listingsInserted).toBe(0);
|
||||
expect(result.providerMappingsInserted).toBe(0);
|
||||
expect(result.providerMappingsDeletedStaleFallback).toBe(0);
|
||||
expect(result.overridesApplied).toBe(0);
|
||||
});
|
||||
|
||||
|
|
@ -960,4 +961,366 @@ describe('persistSecurityMaster unit tests', () => {
|
|||
).rejects.toThrow(TypeError);
|
||||
expect(queries.length).toBe(0);
|
||||
});
|
||||
|
||||
test('selects deduplicated dated-only stale-fallback targets with override guards and parameterized SQL', async () => {
|
||||
const hostileSourceId = "HOSTILE'ID;DELETE";
|
||||
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 validKey = {
|
||||
primaryIsin: 'US0378331005',
|
||||
mic: 'XNAS',
|
||||
tickerRoot: 'AAPL',
|
||||
classSuffix: '',
|
||||
currency: 'USD',
|
||||
};
|
||||
const missingKey = {
|
||||
primaryIsin: 'US0378331005',
|
||||
mic: 'XXXX',
|
||||
tickerRoot: 'MISS',
|
||||
classSuffix: '',
|
||||
currency: 'USD',
|
||||
};
|
||||
const stageB = emptyStageB();
|
||||
stageB.listings.push({
|
||||
...validKey,
|
||||
ibConid: null,
|
||||
ibPrimaryExchange: null,
|
||||
listedFrom: '2012-05-18',
|
||||
listedTo: null,
|
||||
});
|
||||
stageB.providerMappings.push(
|
||||
// Dated-only with two dated rows -> must appear ONCE in staleFallbackTargets
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'DATED_MULTI.US',
|
||||
listingKey: validKey,
|
||||
method: 'tier1_isin',
|
||||
validFrom: '2012-05-18',
|
||||
validTo: '2019-12-31',
|
||||
},
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'DATED_MULTI.US',
|
||||
listingKey: validKey,
|
||||
method: 'tier1_isin',
|
||||
validFrom: '2020-01-01',
|
||||
validTo: null,
|
||||
},
|
||||
// Dated-only with hostile sourceId -> must be parameterized, never interpolated
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: hostileSourceId,
|
||||
listingKey: validKey,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: '2015-01-02',
|
||||
validTo: null,
|
||||
},
|
||||
// Fallback-only (null) -> excluded
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'FALLBACK_NULL.US',
|
||||
listingKey: validKey,
|
||||
method: 'tier1_isin',
|
||||
validFrom: null,
|
||||
validTo: null,
|
||||
},
|
||||
// Fallback-only (explicit DEFAULT_VALID_FROM) -> excluded
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'FALLBACK_EXPLICIT.US',
|
||||
listingKey: validKey,
|
||||
method: 'tier1_isin',
|
||||
validFrom: DEFAULT_VALID_FROM,
|
||||
validTo: null,
|
||||
},
|
||||
// Mixed fallback-first then dated -> excluded
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'MIXED_FB_FIRST.US',
|
||||
listingKey: validKey,
|
||||
method: 'tier1_isin',
|
||||
validFrom: null,
|
||||
validTo: '2014-12-31',
|
||||
},
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'MIXED_FB_FIRST.US',
|
||||
listingKey: validKey,
|
||||
method: 'tier1_isin',
|
||||
validFrom: '2015-01-01',
|
||||
validTo: null,
|
||||
},
|
||||
// Mixed dated-first then fallback -> excluded
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'MIXED_DATED_FIRST.US',
|
||||
listingKey: validKey,
|
||||
method: 'tier1_isin',
|
||||
validFrom: '2015-01-01',
|
||||
validTo: null,
|
||||
},
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'MIXED_DATED_FIRST.US',
|
||||
listingKey: validKey,
|
||||
method: 'tier1_isin',
|
||||
validFrom: DEFAULT_VALID_FROM,
|
||||
validTo: '2014-12-31',
|
||||
},
|
||||
// Dated mapping whose listing is skipped (missing_listing) -> excluded
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'SKIPPED_LISTING.US',
|
||||
listingKey: missingKey,
|
||||
method: 'tier1_isin',
|
||||
validFrom: '2018-01-01',
|
||||
validTo: null,
|
||||
}
|
||||
);
|
||||
|
||||
const { client, queries } = 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')) {
|
||||
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,
|
||||
})),
|
||||
};
|
||||
}
|
||||
if (text.includes('DELETE FROM provider_mapping')) {
|
||||
return { rows: [{ deleted_count: 2 }] };
|
||||
}
|
||||
return { rows: [] };
|
||||
});
|
||||
|
||||
const result = await persistSecurityMaster(client, {
|
||||
stageA,
|
||||
stageB,
|
||||
securityTypeByIsin: new Map<string, SecurityType>([['US0378331005', 'common']]),
|
||||
});
|
||||
|
||||
const deleteQueries = queries.filter(q => q.text.includes('DELETE FROM provider_mapping'));
|
||||
expect(deleteQueries).toHaveLength(1);
|
||||
const dq = deleteQueries[0]!;
|
||||
expect(dq.text).toContain("pm.method <> 'override'");
|
||||
expect(dq.text).toContain('NOT EXISTS');
|
||||
expect(dq.text).toContain('FROM override o');
|
||||
expect(dq.text).not.toContain(hostileSourceId);
|
||||
expect(dq.values).toEqual([
|
||||
['eod', 'qm'],
|
||||
['DATED_MULTI.US', hostileSourceId],
|
||||
DEFAULT_VALID_FROM,
|
||||
]);
|
||||
|
||||
expect(result.providerMappingsDeletedStaleFallback).toBe(2);
|
||||
expect(result.providerMappingsInserted).toBe(9);
|
||||
expect(result.providerMappingsUpdated).toBe(0);
|
||||
expect(result.providerMappingsUnchanged).toBe(0);
|
||||
expect(result.providerMappingsSkipped).toBe(1);
|
||||
});
|
||||
|
||||
test('batches stale-fallback deletes by chunkSize and sums counts independently of upsert totals', 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: '2012-05-18',
|
||||
listedTo: null,
|
||||
});
|
||||
for (let i = 0; i < 5; i++) {
|
||||
stageB.providerMappings.push({
|
||||
source: 'eod',
|
||||
sourceId: `S${i}.US`,
|
||||
listingKey: key,
|
||||
method: 'tier1_isin',
|
||||
validFrom: '2012-05-18',
|
||||
validTo: null,
|
||||
});
|
||||
}
|
||||
|
||||
const chunkDeleteCounts = [1, 2, 0];
|
||||
let deleteChunkIdx = 0;
|
||||
const { client, queries } = 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')) {
|
||||
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,
|
||||
})),
|
||||
};
|
||||
}
|
||||
if (text.includes('DELETE FROM provider_mapping')) {
|
||||
const deleted = chunkDeleteCounts[deleteChunkIdx++] ?? 0;
|
||||
return { rows: [{ deleted_count: deleted }] };
|
||||
}
|
||||
return { rows: [] };
|
||||
});
|
||||
|
||||
const result = await persistSecurityMaster(
|
||||
client,
|
||||
{
|
||||
stageA,
|
||||
stageB,
|
||||
securityTypeByIsin: new Map<string, SecurityType>([['US0378331005', 'common']]),
|
||||
},
|
||||
{ chunkSize: 2 }
|
||||
);
|
||||
|
||||
const deleteQueries = queries.filter(q => q.text.includes('DELETE FROM provider_mapping'));
|
||||
expect(deleteQueries).toHaveLength(3);
|
||||
expect(deleteQueries.map(q => (q.values?.[1] as string[]).length)).toEqual([2, 2, 1]);
|
||||
expect(result.providerMappingsDeletedStaleFallback).toBe(3);
|
||||
expect(result.providerMappingsInserted).toBe(5);
|
||||
expect(result.providerMappingsUpdated).toBe(0);
|
||||
expect(result.providerMappingsUnchanged).toBe(0);
|
||||
});
|
||||
|
||||
test('issues zero DELETE queries when no dated-only provider_mapping pairs exist', 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: null,
|
||||
listedTo: null,
|
||||
});
|
||||
stageB.providerMappings.push(
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'AAPL.US',
|
||||
listingKey: key,
|
||||
method: 'tier1_isin',
|
||||
validFrom: null,
|
||||
validTo: null,
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'AAPL:US',
|
||||
listingKey: key,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: DEFAULT_VALID_FROM,
|
||||
validTo: '2014-12-31',
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'AAPL:US',
|
||||
listingKey: key,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: '2015-01-01',
|
||||
validTo: null,
|
||||
}
|
||||
);
|
||||
|
||||
const { client, queries } = createMockClient();
|
||||
const result = await persistSecurityMaster(client, {
|
||||
stageA,
|
||||
stageB,
|
||||
securityTypeByIsin: new Map<string, SecurityType>([['US0378331005', 'common']]),
|
||||
});
|
||||
|
||||
expect(queries.filter(q => q.text.includes('DELETE FROM provider_mapping'))).toHaveLength(0);
|
||||
expect(result.providerMappingsDeletedStaleFallback).toBe(0);
|
||||
expect(result.providerMappingsInserted).toBe(3);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue