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 23package require sqlite3 24 25set CLI "./sqlite3" 26 27proc 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 40proc execsql {sql} { 41 uplevel [list db eval $sql] 42} 43 44proc catchsql {sql} { 45 set rc [catch {uplevel [list db eval $sql]} msg] 46 list $rc $msg 47} 48 49proc 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 59file delete -force test.db test.db.journal 60sqlite3 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 68do_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} 74do_test shell3-1.2 { 75 catchcmd "foo.db" ".tables" 76} {0 t1} 77do_test shell3-1.3 { 78 catchcmd "foo.db \"DROP TABLE t1;\"" 79} {0 {}} 80do_test shell3-1.4 { 81 catchcmd "foo.db" ".tables" 82} {0 {}} 83do_test shell3-1.5 { 84 catchcmd "foo.db \"CREATE TABLE t1(a); DROP TABLE t1;\"" 85} {0 {}} 86do_test shell3-1.6 { 87 catchcmd "foo.db" ".tables" 88} {0 {}} 89do_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 98do_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} 104do_test shell3-2.2 { 105 catchcmd "foo.db" ".tables" 106} {0 t1} 107do_test shell3-2.3 { 108 catchcmd "foo.db" "DROP TABLE t1;" 109} {0 {}} 110do_test shell3-2.4 { 111 catchcmd "foo.db" ".tables" 112} {0 {}} 113do_test shell3-2.5 { 114 catchcmd "foo.db" "CREATE TABLE t1(a); DROP TABLE t1;" 115} {0 {}} 116do_test shell3-2.6 { 117 catchcmd "foo.db" ".tables" 118} {0 {}} 119do_test shell3-2.7 { 120 catchcmd "foo.db" "CREATE TABLE" 121} {1 {Error: incomplete SQL: CREATE TABLE}} 122 123 124puts "CLI tests completed successfully" 125