|
| 1 | +// Flags: --experimental-sqlite |
| 2 | +'use strict'; |
| 3 | +require('../common'); |
| 4 | +const tmpdir = require('../common/tmpdir'); |
| 5 | +const { existsSync } = require('node:fs'); |
| 6 | +const { join } = require('node:path'); |
| 7 | +const { DatabaseSync, StatementSync } = require('node:sqlite'); |
| 8 | +const { suite, test } = require('node:test'); |
| 9 | +let cnt = 0; |
| 10 | + |
| 11 | +tmpdir.refresh(); |
| 12 | + |
| 13 | +function nextDb() { |
| 14 | + return join(tmpdir.path, `database-${cnt++}.db`); |
| 15 | +} |
| 16 | + |
| 17 | +suite('DatabaseSync() constructor', () => { |
| 18 | + test('throws if called without new', (t) => { |
| 19 | + t.assert.throws(() => { |
| 20 | + DatabaseSync(); |
| 21 | + }, { |
| 22 | + code: 'ERR_CONSTRUCT_CALL_REQUIRED', |
| 23 | + message: /Cannot call constructor without `new`/, |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + test('throws if database path is not a string', (t) => { |
| 28 | + t.assert.throws(() => { |
| 29 | + new DatabaseSync(); |
| 30 | + }, { |
| 31 | + code: 'ERR_INVALID_ARG_TYPE', |
| 32 | + message: /The "path" argument must be a string/, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + test('throws if options is provided but is not an object', (t) => { |
| 37 | + t.assert.throws(() => { |
| 38 | + new DatabaseSync('foo', null); |
| 39 | + }, { |
| 40 | + code: 'ERR_INVALID_ARG_TYPE', |
| 41 | + message: /The "options" argument must be an object/, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + test('throws if options.open is provided but is not a boolean', (t) => { |
| 46 | + t.assert.throws(() => { |
| 47 | + new DatabaseSync('foo', { open: 5 }); |
| 48 | + }, { |
| 49 | + code: 'ERR_INVALID_ARG_TYPE', |
| 50 | + message: /The "options\.open" argument must be a boolean/, |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +suite('DatabaseSync.prototype.open()', () => { |
| 56 | + test('opens a database connection', (t) => { |
| 57 | + const dbPath = nextDb(); |
| 58 | + const db = new DatabaseSync(dbPath, { open: false }); |
| 59 | + t.after(() => { db.close(); }); |
| 60 | + |
| 61 | + t.assert.strictEqual(existsSync(dbPath), false); |
| 62 | + t.assert.strictEqual(db.open(), undefined); |
| 63 | + t.assert.strictEqual(existsSync(dbPath), true); |
| 64 | + }); |
| 65 | + |
| 66 | + test('throws if database is already open', (t) => { |
| 67 | + const db = new DatabaseSync(nextDb(), { open: false }); |
| 68 | + t.after(() => { db.close(); }); |
| 69 | + |
| 70 | + db.open(); |
| 71 | + t.assert.throws(() => { |
| 72 | + db.open(); |
| 73 | + }, { |
| 74 | + code: 'ERR_INVALID_STATE', |
| 75 | + message: /database is already open/, |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +suite('DatabaseSync.prototype.close()', () => { |
| 81 | + test('closes an open database connection', (t) => { |
| 82 | + const db = new DatabaseSync(nextDb()); |
| 83 | + |
| 84 | + t.assert.strictEqual(db.close(), undefined); |
| 85 | + }); |
| 86 | + |
| 87 | + test('throws if database is not open', (t) => { |
| 88 | + const db = new DatabaseSync(nextDb(), { open: false }); |
| 89 | + |
| 90 | + t.assert.throws(() => { |
| 91 | + db.close(); |
| 92 | + }, { |
| 93 | + code: 'ERR_INVALID_STATE', |
| 94 | + message: /database is not open/, |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +suite('DatabaseSync.prototype.prepare()', () => { |
| 100 | + test('returns a prepared statement', (t) => { |
| 101 | + const db = new DatabaseSync(nextDb()); |
| 102 | + t.after(() => { db.close(); }); |
| 103 | + const stmt = db.prepare('CREATE TABLE webstorage(key TEXT)'); |
| 104 | + t.assert.ok(stmt instanceof StatementSync); |
| 105 | + }); |
| 106 | + |
| 107 | + test('throws if database is not open', (t) => { |
| 108 | + const db = new DatabaseSync(nextDb(), { open: false }); |
| 109 | + |
| 110 | + t.assert.throws(() => { |
| 111 | + db.prepare(); |
| 112 | + }, { |
| 113 | + code: 'ERR_INVALID_STATE', |
| 114 | + message: /database is not open/, |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + test('throws if sql is not a string', (t) => { |
| 119 | + const db = new DatabaseSync(nextDb()); |
| 120 | + t.after(() => { db.close(); }); |
| 121 | + |
| 122 | + t.assert.throws(() => { |
| 123 | + db.prepare(); |
| 124 | + }, { |
| 125 | + code: 'ERR_INVALID_ARG_TYPE', |
| 126 | + message: /The "sql" argument must be a string/, |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +suite('DatabaseSync.prototype.exec()', () => { |
| 132 | + test('executes SQL', (t) => { |
| 133 | + const db = new DatabaseSync(nextDb()); |
| 134 | + t.after(() => { db.close(); }); |
| 135 | + const result = db.exec(` |
| 136 | + CREATE TABLE data( |
| 137 | + key INTEGER PRIMARY KEY, |
| 138 | + val INTEGER |
| 139 | + ) STRICT; |
| 140 | + INSERT INTO data (key, val) VALUES (1, 2); |
| 141 | + INSERT INTO data (key, val) VALUES (8, 9); |
| 142 | + `); |
| 143 | + t.assert.strictEqual(result, undefined); |
| 144 | + const stmt = db.prepare('SELECT * FROM data ORDER BY key'); |
| 145 | + t.assert.deepStrictEqual(stmt.all(), [ |
| 146 | + { key: 1, val: 2 }, |
| 147 | + { key: 8, val: 9 }, |
| 148 | + ]); |
| 149 | + }); |
| 150 | + |
| 151 | + test('reports errors from SQLite', (t) => { |
| 152 | + const db = new DatabaseSync(nextDb()); |
| 153 | + t.after(() => { db.close(); }); |
| 154 | + |
| 155 | + t.assert.throws(() => { |
| 156 | + db.exec('CREATE TABLEEEE'); |
| 157 | + }, { |
| 158 | + code: 'ERR_SQLITE_ERROR', |
| 159 | + message: /syntax error/, |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + test('throws if database is not open', (t) => { |
| 164 | + const db = new DatabaseSync(nextDb(), { open: false }); |
| 165 | + |
| 166 | + t.assert.throws(() => { |
| 167 | + db.exec(); |
| 168 | + }, { |
| 169 | + code: 'ERR_INVALID_STATE', |
| 170 | + message: /database is not open/, |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + test('throws if sql is not a string', (t) => { |
| 175 | + const db = new DatabaseSync(nextDb()); |
| 176 | + t.after(() => { db.close(); }); |
| 177 | + |
| 178 | + t.assert.throws(() => { |
| 179 | + db.exec(); |
| 180 | + }, { |
| 181 | + code: 'ERR_INVALID_ARG_TYPE', |
| 182 | + message: /The "sql" argument must be a string/, |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments