T-74: M1.1i — Pass 3/4/6 match & conflict report: pure in-memory Markdown + CSV generator and override seed-SQL template (no DB I/O)

This commit is contained in:
Studio 2026-11-27 21:18:56 -05:00
parent cbabb17094
commit 83ec301220
3 changed files with 251 additions and 43 deletions

View file

@ -166,6 +166,56 @@ describe.skipIf(!url)('security master override seed SQL', () => {
expect(count).toBe(0);
});
test('duplicate natural key raises cardinality_violation and inserts no override row', async () => {
const f = await createIsolatedFixture();
await seedSingleListing(f.pool);
// Insert a second listing row with the same natural key (e.g., different listing dates)
const instrumentResult = await f.pool.query<{ instrument_id: number }>(
`SELECT instrument_id FROM instrument WHERE primary_isin = 'US0378331005'`
);
const instrumentId = instrumentResult.rows[0]?.instrument_id;
if (instrumentId === undefined) {
throw new Error('Expected fixture instrument');
}
await f.pool.query(
`INSERT INTO listing (instrument_id, mic, ticker_root, class_suffix, currency, listed_from)
VALUES ($1, 'XNAS', 'AAPL', '', 'USD', DATE '2022-01-03')`,
[instrumentId]
);
const sql = generateOverrideSeedSql([
{
source: 'eod',
sourceId: 'AAPL.US',
validFrom: '2020-01-15',
validTo: null,
primaryIsin: 'US0378331005',
mic: 'XNAS',
tickerRoot: 'AAPL',
classSuffix: '',
currency: 'USD',
reason: 'Ambiguous natural key test',
createdBy: 'director',
},
]);
let caughtError: unknown;
try {
await f.pool.query(sql);
} catch (err) {
caughtError = err;
}
expect(caughtError).toBeDefined();
expect((caughtError as { code?: string }).code).toBe('21000');
const count = (
await f.pool.query<{ count: number }>('SELECT count(*)::int AS count FROM override')
).rows[0]?.count;
expect(count).toBe(0);
});
test('empty seed SQL and template SQL execute cleanly without inserting rows', async () => {
const f = await createIsolatedFixture();
await seedSingleListing(f.pool);
@ -222,4 +272,71 @@ describe.skipIf(!url)('security master override seed SQL', () => {
).rows[0]?.count;
expect(count).toBe(0);
});
test('uncommented template candidate with CR/LF source_id preserves the exact ID in PostgreSQL', async () => {
const f = await createIsolatedFixture();
const expectedListingId = await seedSingleListing(f.pool);
const rawSourceId = "AAPL\r\nLINE2'\\END";
const report = buildMatchReport({
stageA: {
instruments: [],
unresolved: [],
conflicts: [
{
source: 'eod',
sourceId: rawSourceId,
conflictKind: 'isin_cusip_mismatch',
detail: { isin: 'US0378331005', cusip: '037833100', country: 'US', derivedIsin: null },
},
],
stats: {
totalInputRecords: 1,
totalInstruments: 0,
bySource: {
ib: { total: 0, tier1Isin: 0, tier2Cusip: 0, unresolved: 0, conflict: 0 },
eod: { total: 1, tier1Isin: 0, tier2Cusip: 0, unresolved: 0, conflict: 1 },
},
},
},
stageB: {
listings: [],
providerMappings: [],
unresolved: [],
conflicts: [],
stats: {
totalListings: 0,
totalProviderMappings: 0,
listingsWithIbConid: 0,
excludedFunds: 0,
bySource: {
eod: { total: 0, matched: 0, 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 templateSql = generateOverrideTemplateSql(report);
const candidateLines = templateSql
.split('\n')
.slice(templateSql.split('\n').findIndex(line => line === '-- INSERT INTO override (source, source_id, valid_from, valid_to, listing_id, reason, created_by)'))
.filter(line => line.startsWith('-- '))
.map(line => line.slice(3))
.join('\n')
.replace("'TODO_ISIN_12'", "'US0378331005'")
.replace("'TODO_MIC'", "'XNAS'")
.replace("'TODO_TICKER'", "'AAPL'");
await f.pool.query(candidateLines);
const inserted = (
await f.pool.query<{ source_id: string; listing_id: number }>(
'SELECT source_id, listing_id FROM override'
)
).rows;
expect(inserted).toEqual([{ source_id: rawSourceId, listing_id: expectedListingId }]);
});
});