Home | History | Annotate | Download | only in tool
      1 # 2009 Dec 16
      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 #
     12 # The focus of this file is testing the CLI shell tool.
     13 #
     14 # $Id: shell2.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $
     15 #
     16 
     17 # Test plan:
     18 #
     19 #   shell3-1.*: Basic tests for running SQL statments from command line.
     20 #   shell3-2.*: Basic tests for running SQL file from command line.
     21 #
     22 
     23 package require sqlite3
     24 
     25 set CLI "./sqlite3"
     26 
     27 proc do_test {name cmd expected} {
     28   puts -nonewline "$name ..."
     29   set res [uplevel $cmd]
     30   if {$res eq $expected} {
     31     puts Ok
     32   } else {
     33     puts Error
     34     puts "  Got: $res"
     35     puts "  Expected: $expected"
     36     exit
     37   }
     38 }
     39 
     40 proc execsql {sql} {
     41   uplevel [list db eval $sql]
     42 }
     43 
     44 proc catchsql {sql} {
     45   set rc [catch {uplevel [list db eval $sql]} msg]
     46   list $rc $msg
     47 }
     48 
     49 proc catchcmd {db {cmd ""}} {
     50   global CLI
     51   set out [open cmds.txt w]
     52   puts $out $cmd
     53   close $out
     54   set line "exec $CLI $db < cmds.txt"
     55   set rc [catch { eval $line } msg]
     56   list $rc $msg
     57 }
     58 
     59 file delete -force test.db test.db.journal
     60 sqlite3 db test.db
     61 
     62 
     63 #----------------------------------------------------------------------------
     64 #   shell3-1.*: Basic tests for running SQL statments from command line.
     65 #
     66 
     67 # Run SQL statement from command line
     68 do_test shell3-1.1 {
     69   file delete -force foo.db
     70   set rc [ catchcmd "foo.db \"CREATE TABLE t1(a);\"" ]
     71   set fexist [file exist foo.db]
     72   list $rc $fexist
     73 } {{0 {}} 1}
     74 do_test shell3-1.2 {
     75   catchcmd "foo.db" ".tables"
     76 } {0 t1}
     77 do_test shell3-1.3 {
     78   catchcmd "foo.db \"DROP TABLE t1;\""
     79 } {0 {}}
     80 do_test shell3-1.4 {
     81   catchcmd "foo.db" ".tables"
     82 } {0 {}}
     83 do_test shell3-1.5 {
     84   catchcmd "foo.db \"CREATE TABLE t1(a); DROP TABLE t1;\""
     85 } {0 {}}
     86 do_test shell3-1.6 {
     87   catchcmd "foo.db" ".tables"
     88 } {0 {}}
     89 do_test shell3-1.7 {
     90   catchcmd "foo.db \"CREATE TABLE\""
     91 } {1 {Error: near "TABLE": syntax error}}
     92 
     93 #----------------------------------------------------------------------------
     94 #   shell3-2.*: Basic tests for running SQL file from command line.
     95 #
     96 
     97 # Run SQL file from command line
     98 do_test shell3-2.1 {
     99   file delete -force foo.db
    100   set rc [ catchcmd "foo.db" "CREATE TABLE t1(a);" ]
    101   set fexist [file exist foo.db]
    102   list $rc $fexist
    103 } {{0 {}} 1}
    104 do_test shell3-2.2 {
    105   catchcmd "foo.db" ".tables"
    106 } {0 t1}
    107 do_test shell3-2.3 {
    108   catchcmd "foo.db" "DROP TABLE t1;"
    109 } {0 {}}
    110 do_test shell3-2.4 {
    111   catchcmd "foo.db" ".tables"
    112 } {0 {}}
    113 do_test shell3-2.5 {
    114   catchcmd "foo.db" "CREATE TABLE t1(a); DROP TABLE t1;"
    115 } {0 {}}
    116 do_test shell3-2.6 {
    117   catchcmd "foo.db" ".tables"
    118 } {0 {}}
    119 do_test shell3-2.7 {
    120   catchcmd "foo.db" "CREATE TABLE"
    121 } {1 {Error: incomplete SQL: CREATE TABLE}}
    122 
    123 
    124 puts "CLI tests completed successfully"
    125