Skip to content

Commit 4f505c7

Browse files
committed
make test more automatic
1 parent ab83123 commit 4f505c7

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

test.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
// js -m -n -e "load('sql.js')" test.js
22

3-
function test() {
4-
var db = SQL.open();
5-
6-
print('created db');
3+
function jsonCompare(x, y) {
4+
return JSON.stringify(x) == JSON.stringify(y);
5+
}
76

8-
db.exec("CREATE TABLE my_table(a INTEGER, b INTEGER, c VARCHAR(100));");
7+
function check(data, expected) {
8+
if (!jsonCompare(data, expected)) throw 'Comparison failed: ' + JSON.stringify(data) + ' vs. ' + JSON.stringify(expected) + ' at ' + new Error().stack;
9+
}
910

10-
print('executed one command');
11+
function testBasics() {
12+
var db = SQL.open();
1113

14+
db.exec("CREATE TABLE my_table(a INTEGER, b INTEGER, c VARCHAR(100));");
1215
db.exec("INSERT INTO my_table VALUES(1,13153,'thirteen thousand one hundred fifty three');");
1316
db.exec("INSERT INTO my_table VALUES(1,987,'some other number');");
14-
15-
function report(data) {
16-
print(JSON.stringify(data, null, ' '));
17-
}
18-
19-
report(db.exec("SELECT count(*) FROM my_table;"));
20-
21-
// prints [{ "column": "count(*)", "value": "2" }]
22-
23-
print('printed one report');
17+
check(db.exec("SELECT count(*) FROM my_table;"), [[{ "column": "count(*)", "value": "2" }]]);
2418

2519
var db2 = SQL.open();
2620
try {
@@ -29,9 +23,10 @@ function test() {
2923
// Failure is expected, as the other db doesn't have that table!
3024
db2.close();
3125

32-
report(db.exec("SELECT a, b, c FROM my_table;"));
33-
// prints [{ "column": "a", "value": "1" }, { "column": "b", "value": "13153" }, { "column": "c", "value": "thirteen thousand one hundred fifty three" }]
34-
// [{ "column": "a", "value": "1" }, { "column": "b", "value": "987" }, { "column": "c", "value": "some other number" }]
26+
check(db.exec("SELECT a, b, c FROM my_table;"), [
27+
[{ "column": "a", "value": "1" }, { "column": "b", "value": "13153" }, { "column": "c", "value": "thirteen thousand one hundred fifty three" }],
28+
[{ "column": "a", "value": "1" }, { "column": "b", "value": "987" }, { "column": "c", "value": "some other number" }]
29+
]);
3530

3631
db.close();
3732
return;
@@ -40,5 +35,23 @@ function test() {
4035
throw 'reading from the second db should have failed!';
4136
}
4237

43-
test();
38+
function testPersistence() {
39+
// Create a db with some data
40+
var db = SQL.open();
41+
db.exec("CREATE TABLE my_table(a INTEGER, b INTEGER, c VARCHAR(100));");
42+
db.exec("INSERT INTO my_table VALUES(1,987,'some other number');");
43+
db.exec("INSERT INTO my_table VALUES(5,6987,'moar numberz');");
44+
45+
// Serialize it to a typed array
46+
var serialized = db.serialize();
47+
48+
var db2 = SQL.open(serialized);
49+
50+
}
51+
52+
// Run tests
53+
54+
testBasics();
55+
56+
print('ok.');
4457

0 commit comments

Comments
 (0)