Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 48 additions & 37 deletions versions/18/test/snapshots.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ const fs = require('node:fs');
const path = require('node:path');
const lib = require('../wasm/index.cjs');

// corpus.json is pgsql-parser's __fixtures__/generated/generated.json: every
// kitchen-sink statement by name. pg18.json holds what this build returns for
// each one: the fingerprint, then a hash of the parse tree, of the normalized
// query and of the scan tokens. An output that changes fails here; when the
// change is intended, regenerate with
// Each corpus is copied from pgsql-parser's __fixtures__ and maps a statement
// name to its SQL; the snapshot beside it holds what this build returns for
// each one. An output that changes fails here; when the change is intended,
// regenerate with
// UPDATE_SNAPSHOTS=1 node --test test/snapshots.test.js
// and say in the release notes what changed for users.
const corpus = require('./snapshots/corpus.json');
const snapshotFile = path.join(__dirname, 'snapshots', 'pg18.json');
const COLUMNS = ['fingerprint', 'parse', 'normalize', 'scan'];

//
// corpus.json (generated/generated.json, every kitchen-sink statement) ->
// pg18.json: the fingerprint, then a hash of the parse tree, of the
// normalized query and of the scan tokens.
// plpgsql-corpus.json (plpgsql-generated/generated.json) ->
// plpgsql-pg18.json: the parsePlPgSQL result itself.
const dir = path.join(__dirname, 'snapshots');
const hash = (text) => createHash('sha256').update(text).digest('hex').slice(0, 12);
const attempt = (run) => {
try {
Expand All @@ -26,38 +28,47 @@ const attempt = (run) => {
};
const withoutVersion = ({ version, ...rest }) => JSON.stringify(rest);

function outputs(sql) {
return [
attempt(() => lib.fingerprintSync(sql)),
attempt(() => hash(withoutVersion(lib.parseSync(sql)))),
attempt(() => hash(lib.normalizeSync(sql))),
attempt(() => hash(withoutVersion(lib.scanSync(sql)))),
];
const SQL = {
fingerprint: (sql) => lib.fingerprintSync(sql),
parse: (sql) => hash(withoutVersion(lib.parseSync(sql))),
normalize: (sql) => hash(lib.normalizeSync(sql)),
scan: (sql) => hash(withoutVersion(lib.scanSync(sql))),
};
const PLPGSQL = {
parsePlPgSQL: (sql) => lib.parsePlPgSQLSync(sql),
};

function check(corpusFile, snapshotFile, fns) {
const corpus = JSON.parse(fs.readFileSync(path.join(dir, corpusFile), 'utf8'));
const actual = {};
for (const [name, sql] of Object.entries(corpus)) {
actual[name] = Object.values(fns).map((fn) => attempt(() => fn(sql)));
}

if (process.env.UPDATE_SNAPSHOTS) {
const lines = Object.entries(actual).map(([name, row]) => `${JSON.stringify(name)}: ${JSON.stringify(row)}`);
fs.writeFileSync(path.join(dir, snapshotFile), `{\n${lines.join(',\n')}\n}\n`);
return;
}

const expected = JSON.parse(fs.readFileSync(path.join(dir, snapshotFile), 'utf8'));
assert.deepEqual(Object.keys(actual), Object.keys(expected), `${corpusFile} and ${snapshotFile} list different statements`);
const text = (value) => (typeof value === 'string' ? value : JSON.stringify(value));
const changed = [];
for (const [name, row] of Object.entries(actual)) {
Object.keys(fns).forEach((fn, i) => {
const [was, now] = [text(expected[name][i]), text(row[i])];
if (was !== now) changed.push(`${name} ${fn}: ${was.slice(0, 200)} -> ${now.slice(0, 200)}`);
});
}
assert.equal(changed.length, 0, `${changed.length} outputs changed:\n${changed.slice(0, 50).join('\n')}`);
}

describe('Outputs for every kitchen-sink statement', { timeout: 300000 }, () => {
describe('Committed outputs', { timeout: 300000 }, () => {
before(async () => {
await lib.loadModule();
});

it('match the committed snapshot', () => {
const actual = {};
for (const [name, sql] of Object.entries(corpus)) actual[name] = outputs(sql);

if (process.env.UPDATE_SNAPSHOTS) {
const lines = Object.entries(actual).map(([name, row]) => `${JSON.stringify(name)}: ${JSON.stringify(row)}`);
fs.writeFileSync(snapshotFile, `{\n${lines.join(',\n')}\n}\n`);
return;
}

const expected = JSON.parse(fs.readFileSync(snapshotFile, 'utf8'));
assert.deepEqual(Object.keys(actual), Object.keys(expected), 'corpus.json and pg18.json list different statements');
const changed = [];
for (const [name, row] of Object.entries(actual)) {
row.forEach((value, i) => {
if (value !== expected[name][i]) changed.push(`${name} ${COLUMNS[i]}: ${expected[name][i]} -> ${value}`);
});
}
assert.equal(changed.length, 0, `${changed.length} outputs changed:\n${changed.slice(0, 50).join('\n')}`);
});
it('match for every kitchen-sink statement', () => check('corpus.json', 'pg18.json', SQL));
it('match for every PL/pgSQL fixture', () => check('plpgsql-corpus.json', 'plpgsql-pg18.json', PLPGSQL));
});
Loading
Loading