4 The author disclaims copyright to this source code. In place of a
5 legal notice, here is a blessing:
7 * May you do good and not evil.
8 * May you find forgiveness for yourself and forgive others.
9 * May you share freely, never taking more than you give.
11 ***********************************************************************
13 A basic test script for sqlite3-api.js. This file must be run in
14 main JS thread and sqlite3.js must have been loaded before it.
17 const T = self.SqliteTestUtil;
18 const log = console.log.bind(console);
20 const assert = function(condition, text) {
22 throw new Error('Assertion failed' + (text ? ': ' + text : ''));
26 const test1 = function(db,sqlite3){
27 const api = sqlite3.api;
28 log("Basic sanity tests...");
30 let st = db.prepare("select 3 as a");
31 //log("statement =",st);
34 .assert('a' === st.getColumnName(0))
35 .assert(st === db._statements[st._pStmt])
36 .assert(1===st.columnCount)
37 .assert(0===st.parameterCount)
38 .mustThrow(()=>st.bind(1,null))
39 .assert(true===st.step())
40 .assert(3 === st.get(0))
41 .mustThrow(()=>st.get(1))
42 .mustThrow(()=>st.get(0,~api.SQLITE_INTEGER))
43 .assert(3 === st.get(0,api.SQLITE_INTEGER))
44 .assert(3 === st.getInt(0))
45 .assert('3' === st.get(0,api.SQLITE_TEXT))
46 .assert('3' === st.getString(0))
47 .assert(3.0 === st.get(0,api.SQLITE_FLOAT))
48 .assert(3.0 === st.getFloat(0))
49 .assert(st.get(0,api.SQLITE_BLOB) instanceof Uint8Array)
50 .assert(st.getBlob(0) instanceof Uint8Array)
51 .assert(3 === st.get([])[0])
52 .assert(3 === st.get({}).a)
53 .assert(3 === st.getJSON(0))
55 .assert(false===st.step())
61 .assert(!db._statements[pId]);
65 sql:`CREATE TABLE t(a,b);
66 INSERT INTO t(a,b) VALUES(1,2),(3,4),(?,?);`,
71 T.assert(2 === list.length);
72 //log("Exec'd SQL:", list);
73 let counter = 0, colNames = [];
75 db.exec("SELECT a a, b b FROM t",{
78 columnNames: colNames,
79 callback: function(row,stmt){
81 T.assert(row.a%2 && row.a<6);
84 T.assert(2 === colNames.length)
85 .assert('a' === colNames[0])
86 .assert(3 === counter)
87 .assert(3 === list.length);
89 db.exec("SELECT a a, b b FROM t",{
91 callback: function(row,stmt){
93 T.assert(Array.isArray(row))
94 .assert(0===row[1]%2 && row[1]<7);
97 T.assert(6 === counter);
100 const testUDF = function(db){
101 log("Testing UDF...");
102 db.createFunction("foo",function(a,b){return a+b});
103 T.assert(7===db.selectValue("select foo(3,4)")).
104 assert(5===db.selectValue("select foo(3,?)",2)).
105 assert(5===db.selectValue("select foo(?,?)",[1,4])).
106 assert(5===db.selectValue("select foo($a,$b)",{$a:0,$b:5}));
107 db.createFunction("bar", {
109 callback: function(){
111 for(let i = 0; i < arguments.length; ++i) rc += arguments[i];
116 log("Testing DB::selectValue() w/ UDF...");
117 T.assert(0===db.selectValue("select bar()")).
118 assert(1===db.selectValue("select bar(1)")).
119 assert(3===db.selectValue("select bar(1,2)")).
120 assert(-1===db.selectValue("select bar(1,2,-4)"));
122 const eqApprox = function(v1,v2,factor=0.05){
123 return v1>=(v2-factor) && v1<=(v2+factor);
126 T.assert('hi' === db.selectValue("select ?",'hi')).
127 assert(null===db.selectValue("select null")).
128 assert(null === db.selectValue("select ?",null)).
129 assert(null === db.selectValue("select ?",[null])).
130 assert(null === db.selectValue("select $a",{$a:null})).
131 assert(eqApprox(3.1,db.selectValue("select 3.0 + 0.1")))
135 const testAttach = function(db){
136 log("Testing ATTACH...");
139 "attach 'foo.db' as foo",
140 "create table foo.bar(a)",
141 "insert into foo.bar(a) values(1),(2),(3)"
145 T.assert(2===db.selectValue('select a from foo.bar where a>1 order by a'));
146 db.exec("detach foo");
147 T.mustThrow(()=>db.exec("select * from foo.bar"));
150 const runTests = function(Module){
151 T.assert(Module._free instanceof Function).
152 assert(Module.allocate instanceof Function).
153 assert(Module.addFunction instanceof Function).
154 assert(Module.removeFunction instanceof Function);
155 const sqlite3 = Module.sqlite3;
156 const api = sqlite3.api;
157 const oo = sqlite3.SQLite3;
158 console.log("Loaded module:",api.sqlite3_libversion(),
159 api.sqlite3_sourceid());
160 log("Build options:",oo.compileOptionUsed());
161 const db = new oo.DB();
163 log("DB:",db.filename);
165 test1, testUDF, testAttach
169 log("Test count:",T.counter - t);
174 log("Total Test count:",T.counter);
177 initSqlite3Module(self.sqlite3TestModule).then(function(theModule){
178 /** Use a timeout so that we are (hopefully) out from
179 under the module init stack when our setup gets
180 run. Just on principle, not because we _need_ to
182 //console.debug("theModule =",theModule);
183 setTimeout(()=>runTests(theModule), 0);