# 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 "dot" command token parsing. # shell1-2.*: Basic test that "dot" command can be called. # package require 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 {cmd} { set out [open cmds.txt w] puts $out $cmd close $out set rc [catch { exec ./sqlite test.db < cmds.txt } msg] list $rc $msg } file delete -force test.db test.db.journal sqlite3 db test.db #---------------------------------------------------------------------------- # Test cases shell1-1.* Basic "dot" command token parsing. # # check first token handling do_test shell1-1.1.1 { catchcmd ".foo" } {1 {Error: unknown command or invalid arguments: "foo". Enter ".help" for help}} do_test shell1-1.1.2 { catchcmd ".\"foo OFF\"" } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}} do_test shell1-1.1.3 { catchcmd ".\'foo OFF\'" } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}} # unbalanced quotes do_test shell1-1.2.1 { catchcmd ".\"foo OFF" } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}} do_test shell1-1.2.2 { catchcmd ".\'foo OFF" } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}} do_test shell1-1.2.3 { catchcmd ".explain \"OFF" } {0 {}} do_test shell1-1.2.4 { catchcmd ".explain \'OFF" } {0 {}} do_test shell1-1.2.5 { catchcmd ".mode \"insert FOO" } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}} do_test shell1-1.2.6 { catchcmd ".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-1.3.1 { catchcmd ".explain 1" } {0 {}} do_test shell1-1.3.2 { catchcmd ".explain on" } {0 {}} do_test shell1-1.3.3 { catchcmd ".explain \"1 2 3\"" } {0 {}} do_test shell1-1.3.4 { catchcmd ".explain \"OFF\"" } {0 {}} do_test shell1-1.3.5 { catchcmd ".\'explain\' \'OFF\'" } {0 {}} do_test shell1-1.3.6 { catchcmd ".explain \'OFF\'" } {0 {}} do_test shell1-1.3.7 { catchcmd ".\'explain\' \'OFF\'" } {0 {}} # check quoted args are unquoted do_test shell1-1.4.1 { catchcmd ".mode FOO" } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}} do_test shell1-1.4.2 { catchcmd ".mode csv" } {0 {}} do_test shell1-1.4.2 { catchcmd ".mode \"csv\"" } {0 {}} #---------------------------------------------------------------------------- # Test cases shell1-2.* Basic test that "dot" command can be called. # # .backup ?DB? FILE Backup DB (default "main") to FILE do_test shell1-2.1.1 { catchcmd ".backup" } {1 {Error: unknown command or invalid arguments: "backup". Enter ".help" for help}} do_test shell1-2.1.2 { # catchcmd ".backup FOO" #TBD!!! this asserts currently } {} do_test shell1-2.1.3 { catchcmd ".backup FOO BAR" } {1 {Error: unknown database FOO}} do_test shell1-2.1.4 { # too many arguments catchcmd ".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-2.2.1 { catchcmd ".bail" } {1 {Error: unknown command or invalid arguments: "bail". Enter ".help" for help}} do_test shell1-2.2.2 { catchcmd ".bail ON" } {0 {}} do_test shell1-2.2.3 { catchcmd ".bail OFF" } {0 {}} do_test shell1-2.2.4 { # too many arguments catchcmd ".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-2.3.1 { set res [catchcmd ".databases"] regexp {0.*main.*test\.db} $res } {1} do_test shell1-2.3.2 { # too many arguments catchcmd ".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-2.4.1 { set res [catchcmd ".dump"] list [regexp {BEGIN TRANSACTION;} $res] \ [regexp {COMMIT;} $res] } {1 1} do_test shell1-2.4.2 { set res [catchcmd ".dump FOO"] list [regexp {BEGIN TRANSACTION;} $res] \ [regexp {COMMIT;} $res] } {1 1} do_test shell1-2.4.3 { # too many arguments catchcmd ".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-2.5.1 { catchcmd ".echo" } {1 {Error: unknown command or invalid arguments: "echo". Enter ".help" for help}} do_test shell1-2.5.2 { catchcmd ".echo ON" } {0 {}} do_test shell1-2.5.3 { catchcmd ".echo OFF" } {0 {}} do_test shell1-2.5.4 { # too many arguments catchcmd ".echo OFF BAD" } {1 {Error: unknown command or invalid arguments: "echo". Enter ".help" for help}} # .exit Exit this program do_test shell1-2.6.1 { catchcmd ".exit" } {0 {}} do_test shell1-2.6.2 { # too many arguments catchcmd ".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-2.7.1 { catchcmd ".explain" # explain is the exception to the booleans. without an option, it turns it on. } {0 {}} do_test shell1-2.7.2 { catchcmd ".explain ON" } {0 {}} do_test shell1-2.7.3 { catchcmd ".explain OFF" } {0 {}} do_test shell1-2.7.4 { # too many arguments catchcmd ".explain OFF BAD" } {1 {Error: unknown command or invalid arguments: "explain". Enter ".help" for help}} # .genfkey ?OPTIONS? Options are: # --no-drop: Do not drop old fkey triggers. # --ignore-errors: Ignore tables with fkey errors # --exec: Execute generated SQL immediately # See file tool/genfkey.README in the source # distribution for further information. do_test shell1-2.8.1 { catchcmd ".genfkey" } {0 {}} do_test shell1-2.8.2 { catchcmd ".genfkey FOO" } {1 {unknown option: FOO}} # .header(s) ON|OFF Turn display of headers on or off do_test shell1-2.9.1 { catchcmd ".header" } {1 {Error: unknown command or invalid arguments: "header". Enter ".help" for help}} do_test shell1-2.9.2 { catchcmd ".header ON" } {0 {}} do_test shell1-2.9.3 { catchcmd ".header OFF" } {0 {}} do_test shell1-2.9.4 { # too many arguments catchcmd ".header OFF BAD" } {1 {Error: unknown command or invalid arguments: "header". Enter ".help" for help}} do_test shell1-2.9.5 { catchcmd ".headers" } {1 {Error: unknown command or invalid arguments: "headers". Enter ".help" for help}} do_test shell1-2.9.6 { catchcmd ".headers ON" } {0 {}} do_test shell1-2.9.7 { catchcmd ".headers OFF" } {0 {}} do_test shell1-2.9.8 { # too many arguments catchcmd ".headers OFF BAD" } {1 {Error: unknown command or invalid arguments: "headers". Enter ".help" for help}} # .help Show this message do_test shell1-2.10.1 { set res [catchcmd ".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-2.10.2 { # we allow .help to take extra args (it is help after all) set res [catchcmd ".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-2.11.1 { catchcmd ".import" } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}} do_test shell1-2.11.2 { catchcmd ".import FOO" } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}} do_test shell1-2.11.2 { catchcmd ".import FOO BAR" } {1 {Error: no such table: BAR}} do_test shell1-2.11.3 { # too many arguments catchcmd ".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-2.12.1 { catchcmd ".indices" } {0 {}} do_test shell1-2.12.2 { catchcmd ".indices FOO" } {0 {}} do_test shell1-2.12.3 { # too many arguments catchcmd ".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