Files
node/test/parallel/test-sqlite-transactions.js
T
Paul Bouchon 500d0482df test: prefer in-memory databases in sqlite tests
Several SQLite tests created temporary file databases through a
`nextDb()` helper even though they only exercise SQL behavior and never
rely on filesystem persistence. Switch those to `:memory:`, which is
faster and drops the temporary-file bookkeeping. Tests that depend on an
on-disk or shared database keep using files: the constructor, open() and
backup() cases, the timeout and cross-worker suites, and the WAL
journal-mode PRAGMA.

Refs: https://github.com/nodejs/node/issues/64665
Signed-off-by: Paul Bouchon <mail@bitpshr.net>
PR-URL: https://github.com/nodejs/node/pull/64701
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2026-08-04 15:03:13 +00:00

59 lines
1.7 KiB
JavaScript

'use strict';
const { skipIfSQLiteMissing } = require('../common');
skipIfSQLiteMissing();
const { DatabaseSync } = require('node:sqlite');
const { suite, test } = require('node:test');
suite('manual transactions', () => {
test('a transaction is committed', (t) => {
const db = new DatabaseSync(':memory:');
t.after(() => { db.close(); });
const setup = db.exec(`
CREATE TABLE data(
key INTEGER PRIMARY KEY
) STRICT;
`);
t.assert.strictEqual(setup, undefined);
t.assert.deepStrictEqual(
db.prepare('BEGIN').run(),
{ changes: 0, lastInsertRowid: 0 },
);
t.assert.deepStrictEqual(
db.prepare('INSERT INTO data (key) VALUES (100)').run(),
{ changes: 1, lastInsertRowid: 100 },
);
t.assert.deepStrictEqual(
db.prepare('COMMIT').run(),
{ changes: 1, lastInsertRowid: 100 },
);
t.assert.deepStrictEqual(
db.prepare('SELECT * FROM data').all(),
[{ __proto__: null, key: 100 }],
);
});
test('a transaction is rolled back', (t) => {
const db = new DatabaseSync(':memory:');
t.after(() => { db.close(); });
const setup = db.exec(`
CREATE TABLE data(
key INTEGER PRIMARY KEY
) STRICT;
`);
t.assert.strictEqual(setup, undefined);
t.assert.deepStrictEqual(
db.prepare('BEGIN').run(),
{ changes: 0, lastInsertRowid: 0 },
);
t.assert.deepStrictEqual(
db.prepare('INSERT INTO data (key) VALUES (100)').run(),
{ changes: 1, lastInsertRowid: 100 },
);
t.assert.deepStrictEqual(
db.prepare('ROLLBACK').run(),
{ changes: 1, lastInsertRowid: 100 },
);
t.assert.deepStrictEqual(db.prepare('SELECT * FROM data').all(), []);
});
});