forked from TryGhost/node-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadOnly.test.js
50 lines (44 loc) · 1.82 KB
/
readOnly.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const sqlite3 = require('..');
const assert = require('assert');
const testCases = [
{ sql: 'DELETE FROM foo', readOnly: false },
{ sql: 'SELECT * FROM foo', readOnly: true },
{ sql: 'UPDATE foo SET x = 10', readOnly: false },
{ sql: 'PRAGMA application_id', readOnly: true },
{ sql: 'PRAGMA application_id = 1', readOnly: false },
{ sql: 'CREATE TABLE bar(x, y)', readOnly: false },
{ sql: 'CREATE TABLE IF NOT EXISTS foo(x, y)', readOnly: false },
// Prepare only uses the first statement if there are multiple
// (the rest are returned in a tail string by the underlying
// sqlite3 function). The readOnly flag is for that first
// statement. Anything after the first statement could be
// readOnly or not readOnly, it just won't be running.
{ sql: 'SELECT * FROM foo; DELETE FROM foo', readOnly: true },
// Statements that only affect the connection don't count.
// This is too bad, wish there were a way to detect such things.
{ sql: 'PRAGMA query_only = false', readOnly: true },
{ sql: 'ATTACH DATABASE \'test\' AS test', readOnly: true },
];
describe('readOnly', function() {
let db;
before(function(done) {
db = new sqlite3.Database(':memory:', function(err) {
if (err) throw err;
db.run('CREATE TABLE foo(x, y)', function(err) {
if (err) throw err;
done();
});
});
});
for (let i = 0; i < testCases.length; i++) {
const testCase = testCases[i];
it("reports " + testCase.sql + " as " +
(testCase.readOnly ? "not " : "") + "readOnly", function(done) {
const stmt = db.prepare(testCase.sql, function(err) {
if (err) throw err;
assert.equal(stmt.readOnly, testCase.readOnly);
stmt.finalize(done);
});
});
}
});