forked from sql-js/sql.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_extension_functions.js
88 lines (72 loc) · 2.88 KB
/
test_extension_functions.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
exports.test = function(sql, assert) {
var db = new sql.Database();
var res = db.exec("CREATE TABLE test (str_data, data);");
db.run("INSERT INTO test VALUES ('Hello World!', 1);");
db.run("INSERT INTO test VALUES ('', 2);");
db.run("INSERT INTO test VALUES ('', 2);");
db.run("INSERT INTO test VALUES ('', 4);");
db.run("INSERT INTO test VALUES ('', 5);");
db.run("INSERT INTO test VALUES ('', 6);");
db.run("INSERT INTO test VALUES ('', 7);");
db.run("INSERT INTO test VALUES ('', 8);");
db.run("INSERT INTO test VALUES ('', 9);");
var res = db.exec("SELECT mode(data) FROM test;");
var expectedResult = [{
columns : ['mode(data)'],
values : [
[2]
]
}];
assert.deepEqual(res, expectedResult, "mode() function works");
var res = db.exec("SELECT lower_quartile(data) FROM test;");
var expectedResult = [{
columns : ['lower_quartile(data)'],
values : [
[2]
]
}];
assert.deepEqual(res, expectedResult, "upper_quartile() function works");
var res = db.exec("SELECT upper_quartile(data) FROM test;");
var expectedResult = [{
columns : ['upper_quartile(data)'],
values : [
[7]
]
}];
assert.deepEqual(res, expectedResult, "upper_quartile() function works");
var res = db.exec("SELECT variance(data) FROM test;");
assert.equal(res[0]['values'][0][0].toFixed(2), 8.11, "variance() function works");
var res = db.exec("SELECT stdev(data) FROM test;");
assert.equal(res[0]['values'][0][0].toFixed(2), 2.85, "stdev() function works");
var res = db.exec("SELECT acos(data) FROM test;");
assert.equal(res[0]['values'][0][0].toFixed(2), 0, "acos() function works");
var res = db.exec("SELECT asin(data) FROM test;");
assert.equal(res[0]['values'][0][0].toFixed(2), 1.57, "asin() function works");
var res = db.exec("SELECT atan2(data, 1) FROM test;");
assert.equal(res[0]['values'][0][0].toFixed(2), 0.79, "atan2() function works");
var res = db.exec("SELECT difference(str_data, 'ello World!') FROM test;");
assert.equal(res[0]['values'][0][0], 3, "difference() function works");
var res = db.exec("SELECT ceil(4.1)");
assert.equal(res[0]['values'][0][0], 5, "ceil() function works");
var res = db.exec("SELECT floor(4.1)");
assert.equal(res[0]['values'][0][0], 4, "floor() function works");
var res = db.exec("SELECT pi()");
assert.equal(res[0]['values'][0][0].toFixed(5), 3.14159, "pi() function works");
var res = db.exec("SELECT reverse(str_data) FROM test;");
assert.equal(res[0]['values'][0][0], "!dlroW olleH", "reverse() function works");
};
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 extension functions': function(assert){
exports.test(sql, assert);
}
});
})
.catch((e)=>{
console.error(e);
assert.fail(e);
});
}