From 86bf0a68b21bb44415ac7f22df18f9f5d22b7319 Mon Sep 17 00:00:00 2001 From: Studio Date: Fri, 27 Nov 2026 21:23:56 -0500 Subject: [PATCH] =?UTF-8?q?T-74:=20M1.1i=20=E2=80=94=20Pass=203/4/6=20matc?= =?UTF-8?q?h=20&=20conflict=20report:=20pure=20in-memory=20Markdown=20+=20?= =?UTF-8?q?CSV=20generator=20and=20`override`=20seed-SQL=20template=20(no?= =?UTF-8?q?=20DB=20I/O)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/security-master/match-report.ts | 4 +- .../security-master-override-seed.test.ts | 80 +++++++++++++++++++ .../unit/security-master-match-report.test.ts | 5 +- 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/apps/stock/data-pipeline/src/security-master/match-report.ts b/apps/stock/data-pipeline/src/security-master/match-report.ts index f0261c9e..6ab8b082 100644 --- a/apps/stock/data-pipeline/src/security-master/match-report.ts +++ b/apps/stock/data-pipeline/src/security-master/match-report.ts @@ -1116,11 +1116,11 @@ export function generateOverrideTemplateSql(report: MatchReport): string { const sourceSqlLiteral = sourceIssues.length === 0 ? formatSingleLineSqlLiteral(conflict.source) - : "'TODO_CORRECT_SOURCE'"; + : 'NULL /* TODO: replace with valid source */'; const sourceIdSqlLiteral = sourceIdIssues.length === 0 ? formatSingleLineSqlLiteral(conflict.sourceId) - : "'TODO_CORRECT_SOURCE_ID'"; + : 'NULL /* TODO: replace with valid source_id */'; const blockLines: string[] = [ `Conflict: pass=${commentPass} kind=${commentKind} source=${commentSource} source_id=${commentSourceId}`, diff --git a/test/postgres/security-master-override-seed.test.ts b/test/postgres/security-master-override-seed.test.ts index e2aee480..12b58aa5 100644 --- a/test/postgres/security-master-override-seed.test.ts +++ b/test/postgres/security-master-override-seed.test.ts @@ -339,4 +339,84 @@ describe.skipIf(!url)('security master override seed SQL', () => { ).rows; expect(inserted).toEqual([{ source_id: rawSourceId, listing_id: expectedListingId }]); }); + + test('uncommented template candidate with unstorable source_id fails not_null_violation and inserts no row when marker is left in place', async () => { + const f = await createIsolatedFixture(); + await seedSingleListing(f.pool); + const overlongSourceId = 'X'.repeat(65); + + const report = buildMatchReport({ + stageA: { + instruments: [], + unresolved: [], + conflicts: [ + { + source: 'eod', + sourceId: overlongSourceId, + 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 templateLines = templateSql.split('\n'); + const insertStartIndex = templateLines.findIndex( + line => + line === + '-- INSERT INTO override (source, source_id, valid_from, valid_to, listing_id, reason, created_by)' + ); + expect(insertStartIndex).toBeGreaterThan(-1); + + const candidateSql = templateLines + .slice(insertStartIndex) + .filter(line => line.startsWith('-- ')) + .map(line => line.slice(3)) + .join('\n') + .replace("'TODO_ISIN_12'", "'US0378331005'") + .replace("'TODO_MIC'", "'XNAS'") + .replace("'TODO_TICKER'", "'AAPL'"); + + let caughtError: unknown; + try { + await f.pool.query(candidateSql); + } catch (err) { + caughtError = err; + } + + expect(caughtError).toBeDefined(); + expect((caughtError as { code?: string }).code).toBe('23502'); + + const count = ( + await f.pool.query<{ count: number }>('SELECT count(*)::int AS count FROM override') + ).rows[0]?.count; + expect(count).toBe(0); + }); }); diff --git a/test/unit/security-master-match-report.test.ts b/test/unit/security-master-match-report.test.ts index 7683b870..d60e1b80 100644 --- a/test/unit/security-master-match-report.test.ts +++ b/test/unit/security-master-match-report.test.ts @@ -1037,11 +1037,12 @@ describe('generateOverrideSeedSql & generateOverrideTemplateSql', () => { // CR/LF source_id is preserved via a single-line PostgreSQL E'...' literal expect(templateSql).toContain("-- E'BAD\\r\\nDROP''\\\\TABLE override;--',"); - // Unstorable NUL-byte source_id gets a diagnostic warning and placeholder instead of silent mangling + // Unstorable NUL-byte source_id gets a diagnostic warning and non-insertable NULL marker instead of silent mangling expect(templateSql).toContain( '-- WARNING: Cannot store raw source_id in PostgreSQL (contains NUL byte); correct source_id before applying.' ); - expect(templateSql).toContain("-- 'TODO_CORRECT_SOURCE_ID',"); + expect(templateSql).toContain('-- NULL /* TODO: replace with valid source_id */,'); + expect(templateSql).not.toContain('TODO_CORRECT_SOURCE_ID'); // Empty conflicts report still produces only comments const emptyTemplate = generateOverrideTemplateSql(