1/*
2  2022-05-22
3
4  The author disclaims copyright to this source code.  In place of a
5  legal notice, here is a blessing:
6
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.
10
11  ***********************************************************************
12
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.
15*/
16'use strict';
17(function(){
18  const toss = function(...args){throw new Error(args.join(' '))};
19  const log = console.log.bind(console),
20        warn = console.warn.bind(console),
21        error = console.error.bind(console);
22
23  const stdout = log;
24  const stderr = error;
25
26  const test1 = function(db){
27    db.exec("create table if not exists t(a);")
28      .transaction(function(db){
29        db.prepare("insert into t(a) values(?)")
30          .bind(new Date().getTime())
31          .stepFinalize();
32        stdout("Number of values in table t:",
33            db.selectValue("select count(*) from t"));
34      });
35  };
36
37  const runTests = function(sqlite3){
38    const capi = sqlite3.capi,
39          oo = sqlite3.oo1,
40          wasm = sqlite3.wasm;
41    stdout("Loaded sqlite3:",capi.sqlite3_libversion(), capi.sqlite3_sourceid());
42    const persistentDir = capi.sqlite3_wasmfs_opfs_dir();
43    if(persistentDir){
44      stdout("Persistent storage dir:",persistentDir);
45    }else{
46      stderr("No persistent storage available.");
47    }
48    const startTime = performance.now();
49    let db;
50    try {
51      db = new oo.DB(persistentDir+'/foo.db');
52      stdout("DB filename:",db.filename);
53      const banner1 = '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>',
54            banner2 = '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<';
55      [
56        test1
57      ].forEach((f)=>{
58        const n = performance.now();
59        stdout(banner1,"Running",f.name+"()...");
60        f(db, sqlite3);
61        stdout(banner2,f.name+"() took ",(performance.now() - n),"ms");
62      });
63    }finally{
64      if(db) db.close();
65    }
66    stdout("Total test time:",(performance.now() - startTime),"ms");
67  };
68
69  sqlite3InitModule(self.sqlite3TestModule).then(runTests);
70})();
71