SQLTester.run.mjs 3.6 KB

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