tests/sims/sqljoin.test.ts
The automated tests for the /simulators/sql-joins simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- On 500 random pairs of tables with NULLs and duplicate keys, every join returns the same rows as SQLite running the query the page shows.
// /simulators/sql-joins: inner, outer, cross, semi and anti joins with NULLs, against SQLite.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { evaluate, resultOf, sqlFor, JOINS, DEFAULT_CUSTOMERS, DEFAULT_ORDERS, type Customer, type Order } from '../../src/lib/sims/sqlJoin.ts';
import { lcg } from './check.ts';
let sqlite = true;
try { execFileSync('python3', ['-c', 'import sqlite3; assert sqlite3.sqlite_version_info >= (3, 39)'], { stdio: 'ignore' }); } catch { sqlite = false; }
function tables(r: () => number): { cs: Customer[]; os: Order[] } {
const nc = Math.floor(r() * 6), no = Math.floor(r() * 7);
const id = () => (r() < 0.2 ? null : 1 + Math.floor(r() * 4)); // small range, so duplicates and misses both happen
return {
cs: Array.from({ length: nc }, (_, i) => ({ id: id(), name: `c${i}` })),
os: Array.from({ length: no }, (_, j) => ({ id: j, customerId: id(), item: `i${j}` }))
};
}
test('on 500 random pairs of tables with NULLs and duplicate keys, every join returns the same rows as SQLite running the query the page shows', { skip: !sqlite && 'python3 with SQLite 3.39+ (for RIGHT and FULL JOIN) is not installed' }, () => {
const r = lcg(18);
const cases = [{ cs: DEFAULT_CUSTOMERS, os: DEFAULT_ORDERS }, ...Array.from({ length: 500 }, () => tables(r))];
const queries = JOINS.map((j) => sqlFor(j.id));
const got = JSON.parse(execFileSync('python3', ['-c', `
import sqlite3, json, sys
cases, queries = json.loads(sys.stdin.read())
out = []
for c in cases:
db = sqlite3.connect(':memory:')
db.execute('CREATE TABLE customers (id INTEGER, name TEXT)')
db.execute('CREATE TABLE orders (id INTEGER, customer_id INTEGER, item TEXT)')
db.executemany('INSERT INTO customers VALUES (?, ?)', [(x['id'], x['name']) for x in c['cs']])
db.executemany('INSERT INTO orders VALUES (?, ?, ?)', [(x['id'], x['customerId'], x['item']) for x in c['os']])
out.append([[list(row) for row in db.execute(q)] for q in queries])
print(json.dumps(out))
`], { input: JSON.stringify([cases, queries]), maxBuffer: 1 << 26 }).toString());
cases.forEach(({ cs, os }, k) => JOINS.forEach((j, q) => {
const mine = resultOf(evaluate(j.id, cs, os)).map(({ li, rj }) => {
const c = li === null ? null : cs[li], o = rj === null ? null : os[rj];
return j.id === 'SEMI' || j.id === 'ANTI' ? [c!.id, c!.name] : [c?.id ?? null, c?.name ?? null, o?.id ?? null, o?.customerId ?? null, o?.item ?? null];
});
const key = (rows: unknown[][]) => rows.map((x) => JSON.stringify(x)).sort();
assert.deepEqual(key(mine), key(got[k][q]), `${j.id} JOIN on case ${k}: ${JSON.stringify({ cs, os })}`);
}));
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.