-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtest_errors.js
69 lines (56 loc) · 1.94 KB
/
test_errors.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
exports.test = function(sql, assert) {
assert.throws(function(){
var db = new sql.Database([1,2,3]);
db.exec("SELECT * FROM sqlite_master");
},
/not a database/,
"Querying an invalid database should throw an error");
// Create a database
var db = new sql.Database();
// Execute some sql
var res = db.exec("CREATE TABLE test (a INTEGER PRIMARY KEY, b, c, d, e);");
assert.throws(function(){
db.exec("I ain't be no valid sql ...");
},
/syntax error/,
"Executing invalid SQL should throw an error");
assert.throws(function(){
db.run("INSERT INTO test (a) VALUES (1)");
db.run("INSERT INTO test (a) VALUES (1)");
},
/UNIQUE constraint failed/,
"Inserting two rows with the same primary key should fail");
var stmt = db.prepare("INSERT INTO test (a) VALUES (?)");
assert.throws(function(){
stmt.bind([1,2,3]);
},
/out of range/,
"Binding too many parameters should throw an exception");
assert.throws(function(){
db.run("CREATE TABLE test (this,wont,work)");
},
/table .+ already exists/,
"Trying to create a table with a name that is already used should throw an error");
stmt.run([2]);
assert.deepEqual(db.exec("SELECT a,b FROM test WHERE a=2"),
[{columns:['a', 'b'],values:[[2, null]]}],
"Previous errors should not have spoiled the statement");
db.close();
assert.throws(function(){
stmt.run([3]);
}, "Statements shouldn't be able to execute after the database is closed");
};
if (module == require.main) {
const target_file = process.argv[2];
const sql_loader = require('./load_sql_lib');
sql_loader(target_file).then((sql)=>{
require('test').run({
'test errors': function(assert){
exports.test(sql, assert);
}
});
})
.catch((e)=>{
console.error(e);
});
}