|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { skipIfSQLiteMissing } = require('../common'); |
| 4 | +skipIfSQLiteMissing(); |
| 5 | +const tmpdir = require('../common/tmpdir'); |
| 6 | +const assert = require('node:assert'); |
| 7 | +const { join } = require('node:path'); |
| 8 | +const { test } = require('node:test'); |
| 9 | +const { backup, DatabaseSync } = require('node:sqlite'); |
| 10 | + |
| 11 | +tmpdir.refresh(); |
| 12 | + |
| 13 | +const closedError = { |
| 14 | + code: 'ERR_INVALID_STATE', |
| 15 | + message: 'database is not open', |
| 16 | +}; |
| 17 | + |
| 18 | +// Reading the options bag runs a user getter, so the connection validated on |
| 19 | +// entry can already be closed by the time the method reaches SQLite. |
| 20 | +test('function() with a getter that closes the database', () => { |
| 21 | + const db = new DatabaseSync(':memory:'); |
| 22 | + const options = { |
| 23 | + get useBigIntArguments() { |
| 24 | + db.close(); |
| 25 | + return false; |
| 26 | + }, |
| 27 | + }; |
| 28 | + |
| 29 | + assert.throws(() => { |
| 30 | + db.function('custom', options, () => 1); |
| 31 | + }, closedError); |
| 32 | +}); |
| 33 | + |
| 34 | +test('aggregate() with a getter that closes the database', () => { |
| 35 | + const db = new DatabaseSync(':memory:'); |
| 36 | + const options = { |
| 37 | + start: 0, |
| 38 | + step: (acc, value) => acc + value, |
| 39 | + get useBigIntArguments() { |
| 40 | + db.close(); |
| 41 | + return false; |
| 42 | + }, |
| 43 | + }; |
| 44 | + |
| 45 | + assert.throws(() => { |
| 46 | + db.aggregate('custom', options); |
| 47 | + }, closedError); |
| 48 | +}); |
| 49 | + |
| 50 | +test('deserialize() with a getter that closes the database', () => { |
| 51 | + const source = new DatabaseSync(':memory:'); |
| 52 | + source.exec('CREATE TABLE data (value INTEGER)'); |
| 53 | + const serialized = source.serialize(); |
| 54 | + source.close(); |
| 55 | + |
| 56 | + const db = new DatabaseSync(':memory:'); |
| 57 | + const options = { |
| 58 | + get dbName() { |
| 59 | + db.close(); |
| 60 | + return 'main'; |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + assert.throws(() => { |
| 65 | + db.deserialize(serialized, options); |
| 66 | + }, closedError); |
| 67 | +}); |
| 68 | + |
| 69 | +test('backup() with a getter that closes the database', () => { |
| 70 | + const db = new DatabaseSync(':memory:'); |
| 71 | + db.exec('CREATE TABLE data (value INTEGER)'); |
| 72 | + const options = { |
| 73 | + get rate() { |
| 74 | + db.close(); |
| 75 | + return 1; |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + assert.throws(() => { |
| 80 | + backup(db, join(tmpdir.path, 'backup.db'), options); |
| 81 | + }, closedError); |
| 82 | +}); |
0 commit comments