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