Home | History | Annotate | Download | only in test
      1 # 2011 Jan 21
      2 #
      3 # The author disclaims copyright to this source code.  In place of
      4 # a legal notice, here is a blessing:
      5 #
      6 #    May you do good and not evil.
      7 #    May you find forgiveness for yourself and forgive others.
      8 #    May you share freely, never taking more than you give.
      9 #
     10 #***********************************************************************
     11 # This file implements regression tests for SQLite library.
     12 #
     13 # This file implements tests for the "sqlite3_trace()" API. Specifically,
     14 # it tests the special handling of nested SQL statements (those executed
     15 # by virtual table or user function callbacks). These statements are treated
     16 # differently in two respects:
     17 #
     18 #   1. Each line of the statement is prefixed with "-- " to turn it into
     19 #      an SQL comment.
     20 #
     21 #   2. Parameter expansion is not performed.
     22 #
     23 
     24 set testdir [file dirname $argv0]
     25 source $testdir/tester.tcl
     26 ifcapable !trace { finish_test ; return }
     27 set ::testprefix trace2
     28 
     29 proc sql {zSql} { db one $zSql }
     30 proc trace {zSql} { lappend ::trace $zSql }
     31 
     32 db func sql sql
     33 db trace trace
     34 
     35 proc do_trace_test {tn sql expected} {
     36   # Test that the list of string passed to the trace callback when $sql
     37   # is executed is equivalent to the list of strings in $expected.
     38   #
     39   set ::trace [list]
     40   execsql $sql
     41   uplevel do_test $tn [list {set ::trace}] [list [list {*}$expected]]
     42 }
     43 
     44 proc do_trace_select_test {tn sql expected} {
     45 
     46   uplevel [list do_trace_test ${tn}.a $sql $expected]
     47 
     48   # Now execute each SQL statement passed to the trace callback in the
     49   # block above. Check that this causes the same set of strings to be
     50   # passed to the trace callback again. i.e. that executing the output
     51   # of the trace callback is equivalent to the SQL script in $sql.
     52   #
     53   set sqllist $::trace
     54   set ::trace [list]
     55   foreach item $sqllist { execsql $item }
     56   uplevel do_test $tn.b [list {set ::trace}] [list $sqllist]
     57 }
     58 
     59 do_trace_select_test 1.1  {
     60   SELECT 1, 2, 3;
     61 } {
     62   "SELECT 1, 2, 3;"
     63 }
     64 
     65 do_trace_select_test 1.2  {
     66   SELECT sql('SELECT 1, 2, 3');
     67 } {
     68   "SELECT sql('SELECT 1, 2, 3');"
     69   "-- SELECT 1, 2, 3"
     70 }
     71 
     72 do_trace_select_test 1.3  {
     73   SELECT sql('SELECT 1, 
     74     2, 
     75     3'
     76   );
     77 } {
     78   "SELECT sql('SELECT 1, 
     79     2, 
     80     3'
     81   );"
     82   "-- SELECT 1, 
     83 --     2, 
     84 --     3"
     85 }
     86 
     87 do_trace_select_test 1.4  {
     88   SELECT sql('SELECT 1, 
     89 
     90 
     91     3'
     92   );
     93 } {
     94   "SELECT sql('SELECT 1, 
     95 
     96 
     97     3'
     98   );"
     99   "-- SELECT 1, 
    100 -- 
    101 -- 
    102 --     3"
    103 }
    104 
    105 do_trace_select_test 1.5  {
    106   SELECT $var, sql('SELECT 1, 
    107     $var, 
    108     3'
    109   );
    110 } {
    111   "SELECT NULL, sql('SELECT 1, 
    112     $var, 
    113     3'
    114   );"
    115   "-- SELECT 1, 
    116 --     $var, 
    117 --     3"
    118 }
    119 
    120 ifcapable fts3 {
    121   do_execsql_test 2.1 {
    122     CREATE VIRTUAL TABLE x1 USING fts4;
    123     INSERT INTO x1 VALUES('Cloudy, with a high near 16');
    124     INSERT INTO x1 VALUES('Wind chill values as low as -13');
    125   }
    126 
    127   do_trace_test 2.2 {
    128     INSERT INTO x1 VALUES('North northwest wind between 8 and 14 mph');
    129   } {
    130     "INSERT INTO x1 VALUES('North northwest wind between 8 and 14 mph');" 
    131     "-- INSERT INTO 'main'.'x1_content' VALUES(?,(?))" 
    132     "-- REPLACE INTO 'main'.'x1_docsize' VALUES(?,?)" 
    133     "-- SELECT value FROM 'main'.'x1_stat' WHERE id=0" 
    134     "-- REPLACE INTO 'main'.'x1_stat' VALUES(0,?)" 
    135     "-- SELECT (SELECT max(idx) FROM 'main'.'x1_segdir' WHERE level = ?) + 1" 
    136     "-- SELECT coalesce((SELECT max(blockid) FROM 'main'.'x1_segments') + 1, 1)"
    137     "-- INSERT INTO 'main'.'x1_segdir' VALUES(?,?,?,?,?,?)"
    138   }
    139 
    140   do_trace_test 2.3 {
    141     INSERT INTO x1(x1) VALUES('optimize');
    142   } {
    143     "INSERT INTO x1(x1) VALUES('optimize');"
    144     "-- SELECT idx, start_block, leaves_end_block, end_block, root FROM 'main'.'x1_segdir' ORDER BY level DESC, idx ASC"
    145     "-- SELECT count(*), max(level) FROM 'main'.'x1_segdir'"
    146     "-- SELECT coalesce((SELECT max(blockid) FROM 'main'.'x1_segments') + 1, 1)"
    147     "-- DELETE FROM 'main'.'x1_segdir'"
    148     "-- INSERT INTO 'main'.'x1_segdir' VALUES(?,?,?,?,?,?)"
    149   }
    150 }
    151 
    152 finish_test
    153