T-91: tests
This commit is contained in:
parent
50e6da57f0
commit
4b51bea15a
2 changed files with 202 additions and 0 deletions
|
|
@ -1318,6 +1318,144 @@ describe.skipIf(!url)('persistSecurityMaster PostgreSQL integration', () => {
|
|||
]);
|
||||
});
|
||||
|
||||
test('stale-fallback deletes span multiple chunkSize batches in real PostgreSQL and roll back with the persist transaction', async () => {
|
||||
const f = await createIsolatedFixture();
|
||||
|
||||
const stageA = matchInstrumentsStageA([
|
||||
{
|
||||
source: 'eod',
|
||||
sourceId: 'A.US',
|
||||
isin: 'US0378331005',
|
||||
cusip: '037833100',
|
||||
country: 'US',
|
||||
currency: 'USD',
|
||||
name: 'Listing A Corp',
|
||||
},
|
||||
]);
|
||||
const keyA = {
|
||||
primaryIsin: 'US0378331005',
|
||||
mic: 'XNAS',
|
||||
tickerRoot: 'AAA',
|
||||
classSuffix: '',
|
||||
currency: 'USD',
|
||||
};
|
||||
const securityTypeByIsin = new Map<string, SecurityType>([['US0378331005', 'common']]);
|
||||
const makeStageB = (sourceIds: readonly string[], validFrom: string | null): StageBMatchResult => ({
|
||||
listings: [
|
||||
{ ...keyA, ibConid: null, ibPrimaryExchange: null, listedFrom: '2010-01-04', listedTo: null },
|
||||
],
|
||||
providerMappings: sourceIds.map(sourceId => ({
|
||||
source: 'eod' as const,
|
||||
sourceId,
|
||||
listingKey: keyA,
|
||||
method: 'tier1_isin' as const,
|
||||
validFrom,
|
||||
validTo: null,
|
||||
})),
|
||||
unresolved: [],
|
||||
conflicts: [],
|
||||
stats: {
|
||||
totalListings: 1,
|
||||
totalProviderMappings: sourceIds.length,
|
||||
listingsWithIbConid: 0,
|
||||
excludedFunds: 0,
|
||||
bySource: {
|
||||
eod: { total: sourceIds.length, matched: sourceIds.length, unresolved: 0, conflict: 0 },
|
||||
ib: { total: 0, matched: 0, unresolved: 0, conflict: 0 },
|
||||
qm: { total: 0, matched: 0, unresolved: 0, conflict: 0 },
|
||||
ceo: { total: 0, matched: 0, unresolved: 0, conflict: 0 },
|
||||
},
|
||||
},
|
||||
});
|
||||
const fallbackCount = async (): Promise<number> =>
|
||||
Number(
|
||||
(
|
||||
await f.pool.query<{ n: string }>(
|
||||
`SELECT count(*) AS n FROM provider_mapping WHERE valid_from = DATE '1900-01-01'`
|
||||
)
|
||||
).rows[0]!.n
|
||||
);
|
||||
|
||||
const rollbackIds = ['RB0.US', 'RB1.US', 'RB2.US'];
|
||||
const chunkIds = ['C0.US', 'C1.US', 'C2.US', 'C3.US', 'C4.US'];
|
||||
|
||||
// Seed 8 fallback rows.
|
||||
const seed = await withClient(f.pool, client =>
|
||||
persistSecurityMaster(client, {
|
||||
stageA,
|
||||
stageB: makeStageB([...rollbackIds, ...chunkIds], null),
|
||||
securityTypeByIsin,
|
||||
})
|
||||
);
|
||||
expect(seed.providerMappingsInserted).toBe(8);
|
||||
expect(await fallbackCount()).toBe(8);
|
||||
const listingAId = seed.listingIdByKey.get(formatListingNaturalKey(keyA))!;
|
||||
|
||||
// Atomicity: make the override-enforcement step (which runs after the stale-fallback DELETE)
|
||||
// fail inside the same transaction; the deletes must roll back with it.
|
||||
await f.pool.query(
|
||||
`CREATE FUNCTION reject_override_write() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN RAISE EXCEPTION 'qa: forced failure after stale-fallback delete'; END $$`
|
||||
);
|
||||
await f.pool.query(
|
||||
`CREATE TRIGGER reject_override_write BEFORE INSERT ON provider_mapping
|
||||
FOR EACH ROW WHEN (NEW.method = 'override') EXECUTE FUNCTION reject_override_write()`
|
||||
);
|
||||
await f.pool.query(
|
||||
`INSERT INTO override (source, source_id, valid_from, valid_to, listing_id, reason, created_by)
|
||||
VALUES ('eod', 'TRIGGER_ONLY.US', DATE '1900-01-01', NULL, $1, 'qa forced failure', 'qa')`,
|
||||
[listingAId]
|
||||
);
|
||||
await expect(
|
||||
withClient(f.pool, client =>
|
||||
persistSecurityMaster(
|
||||
client,
|
||||
{ stageA, stageB: makeStageB(rollbackIds, '2012-05-18'), securityTypeByIsin },
|
||||
{ chunkSize: 2 }
|
||||
)
|
||||
)
|
||||
).rejects.toThrow('qa: forced failure after stale-fallback delete');
|
||||
expect(await fallbackCount()).toBe(8);
|
||||
expect(
|
||||
Number(
|
||||
(
|
||||
await f.pool.query<{ n: string }>(
|
||||
`SELECT count(*) AS n FROM provider_mapping WHERE valid_from = DATE '2012-05-18'`
|
||||
)
|
||||
).rows[0]!.n
|
||||
)
|
||||
).toBe(0);
|
||||
|
||||
await f.pool.query(`DROP TRIGGER reject_override_write ON provider_mapping`);
|
||||
await f.pool.query(`DELETE FROM override WHERE source_id = 'TRIGGER_ONLY.US'`);
|
||||
// Removing the override row leaves its 1900-01-01 provider_mapping row absent (never written).
|
||||
|
||||
// Chunking: 5 dated-only pairs at chunkSize 2 -> 3 DELETE batches whose counts sum to 5.
|
||||
const chunked = await withClient(f.pool, client =>
|
||||
persistSecurityMaster(
|
||||
client,
|
||||
{ stageA, stageB: makeStageB(chunkIds, '2012-05-18'), securityTypeByIsin },
|
||||
{ chunkSize: 2 }
|
||||
)
|
||||
);
|
||||
expect(chunked.providerMappingsDeletedStaleFallback).toBe(5);
|
||||
expect(chunked.providerMappingsInserted).toBe(5);
|
||||
expect(chunked.providerMappingsUpdated).toBe(0);
|
||||
expect(chunked.providerMappingsUnchanged).toBe(0);
|
||||
|
||||
const remaining = (
|
||||
await f.pool.query<{ source_id: string; valid_from: string }>(
|
||||
`SELECT source_id, to_char(valid_from, 'YYYY-MM-DD') AS valid_from
|
||||
FROM provider_mapping
|
||||
ORDER BY source_id, valid_from`
|
||||
)
|
||||
).rows;
|
||||
expect(remaining).toEqual([
|
||||
...chunkIds.map(source_id => ({ source_id, valid_from: '2012-05-18' })),
|
||||
...rollbackIds.map(source_id => ({ source_id, valid_from: '1900-01-01' })),
|
||||
]);
|
||||
});
|
||||
|
||||
test('persists Stage A, Stage B, and Pass 6 conflicts once and never reopens or modifies non-open rows', async () => {
|
||||
const f = await createIsolatedFixture();
|
||||
const fixture = buildExitCriterionFixture();
|
||||
|
|
|
|||
|
|
@ -1323,4 +1323,68 @@ describe('persistSecurityMaster unit tests', () => {
|
|||
expect(result.providerMappingsDeletedStaleFallback).toBe(0);
|
||||
expect(result.providerMappingsInserted).toBe(3);
|
||||
});
|
||||
|
||||
test('rolls back instead of committing when a step after the stale-fallback DELETE fails', 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,
|
||||
});
|
||||
stageB.providerMappings.push({
|
||||
source: 'eod',
|
||||
sourceId: 'AAPL.US',
|
||||
listingKey: key,
|
||||
method: 'tier1_isin',
|
||||
validFrom: '2012-05-18',
|
||||
validTo: null,
|
||||
});
|
||||
|
||||
const base = createMockClient();
|
||||
const queries: RecordedQuery[] = [];
|
||||
const client: SecurityMasterQueryable = {
|
||||
release: () => undefined,
|
||||
async query<Row = Record<string, unknown>>(text: string, values?: readonly unknown[]) {
|
||||
if (text.includes('WITH enforced AS')) {
|
||||
queries.push({ text, values });
|
||||
throw new Error('override enforcement failed');
|
||||
}
|
||||
const res = await base.client.query<Row>(text, values);
|
||||
queries.push({ text, values });
|
||||
return res;
|
||||
},
|
||||
};
|
||||
|
||||
await expect(
|
||||
persistSecurityMaster(client, {
|
||||
stageA,
|
||||
stageB,
|
||||
securityTypeByIsin: new Map<string, SecurityType>([['US0378331005', 'common']]),
|
||||
})
|
||||
).rejects.toThrow('override enforcement failed');
|
||||
|
||||
const texts = queries.map(q => q.text.trim());
|
||||
const deleteIdx = texts.findIndex(t => t.includes('DELETE FROM provider_mapping'));
|
||||
expect(texts[0]).toBe('BEGIN');
|
||||
expect(deleteIdx).toBeGreaterThan(0);
|
||||
expect(texts.at(-1)).toBe('ROLLBACK');
|
||||
expect(texts).not.toContain('COMMIT');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue