| 1 |
/* |
| 2 |
** 2023-08-29 |
| 3 |
** |
| 4 |
** The author disclaims copyright to this source code. In place of |
| 5 |
** a 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 |
** This file contains a test application for SQLTester.js. |
| 13 |
*/ |
| 14 |
import {default as ns} from './SQLTester.mjs'; |
| 15 |
import {default as allTests} from './test-list.mjs'; |
| 16 |
|
| 17 |
globalThis.sqlite3 = ns.sqlite3; |
| 18 |
const log = function f(...args){ |
| 19 |
console.log('SQLTester.run:',...args); |
| 20 |
return f; |
| 21 |
}; |
| 22 |
|
| 23 |
const out = function f(...args){ return f.outer.out(...args) }; |
| 24 |
out.outer = new ns.Outer(); |
| 25 |
out.outer.getOutputPrefix = ()=>'SQLTester.run: '; |
| 26 |
const outln = (...args)=>{ return out.outer.outln(...args) }; |
| 27 |
|
| 28 |
const affirm = function(expr, msg){ |
| 29 |
if( !expr ){ |
| 30 |
throw new Error(arguments[1] |
| 31 |
? ("Assertion failed: "+arguments[1]) |
| 32 |
: "Assertion failed"); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
let ts = new ns.TestScript('/foo.test',` |
| 37 |
/* |
| 38 |
** This is a comment. There are many like it but this one is mine. |
| 39 |
** |
| 40 |
** SCRIPT_MODULE_NAME: sanity-check-0 |
| 41 |
** xMIXED_MODULE_NAME: mixed-module |
| 42 |
** xMODULE_NAME: module-name |
| 43 |
** xREQUIRED_PROPERTIES: small fast reliable |
| 44 |
** xREQUIRED_PROPERTIES: RECURSIVE_TRIGGERS |
| 45 |
** xREQUIRED_PROPERTIES: TEMPSTORE_FILE TEMPSTORE_MEM |
| 46 |
** xREQUIRED_PROPERTIES: AUTOVACUUM INCRVACUUM |
| 47 |
** |
| 48 |
*/ |
| 49 |
/* --verbosity 3 */ |
| 50 |
/* ---must-fail */ |
| 51 |
/* # must fail */ |
| 52 |
/* --verbosity 0 */ |
| 53 |
--print Hello, world. |
| 54 |
--close all |
| 55 |
--oom |
| 56 |
--db 0 |
| 57 |
--new my.db |
| 58 |
--null zilch |
| 59 |
--testcase 1.0 |
| 60 |
SELECT 1, null; |
| 61 |
--result 1 zilch |
| 62 |
--glob *zil* |
| 63 |
--notglob *ZIL* |
| 64 |
SELECT 1, 2; |
| 65 |
intentional error; |
| 66 |
--run |
| 67 |
/* ---intentional-failure */ |
| 68 |
--testcase json-1 |
| 69 |
SELECT json_array(1,2,3) |
| 70 |
--json [1,2,3] |
| 71 |
--testcase tableresult-1 |
| 72 |
select 1, 'a'; |
| 73 |
select 2, 'b'; |
| 74 |
--tableresult |
| 75 |
# [a-z] |
| 76 |
2 b |
| 77 |
--end |
| 78 |
--testcase json-block-1 |
| 79 |
select json_array(1,2,3); |
| 80 |
select json_object('a',1,'b',2); |
| 81 |
--json-block |
| 82 |
[1,2,3] |
| 83 |
{"a":1,"b":2} |
| 84 |
--end |
| 85 |
--testcase col-names-on |
| 86 |
--column-names 1 |
| 87 |
select 1 as 'a', 2 as 'b'; |
| 88 |
--result a 1 b 2 |
| 89 |
--testcase col-names-off |
| 90 |
--column-names 0 |
| 91 |
select 1 as 'a', 2 as 'b'; |
| 92 |
--result 1 2 |
| 93 |
--close |
| 94 |
--print Until next time |
| 95 |
`); |
| 96 |
|
| 97 |
const sqt = new ns.SQLTester() |
| 98 |
.setLogger(console.log.bind(console)) |
| 99 |
.verbosity(1) |
| 100 |
.addTestScript(ts); |
| 101 |
sqt.outer().outputPrefix(''); |
| 102 |
|
| 103 |
const runTests = function(){ |
| 104 |
try{ |
| 105 |
if( 0 ){ |
| 106 |
affirm( !sqt.getCurrentDb(), 'sqt.getCurrentDb()' ); |
| 107 |
sqt.openDb('/foo.db', true); |
| 108 |
affirm( !!sqt.getCurrentDb(),'sqt.getCurrentDb()' ); |
| 109 |
affirm( 'zilch' !== sqt.nullValue() ); |
| 110 |
ts.run(sqt); |
| 111 |
affirm( 'zilch' === sqt.nullValue() ); |
| 112 |
sqt.addTestScript(ts); |
| 113 |
sqt.runTests(); |
| 114 |
}else{ |
| 115 |
for(const t of allTests){ |
| 116 |
sqt.addTestScript( new ns.TestScript(t) ); |
| 117 |
} |
| 118 |
allTests.length = 0; |
| 119 |
sqt.runTests(); |
| 120 |
} |
| 121 |
}finally{ |
| 122 |
//log( "Metrics:", sqt.metrics ); |
| 123 |
sqt.reset(); |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
if( globalThis.WorkerGlobalScope ){ |
| 128 |
const wPost = (type,payload)=>globalThis.postMessage({type, payload}); |
| 129 |
globalThis.onmessage = function({data}){ |
| 130 |
switch(data.type){ |
| 131 |
case 'run-tests':{ |
| 132 |
try{ runTests(); } |
| 133 |
finally{ wPost('tests-end', sqt.metrics); } |
| 134 |
break; |
| 135 |
} |
| 136 |
default: |
| 137 |
log("unhandled onmessage: ",data); |
| 138 |
break; |
| 139 |
} |
| 140 |
}; |
| 141 |
sqt.setLogger((msg)=>{ |
| 142 |
wPost('stdout', {message: msg}); |
| 143 |
}); |
| 144 |
wPost('is-ready'); |
| 145 |
//globalThis.onmessage({data:{type:'run-tests'}}); |
| 146 |
}else{ |
| 147 |
runTests(); |
| 148 |
} |