Skip to content

Commit d3c1069

Browse files
committed
Improve test infrastructure
1 parent 36c3a83 commit d3c1069

File tree

7 files changed

+196
-155
lines changed

7 files changed

+196
-155
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "sql.js",
33
"version": "0.1.0",
44
"description": "SQLite library with support for opening and writing databases, prepared statements, and more. This SQLite library is in pure javascript (compiled with emscripten).",
5-
"scripts": { "test": "./test/run.sh" },
5+
"scripts": { "test": "node test/all.js" },
66
"repository" :
77
{ "type" : "git",
88
"url" : "http://github.com/lovasoa/sql.js.git"

test/all.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var fs = require("fs");
2+
3+
var target = process.argv[2];
4+
var file = target ? "../js/sql-"+target+".js" : "../js/sql.js";
5+
var sql = require(file);
6+
7+
var files = fs.readdirSync(__dirname);
8+
9+
for (var i=0; i<files.length; i++) {
10+
var file = files[i];
11+
var m = /^test_(.+)\.js$/.exec(file);
12+
if (m !== null) {
13+
var name = m[1];
14+
var testModule = require("./" + file);
15+
if (testModule.test) exports['test ' + name] = testModule.test.bind(null, sql);
16+
}
17+
}
18+
19+
if (module == require.main) require('test').run(exports);

test/test_blob.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
var assert = require("assert");
2-
var SQL = require("../js/sql.js");
3-
4-
function toSimpleArray(arr) {
5-
return Array.prototype.slice.call(arr);
1+
exports.test = function(SQL, assert){
2+
var db = new SQL.Database();
3+
db.exec("CREATE TABLE test (data); INSERT INTO test VALUES (x'6162ff'),(x'00')"); // Insert binary data. This is invalid UTF8 on purpose
4+
5+
6+
console.log("Testing writing BLOBs");
7+
var stmt = db.prepare("INSERT INTO test VALUES (?)");
8+
var bigArray = new Uint8Array(1e6);
9+
bigArray[500] = 0x42
10+
stmt.run([ bigArray ]);
11+
12+
var stmt = db.prepare("SELECT * FROM test ORDER BY length(data) DESC");
13+
14+
stmt.step();
15+
var array = stmt.get()[0];
16+
assert.equal(array.length, bigArray.length, "BLOB read from the database should be the same size as the one that was inserted");
17+
for (var i=0; i<array.length; i++) {
18+
// Avoid doing 1e6 assert, to not pollute the console
19+
if (array[i]!==bigArray[i])
20+
assert.fail(array[i], bigArray[i] , "The blob stored in the database should be exactly the same as the one that was inserted");
21+
}
22+
23+
stmt.step();
24+
var res = stmt.get();
25+
assert.deepEqual(res, [new Uint8Array([0x61, 0x62, 0xff])], "Reading BLOB");
26+
27+
stmt.step();
28+
var res = stmt.get();
29+
assert.deepEqual(res, [new Uint8Array([0x00])], "Reading BLOB with a null byte");
30+
31+
assert.strictEqual(stmt.step(), false, "stmt.step() should return false after all values were read");
32+
db.close();
33+
};
34+
35+
if (module == require.main) {
36+
var sql = require('../js/sql.js');
37+
var assert = require("assert");
38+
exports.test(sql, assert);
639
}
7-
8-
var db = new SQL.Database();
9-
db.exec("CREATE TABLE test (data); INSERT INTO test VALUES (x'616200ff'),(x'00')"); // Insert binary data. This is invalid UTF8 on purpose
10-
11-
12-
console.log("Testing writing BLOBs");
13-
var stmt = db.prepare("INSERT INTO test VALUES (?)");
14-
var bigArray = new Uint8Array(1e6);
15-
bigArray[500] = 0x42
16-
stmt.run([ bigArray ]);
17-
18-
console.log("Testing reading BLOBs");
19-
var stmt = db.prepare("SELECT * FROM test ORDER BY length(data) DESC");
20-
21-
stmt.step();
22-
var array = stmt.get()[0];
23-
assert.equal(array.length, bigArray.length);
24-
for (var i=0; i<array.length; i++) if (array[i]!==bigArray[i]) assert(false, "The blob stored in the database was altered");
25-
26-
stmt.step();
27-
var res = stmt.get();
28-
assert.deepEqual(res, [new Uint8Array([0x61, 0x62, 0x00, 0xff])]);
29-
30-
stmt.step();
31-
var res = stmt.get();
32-
assert.deepEqual(res, [new Uint8Array([0x00])]);
33-
34-
assert(stmt.step() === false, "All values should have been retrieved");

test/test_database.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
1-
var assert = require("assert");
2-
var sql = require('../js/sql.js');
1+
exports.test = function(sql, assert) {
2+
assert.notEqual(sql.Database, undefined, "Should export a Database object");
33

4-
assert(sql.Database != undefined, "Should export a Database object");
4+
console.log("Testing database creation...");
5+
// Create a database
6+
var db = new sql.Database();
7+
assert.equal(Object.getPrototypeOf(db), sql.Database.prototype, "Creating a database object");
58

6-
console.log("Testing database creation...");
7-
// Create a database
8-
var db = new sql.Database();
9-
assert.equal(Object.getPrototypeOf(db), sql.Database.prototype);
9+
// Execute some sql
10+
sqlstr = "CREATE TABLE test (a, b, c, d, e);";
11+
sqlstr += "INSERT INTO test VALUES (NULL, 42, 4.2, 'fourty two', x'42');";
12+
var res = db.exec(sqlstr);
13+
assert.deepEqual(res, [], "Table creation should not return anything");
1014

11-
// Execute some sql
12-
sqlstr = "CREATE TABLE test (a, b, c, d, e);";
13-
sqlstr += "INSERT INTO test VALUES (NULL, 42, 4.2, 'fourty two', x'42');";
14-
var res = db.exec(sqlstr);
15-
assert.deepEqual(res, []); // Table creation should not return anything
15+
//Retrieving values
16+
sqlstr = "SELECT * FROM test;";
17+
var res = db.exec(sqlstr);
18+
var expectedResult = [{
19+
columns : ['a','b','c','d','e'],
20+
values : [
21+
['','42','4.2','fourty two', String.fromCharCode(0x42)]
22+
]
23+
}];
24+
assert.deepEqual(res, expectedResult, "db.exec() return value");
1625

17-
//Retrieving values
18-
console.log("Testing db.exec");
19-
sqlstr = "SELECT * FROM test;";
20-
var res = db.exec(sqlstr);
21-
var expectedResult = [{
22-
columns : ['a','b','c','d','e'],
23-
values : [
24-
['','42','4.2','fourty two', String.fromCharCode(0x42)]
25-
]
26-
}];
27-
assert.deepEqual(res, expectedResult); // Table creation should not return anything
26+
// Export the database to an Uint8Array containing the SQLite database file
27+
var binaryArray = db.export();
28+
assert.strictEqual(String.fromCharCode.apply(null,binaryArray.slice(0,6)), 'SQLite',
29+
"The first 6 bytes of an SQLite database should form the word 'SQLite'");
30+
db.close();
2831

29-
console.log("Testing database export...");
30-
// Export the database to an Uint8Array containing the SQLite database file
31-
var binaryArray = db.export();
32-
assert(String.fromCharCode.apply(null,binaryArray.slice(0,6)) === 'SQLite',
33-
"The first 6 bytes of an SQLite database should form the word 'SQLite'");
32+
var db2 = new SQL.Database(binaryArray);
33+
result = db2.exec("SELECT * FROM test");
34+
assert.deepEqual(result, expectedResult,
35+
"Exporting and re-importing the database should lead to the same database");
36+
db2.close();
37+
};
3438

35-
var db2 = new SQL.Database(binaryArray);
36-
result = db2.exec("SELECT * FROM test");
37-
assert.deepEqual(result, expectedResult,
38-
"Exporting and re-importing the database should lead to the same database");
39+
if (module == require.main) {
40+
var sql = require('../js/sql.js');
41+
var assert = require('assert');
42+
exports.test(sql, assert);
43+
}

test/test_errors.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
var assert = require("assert");
2-
var sql = require('../js/sql.js');
1+
exports.test = function(sql, assert) {
2+
// Create a database
3+
var db = new sql.Database();
34

4-
// Create a database
5-
var db = new sql.Database();
5+
// Execute some sql
6+
var res = db.exec("CREATE TABLE test (a, b, c, d, e);");
67

7-
// Execute some sql
8-
var res = db.exec("CREATE TABLE test (a, b, c, d, e);");
8+
assert.throws(function(){
9+
db.exec("I ain't be no valid sql ...");
10+
}, "Executing invalid SQL should throw an error");
911

10-
assert.throws(function(){
11-
db.exec("I ain't be no valid sql ...");
12-
}, "Executing invalid SQL should throw an error");
1312

13+
var stmt = db.prepare("INSERT INTO test (a) VALUES (?)");
1414

15-
var stmt = db.prepare("INSERT INTO test (a) VALUES (?)");
1615

16+
assert.throws(function(){
17+
stmt.bind([1,2,3]);
18+
}, "Binding too many parameters should throw an exception");
1719

18-
assert.throws(function(){
19-
stmt.bind([1,2,3]);
20-
}, "Binding too many parameters should throw an exception");
20+
stmt.run([2]); // Previous errors should not hav spoiled the statement
2121

22-
stmt.run([2]); // Previous errors should not hav spoiled the statement
22+
db.close();
2323

24-
db.close();
24+
assert.throws(function(){
25+
stmt.run([3]);
26+
}, "Statements should'nt be able to execute after the database is closed");
27+
}
2528

26-
assert.throws(function(){
27-
stmt.run([3]);
28-
}, "Statements should'nt be able to execute after the database is closed");
29+
if (module == require.main) {
30+
var sql = require('../js/sql.js');
31+
var assert = require("assert");
32+
exports.test(sql, assert);
33+
}

test/test_node_file.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
var assert = require ("assert");
1+
exports.test = function(SQL, assert) {
2+
//Node filesystem module - You know that.
3+
var fs = require('fs');
24

3-
//Node filesystem module - You know that.
4-
var fs = require('fs');
5+
//Ditto, path module
6+
var path = require('path');
57

6-
//Ditto, path module
7-
var path = require('path');
8+
var filebuffer = fs.readFileSync(path.join(__dirname, 'test.sqlite'));
89

9-
//Actual path I'm using to get to sql.js in my project.
10-
var SQL = require('../js/sql.js');
10+
//Works
11+
var db = new SQL.Database(filebuffer);
1112

12-
var filebuffer = fs.readFileSync(path.join(__dirname, 'test.sqlite'));
13+
//[{"columns":["id","content"],"values":[["0","hello"],["1","world"]]}]
14+
var res = db.exec("SELECT * FROM test WHERE id = 0");
15+
assert.deepEqual(res,
16+
[{"columns":["id","content"],"values":[["0","hello"]]}],
17+
"One should be able to read the contents of an SQLite database file read from disk");
18+
db.close();
19+
}
1320

14-
//Works
15-
var db = new SQL.Database(filebuffer);
16-
17-
//[{"columns":["id","content"],"values":[["0","hello"],["1","world"]]}]
18-
var res = db.exec("SELECT * FROM test WHERE id = 0");
19-
assert.deepEqual(res, [{"columns":["id","content"],"values":[["0","hello"]]}]);
21+
if (module == require.main) {
22+
var sql = require('../js/sql.js');
23+
var assert = require("assert");
24+
exports.test(sql, assert);
25+
}

test/test_statement.js

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,70 @@
1-
var assert = require("assert");
2-
var sql = require('../js/sql.js');
1+
exports.test = function(sql, assert){
2+
// Create a database
3+
var db = new sql.Database();
34

4-
// Create a database
5-
var db = new sql.Database();
5+
// Execute some sql
6+
sqlstr = "CREATE TABLE alphabet (letter, code);";
7+
db.exec(sqlstr);
68

7-
// Execute some sql
8-
sqlstr = "CREATE TABLE alphabet (letter, code);";
9-
db.exec(sqlstr);
9+
var result = db.exec("SELECT name FROM sqlite_master WHERE type='table'");
10+
assert.deepEqual(result, [{columns:['name'], values:[['alphabet']]}],
11+
"Table properly created");
1012

11-
var result = db.exec("SELECT name FROM sqlite_master WHERE type='table'");
12-
assert.deepEqual(result, [{columns:['name'], values:[['alphabet']]}]);
13+
// Prepare a statement to insert values in tha database
14+
var stmt = db.prepare("INSERT INTO alphabet (letter,code) VALUES (?,?)");
15+
// Execute the statement several times
16+
stmt.run(['a',1]);
17+
stmt.run(['b',2.2]);
18+
stmt.run(['c']); // The second parameter will be bound to NULL
1319

14-
console.log("Testing prepared statements...");
15-
// Prepare a statement to insert values in tha database
16-
var stmt = db.prepare("INSERT INTO alphabet (letter,code) VALUES (?,?)");
17-
console.log("Testing Statement.run()");
18-
// Execute the statement several times
19-
stmt.run(['a',1]);
20-
stmt.run(['b',2.2]);
21-
stmt.run(['c']); // The second parameter will be bound to NULL
20+
// Free the statement
21+
stmt.free();
2222

23-
console.log("Testing statement.free()");
24-
// Free the statement
25-
stmt.free();
23+
result = db.exec("SELECT * FROM alphabet");
24+
assert.deepEqual(result,
25+
[{columns:['letter', 'code'], values:[['a','1'],['b','2.2'],['c','']]}],
26+
"Statement.run() should have added data to the database");
2627

27-
result = db.exec("SELECT * FROM alphabet");
28-
assert.deepEqual(result, [{columns:['letter', 'code'], values:[['a','1'],['b','2.2'],['c','']]}]);
28+
var stmt = db.prepare("select 5 as nbr, '粵語😄' as str, null as nothing;");
29+
stmt.step(); // Run the statement
30+
assert.deepEqual(stmt.getColumnNames(), ['nbr','str','nothing'], 'Statement.GetColumnNames()');
31+
var res = stmt.getAsObject();
32+
assert.strictEqual(res.nbr, 5, 'Read number');
33+
assert.strictEqual(res.str, '粵語😄', "Read string");
34+
assert.strictEqual(res.nothing, null, "Read null");
35+
assert.deepEqual(res, {nbr:5, str:'粵語😄', nothing:null}, "Statement.getAsObject()");
36+
stmt.free();
2937

30-
console.log("Testing getting data...");
38+
// Prepare an sql statement
39+
var stmt = db.prepare("SELECT * FROM alphabet WHERE code BETWEEN :start AND :end ORDER BY code");
40+
// Bind values to the parameters
41+
stmt.bind([0, 256]);
42+
// Execute the statement
43+
stmt.step();
44+
// Get one row of result
45+
result = stmt.get();
46+
assert.deepEqual(result, ['a',1], "Binding named parameters by their position");
3147

32-
var stmt = db.prepare("select 5 as nbr, '粵語😄' as str, null as nothing;");
33-
stmt.step(); // Run the statement
34-
assert.deepEqual(stmt.getColumnNames(), ['nbr','str','nothing']);
35-
var res = stmt.getAsObject();
36-
assert.strictEqual(res.nbr, 5);
37-
assert.strictEqual(res.str, '粵語😄');
38-
assert.strictEqual(res.nothing, null);
39-
assert.deepEqual(res, {nbr:5, str:'粵語😄', nothing:null});
40-
stmt.free();
48+
// Fetch the next row of result
49+
result = stmt.step();
50+
assert.equal(result, true);
51+
result = stmt.get();
52+
assert.deepEqual(result, ['b',2.2], "Fetching the next row of result");
4153

42-
// Prepare an sql statement
43-
var stmt = db.prepare("SELECT * FROM alphabet WHERE code BETWEEN :start AND :end ORDER BY code");
44-
// Bind values to the parameters
45-
stmt.bind([0, 256]);
46-
// Execute the statement
47-
stmt.step();
48-
// Get one row of result
49-
result = stmt.get();
50-
assert.deepEqual(result, ['a',1]);
54+
// Reset and reuse at once
55+
result = stmt.get([0, 1]);
56+
assert.deepEqual(result, ['a',1], "Reset and reuse at once");
5157

52-
// Fetch the next row of result
53-
result = stmt.step();
54-
assert.equal(result, true);
55-
result = stmt.get();
56-
assert.deepEqual(result, ['b',2.2]);
58+
// Pass objects to get() and bind() to use named parameters
59+
result = stmt.get({':start':1, ':end':1});
60+
assert.deepEqual(result, ['a',1], "Binding named parameters");
5761

58-
// Reset and reuse at once
59-
result = stmt.get([0, 1]);
60-
assert.deepEqual(result, ['a',1]);
62+
// Close the database and all associated statements
63+
db.close();
64+
}
6165

62-
// Pass objects to get() and bind() to use named parameters
63-
result = stmt.get({':start':1, ':end':1});
64-
assert.deepEqual(result, ['a',1]);
65-
66-
// free the memory used by the statement
67-
stmt.free();
68-
// You can not use your statement anymore once it has been freed.
69-
// But not freeing your statements causes memory leaks. You don't want that.
66+
if (module == require.main) {
67+
var sql = require('../js/sql.js');
68+
var assert = require("assert");
69+
exports.test(sql, assert);
70+
}

0 commit comments

Comments
 (0)