# 2009 Nov 11 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # # The focus of this file is testing the CLI shell tool. # # $Id: shell1.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $ # # Test plan: # # shell1-1.*: Basic command line option handling. # shell1-2.*: Basic "dot" command token parsing. # shell1-3.*: Basic test that "dot" command can be called. # package require sqlite3 set CLI "./sqlite3" proc do_test {name cmd expected} { puts -nonewline "$name ..." set res [uplevel $cmd] if {$res eq $expected} { puts Ok } else { puts Error puts " Got: $res" puts " Expected: $expected" exit } } proc execsql {sql} { uplevel [list db eval $sql] } proc catchsql {sql} { set rc [catch {uplevel [list db eval $sql]} msg] list $rc $msg } proc catchcmd {db {cmd ""}} { global CLI set out [open cmds.txt w] puts $out $cmd close $out set line "exec $CLI $db < cmds.txt" set rc [catch { eval $line } msg] list $rc $msg } file delete -force test.db test.db.journal sqlite3 db test.db #---------------------------------------------------------------------------- # Test cases shell1-1.*: Basic command line option handling. # # invalid option do_test shell1-1.1.1 { set res [catchcmd "-bad test.db" ""] set rc [lindex $res 0] list $rc \ [regexp {Error: unknown option: -bad} $res] } {1 1} # error on extra options do_test shell1-1.1.2 { set res [catchcmd "-bad test.db \"select 3\" \"select 4\"" ""] set rc [lindex $res 0] list $rc \ [regexp {Error: too many options: "select 4"} $res] } {1 1} # error on extra options do_test shell1-1.1.3 { set res [catchcmd "-bad FOO test.db BAD" ".quit"] set rc [lindex $res 0] list $rc \ [regexp {Error: too many options: "BAD"} $res] } {1 1} # -help do_test shell1-1.2.1 { set res [catchcmd "-help test.db" ""] set rc [lindex $res 0] list $rc \ [regexp {Usage} $res] \ [regexp {\-init} $res] \ [regexp {\-version} $res] } {1 1 1 1} # -init filename read/process named file do_test shell1-1.3.1 { catchcmd "-init FOO test.db" "" } {0 {}} do_test shell1-1.3.2 { set res [catchcmd "-init FOO test.db .quit BAD" ""] set rc [lindex $res 0] list $rc \ [regexp {Error: too many options: "BAD"} $res] } {1 1} # -echo print commands before execution do_test shell1-1.4.1 { catchcmd "-echo test.db" "" } {0 {}} # -[no]header turn headers on or off do_test shell1-1.5.1 { catchcmd "-header test.db" "" } {0 {}} do_test shell1-1.5.2 { catchcmd "-noheader test.db" "" } {0 {}} # -bail stop after hitting an error do_test shell1-1.6.1 { catchcmd "-bail test.db" "" } {0 {}} # -interactive force interactive I/O do_test shell1-1.7.1 { set res [catchcmd "-interactive test.db" ".quit"] set rc [lindex $res 0] list $rc \ [regexp {SQLite version} $res] \ [regexp {Enter SQL statements} $res] } {0 1 1} # -batch force batch I/O do_test shell1-1.8.1 { catchcmd "-batch test.db" "" } {0 {}} # -column set output mode to 'column' do_test shell1-1.9.1 { catchcmd "-column test.db" "" } {0 {}} # -csv set output mode to 'csv' do_test shell1-1.10.1 { catchcmd "-csv test.db" "" } {0 {}} # -html set output mode to HTML do_test shell1-1.11.1 { catchcmd "-html test.db" "" } {0 {}} # -line set output mode to 'line' do_test shell1-1.12.1 { catchcmd "-line test.db" "" } {0 {}} # -list set output mode to 'list' do_test shell1-1.13.1 { catchcmd "-list test.db" "" } {0 {}} # -separator 'x' set output field separator (|) do_test shell1-1.14.1 { catchcmd "-separator 'x' test.db" "" } {0 {}} do_test shell1-1.14.2 { catchcmd "-separator x test.db" "" } {0 {}} do_test shell1-1.14.3 { set res [catchcmd "-separator" ""] set rc [lindex $res 0] list $rc \ [regexp {Error: missing argument for option: -separator} $res] } {1 1} # -stats print memory stats before each finalize do_test shell1-1.14b.1 { catchcmd "-stats test.db" "" } {0 {}} # -nullvalue 'text' set text string for NULL values do_test shell1-1.15.1 { catchcmd "-nullvalue 'x' test.db" "" } {0 {}} do_test shell1-1.15.2 { catchcmd "-nullvalue x test.db" "" } {0 {}} do_test shell1-1.15.3 { set res [catchcmd "-nullvalue" ""] set rc [lindex $res 0] list $rc \ [regexp {Error: missing argument for option: -nullvalue} $res] } {1 1} # -version show SQLite version do_test shell1-1.16.1 { catchcmd "-version test.db" "" } {0 3.7.6.3} #---------------------------------------------------------------------------- # Test cases shell1-2.*: Basic "dot" command token parsing. # # check first token handling do_test shell1-2.1.1 { catchcmd "test.db" ".foo" } {1 {Error: unknown command or invalid arguments: "foo". Enter ".help" for help}} do_test shell1-2.1.2 { catchcmd "test.db" ".\"foo OFF\"" } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}} do_test shell1-2.1.3 { catchcmd "test.db" ".\'foo OFF\'" } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}} # unbalanced quotes do_test shell1-2.2.1 { catchcmd "test.db" ".\"foo OFF" } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}} do_test shell1-2.2.2 { catchcmd "test.db" ".\'foo OFF" } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}} do_test shell1-2.2.3 { catchcmd "test.db" ".explain \"OFF" } {0 {}} do_test shell1-2.2.4 { catchcmd "test.db" ".explain \'OFF" } {0 {}} do_test shell1-2.2.5 { catchcmd "test.db" ".mode \"insert FOO" } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}} do_test shell1-2.2.6 { catchcmd "test.db" ".mode \'insert FOO" } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}} # check multiple tokens, and quoted tokens do_test shell1-2.3.1 { catchcmd "test.db" ".explain 1" } {0 {}} do_test shell1-2.3.2 { catchcmd "test.db" ".explain on" } {0 {}} do_test shell1-2.3.3 { catchcmd "test.db" ".explain \"1 2 3\"" } {0 {}} do_test shell1-2.3.4 { catchcmd "test.db" ".explain \"OFF\"" } {0 {}} do_test shell1-2.3.5 { catchcmd "test.db" ".\'explain\' \'OFF\'" } {0 {}} do_test shell1-2.3.6 { catchcmd "test.db" ".explain \'OFF\'" } {0 {}} do_test shell1-2.3.7 { catchcmd "test.db" ".\'explain\' \'OFF\'" } {0 {}} # check quoted args are unquoted do_test shell1-2.4.1 { catchcmd "test.db" ".mode FOO" } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}} do_test shell1-2.4.2 { catchcmd "test.db" ".mode csv" } {0 {}} do_test shell1-2.4.2 { catchcmd "test.db" ".mode \"csv\"" } {0 {}} #---------------------------------------------------------------------------- # Test cases shell1-3.*: Basic test that "dot" command can be called. # # .backup ?DB? FILE Backup DB (default "main") to FILE do_test shell1-3.1.1 { catchcmd "test.db" ".backup" } {1 {Error: unknown command or invalid arguments: "backup". Enter ".help" for help}} do_test shell1-3.1.2 { catchcmd "test.db" ".backup FOO" } {0 {}} do_test shell1-3.1.3 { catchcmd "test.db" ".backup FOO BAR" } {1 {Error: unknown database FOO}} do_test shell1-3.1.4 { # too many arguments catchcmd "test.db" ".backup FOO BAR BAD" } {1 {Error: unknown command or invalid arguments: "backup". Enter ".help" for help}} # .bail ON|OFF Stop after hitting an error. Default OFF do_test shell1-3.2.1 { catchcmd "test.db" ".bail" } {1 {Error: unknown command or invalid arguments: "bail". Enter ".help" for help}} do_test shell1-3.2.2 { catchcmd "test.db" ".bail ON" } {0 {}} do_test shell1-3.2.3 { catchcmd "test.db" ".bail OFF" } {0 {}} do_test shell1-3.2.4 { # too many arguments catchcmd "test.db" ".bail OFF BAD" } {1 {Error: unknown command or invalid arguments: "bail". Enter ".help" for help}} # .databases List names and files of attached databases do_test shell1-3.3.1 { set res [catchcmd "test.db" ".databases"] regexp {0.*main.*test\.db} $res } {1} do_test shell1-3.3.2 { # too many arguments catchcmd "test.db" ".databases BAD" } {1 {Error: unknown command or invalid arguments: "databases". Enter ".help" for help}} # .dump ?TABLE? ... Dump the database in an SQL text format # If TABLE specified, only dump tables matching # LIKE pattern TABLE. do_test shell1-3.4.1 { set res [catchcmd "test.db" ".dump"] list [regexp {BEGIN TRANSACTION;} $res] \ [regexp {COMMIT;} $res] } {1 1} do_test shell1-3.4.2 { set res [catchcmd "test.db" ".dump FOO"] list [regexp {BEGIN TRANSACTION;} $res] \ [regexp {COMMIT;} $res] } {1 1} do_test shell1-3.4.3 { # too many arguments catchcmd "test.db" ".dump FOO BAD" } {1 {Error: unknown command or invalid arguments: "dump". Enter ".help" for help}} # .echo ON|OFF Turn command echo on or off do_test shell1-3.5.1 { catchcmd "test.db" ".echo" } {1 {Error: unknown command or invalid arguments: "echo". Enter ".help" for help}} do_test shell1-3.5.2 { catchcmd "test.db" ".echo ON" } {0 {}} do_test shell1-3.5.3 { catchcmd "test.db" ".echo OFF" } {0 {}} do_test shell1-3.5.4 { # too many arguments catchcmd "test.db" ".echo OFF BAD" } {1 {Error: unknown command or invalid arguments: "echo". Enter ".help" for help}} # .exit Exit this program do_test shell1-3.6.1 { catchcmd "test.db" ".exit" } {0 {}} do_test shell1-3.6.2 { # too many arguments catchcmd "test.db" ".exit BAD" } {1 {Error: unknown command or invalid arguments: "exit". Enter ".help" for help}} # .explain ON|OFF Turn output mode suitable for EXPLAIN on or off. do_test shell1-3.7.1 { catchcmd "test.db" ".explain" # explain is the exception to the booleans. without an option, it turns it on. } {0 {}} do_test shell1-3.7.2 { catchcmd "test.db" ".explain ON" } {0 {}} do_test shell1-3.7.3 { catchcmd "test.db" ".explain OFF" } {0 {}} do_test shell1-3.7.4 { # too many arguments catchcmd "test.db" ".explain OFF BAD" } {1 {Error: unknown command or invalid arguments: "explain". Enter ".help" for help}} # .header(s) ON|OFF Turn display of headers on or off do_test shell1-3.9.1 { catchcmd "test.db" ".header" } {1 {Error: unknown command or invalid arguments: "header". Enter ".help" for help}} do_test shell1-3.9.2 { catchcmd "test.db" ".header ON" } {0 {}} do_test shell1-3.9.3 { catchcmd "test.db" ".header OFF" } {0 {}} do_test shell1-3.9.4 { # too many arguments catchcmd "test.db" ".header OFF BAD" } {1 {Error: unknown command or invalid arguments: "header". Enter ".help" for help}} do_test shell1-3.9.5 { catchcmd "test.db" ".headers" } {1 {Error: unknown command or invalid arguments: "headers". Enter ".help" for help}} do_test shell1-3.9.6 { catchcmd "test.db" ".headers ON" } {0 {}} do_test shell1-3.9.7 { catchcmd "test.db" ".headers OFF" } {0 {}} do_test shell1-3.9.8 { # too many arguments catchcmd "test.db" ".headers OFF BAD" } {1 {Error: unknown command or invalid arguments: "headers". Enter ".help" for help}} # .help Show this message do_test shell1-3.10.1 { set res [catchcmd "test.db" ".help"] # look for a few of the possible help commands list [regexp {.help} $res] \ [regexp {.quit} $res] \ [regexp {.show} $res] } {1 1 1} do_test shell1-3.10.2 { # we allow .help to take extra args (it is help after all) set res [catchcmd "test.db" ".help BAD"] # look for a few of the possible help commands list [regexp {.help} $res] \ [regexp {.quit} $res] \ [regexp {.show} $res] } {1 1 1} # .import FILE TABLE Import data from FILE into TABLE do_test shell1-3.11.1 { catchcmd "test.db" ".import" } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}} do_test shell1-3.11.2 { catchcmd "test.db" ".import FOO" } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}} do_test shell1-3.11.2 { catchcmd "test.db" ".import FOO BAR" } {1 {Error: no such table: BAR}} do_test shell1-3.11.3 { # too many arguments catchcmd "test.db" ".import FOO BAR BAD" } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}} # .indices ?TABLE? Show names of all indices # If TABLE specified, only show indices for tables # matching LIKE pattern TABLE. do_test shell1-3.12.1 { catchcmd "test.db" ".indices" } {0 {}} do_test shell1-3.12.2 { catchcmd "test.db" ".indices FOO" } {0 {}} do_test shell1-3.12.3 { # too many arguments catchcmd "test.db" ".indices FOO BAD" } {1 {Error: unknown command or invalid arguments: "indices". Enter ".help" for help}} # .mode MODE ?TABLE? Set output mode where MODE is one of: # csv Comma-separated values # column Left-aligned columns. (See .width) # html HTML