Skip to content

Commit

Permalink
sqlite,test: test URI passed as string or Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Feb 27, 2025
1 parent 8508790 commit e145acb
Showing 1 changed file with 53 additions and 17 deletions.
70 changes: 53 additions & 17 deletions test/parallel/test-sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { join } = require('node:path');
const { DatabaseSync, constants } = require('node:sqlite');
const { suite, test } = require('node:test');
const { pathToFileURL } = require('node:url');

let cnt = 0;

tmpdir.refresh();
Expand Down Expand Up @@ -143,26 +142,63 @@ test('URL is supported as the database path', (t) => {
);
});

test('URL query params are supported', (t) => {
const url = pathToFileURL(nextDb());
const db = new DatabaseSync(url);
t.after(() => { db.close(); });
db.exec(`

suite('URI query params', () => {
const baseDbPath = nextDb();
const baseDb = new DatabaseSync(baseDbPath);
baseDb.exec(`
CREATE TABLE data(key INTEGER PRIMARY KEY);
INSERT INTO data (key) VALUES (1);
`);
baseDb.close();

test('query params are supported with URL objects', (t) => {
const url = pathToFileURL(baseDbPath);
url.searchParams.set('mode', 'ro');
const readOnlyDB = new DatabaseSync(url);
t.after(() => { readOnlyDB.close(); });

t.assert.deepStrictEqual(
readOnlyDB.prepare('SELECT * FROM data').all(),
[{ __proto__: null, key: 1 }]
);
t.assert.throws(() => {
readOnlyDB.exec('INSERT INTO data (key) VALUES (1);');
}, {
code: 'ERR_SQLITE_ERROR',
message: 'attempt to write a readonly database',
});
});

const readOnlyDB = new DatabaseSync(`${url}?mode=ro`);
t.after(() => { readOnlyDB.close(); });
test('query params are supported with string', (t) => {
const readOnlyDB = new DatabaseSync(`file:${baseDbPath}?mode=ro`);
t.after(() => { readOnlyDB.close(); });

t.assert.deepStrictEqual(
readOnlyDB.prepare('SELECT * FROM data').all(),
[{ __proto__: null, key: 1 }]
);
t.assert.throws(() => {
readOnlyDB.exec('INSERT INTO data (key) VALUES (1);');
}, {
code: 'ERR_SQLITE_ERROR',
message: 'attempt to write a readonly database',
t.assert.deepStrictEqual(
readOnlyDB.prepare('SELECT * FROM data').all(),
[{ __proto__: null, key: 1 }]
);
t.assert.throws(() => {
readOnlyDB.exec('INSERT INTO data (key) VALUES (1);');
}, {
code: 'ERR_SQLITE_ERROR',
message: 'attempt to write a readonly database',
});
});

test('query params are supported with Buffer', (t) => {
const readOnlyDB = new DatabaseSync(Buffer.from(`file://${baseDbPath}?mode=ro`));
t.after(() => { readOnlyDB.close(); });

t.assert.deepStrictEqual(
readOnlyDB.prepare('SELECT * FROM data').all(),
[{ __proto__: null, key: 1 }]
);
t.assert.throws(() => {
readOnlyDB.exec('INSERT INTO data (key) VALUES (1);');
}, {
code: 'ERR_SQLITE_ERROR',
message: 'attempt to write a readonly database',
});
});
});

0 comments on commit e145acb

Please sign in to comment.