shaneh | 642d8b8 | 2010-07-28 16:05:34 +0000 | [diff] [blame^] | 1 | # 2010 July 28 |
| 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: shell4.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $ |
| 15 | # |
| 16 | |
| 17 | # Test plan: |
| 18 | # |
| 19 | # shell4-1.*: Basic tests specific to the "stats" command. |
| 20 | # |
| 21 | |
| 22 | package require sqlite3 |
| 23 | |
| 24 | set CLI "./sqlite3" |
| 25 | |
| 26 | proc do_test {name cmd expected} { |
| 27 | puts -nonewline "$name ..." |
| 28 | set res [uplevel $cmd] |
| 29 | if {$res eq $expected} { |
| 30 | puts Ok |
| 31 | } else { |
| 32 | puts Error |
| 33 | puts " Got: $res" |
| 34 | puts " Expected: $expected" |
| 35 | exit |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | proc execsql {sql} { |
| 40 | uplevel [list db eval $sql] |
| 41 | } |
| 42 | |
| 43 | proc catchsql {sql} { |
| 44 | set rc [catch {uplevel [list db eval $sql]} msg] |
| 45 | list $rc $msg |
| 46 | } |
| 47 | |
| 48 | proc catchcmd {db {cmd ""}} { |
| 49 | global CLI |
| 50 | set out [open cmds.txt w] |
| 51 | puts $out $cmd |
| 52 | close $out |
| 53 | set line "exec $CLI $db < cmds.txt" |
| 54 | set rc [catch { eval $line } msg] |
| 55 | list $rc $msg |
| 56 | } |
| 57 | |
| 58 | file delete -force test.db test.db.journal |
| 59 | sqlite3 db test.db |
| 60 | |
| 61 | #---------------------------------------------------------------------------- |
| 62 | # Test cases shell4-1.*: Tests specific to the "stats" command. |
| 63 | # |
| 64 | |
| 65 | # should default to off |
| 66 | do_test shell4-1.1.1 { |
| 67 | set res [catchcmd "test.db" ".show"] |
| 68 | list [regexp {stats: off} $res] |
| 69 | } {1} |
| 70 | |
| 71 | do_test shell4-1.1.2 { |
| 72 | set res [catchcmd "test.db" ".show"] |
| 73 | list [regexp {stats: on} $res] |
| 74 | } {0} |
| 75 | |
| 76 | # -stats should turn it on |
| 77 | do_test shell4-1.2.1 { |
| 78 | set res [catchcmd "-stats test.db" ".show"] |
| 79 | list [regexp {stats: on} $res] |
| 80 | } {1} |
| 81 | |
| 82 | do_test shell4-1.2.2 { |
| 83 | set res [catchcmd "-stats test.db" ".show"] |
| 84 | list [regexp {stats: off} $res] |
| 85 | } {0} |
| 86 | |
| 87 | # .stats ON|OFF Turn stats on or off |
| 88 | do_test shell4-1.3.1 { |
| 89 | catchcmd "test.db" ".stats" |
| 90 | } {1 {Error: unknown command or invalid arguments: "stats". Enter ".help" for help}} |
| 91 | do_test shell4-1.3.2 { |
| 92 | catchcmd "test.db" ".stats ON" |
| 93 | } {0 {}} |
| 94 | do_test shell4-1.3.3 { |
| 95 | catchcmd "test.db" ".stats OFF" |
| 96 | } {0 {}} |
| 97 | do_test shell4-1.3.4 { |
| 98 | # too many arguments |
| 99 | catchcmd "test.db" ".stats OFF BAD" |
| 100 | } {1 {Error: unknown command or invalid arguments: "stats". Enter ".help" for help}} |
| 101 | |
| 102 | # NB. whitespace is important |
| 103 | do_test shell4-1.4.1 { |
| 104 | set res [catchcmd "test.db" {.show}] |
| 105 | list [regexp {stats: off} $res] |
| 106 | } {1} |
| 107 | |
| 108 | do_test shell4-1.4.2 { |
| 109 | set res [catchcmd "test.db" {.stats ON |
| 110 | .show |
| 111 | }] |
| 112 | list [regexp {stats: on} $res] |
| 113 | } {1} |
| 114 | |
| 115 | do_test shell4-1.4.3 { |
| 116 | set res [catchcmd "test.db" {.stats OFF |
| 117 | .show |
| 118 | }] |
| 119 | list [regexp {stats: off} $res] |
| 120 | } {1} |
| 121 | |
| 122 | # make sure stats not present when off |
| 123 | do_test shell4-1.5.1 { |
| 124 | set res [catchcmd "test.db" {SELECT 1;}] |
| 125 | list [regexp {Mem Used} $res] \ |
| 126 | [regexp {Heap Usage} $res] \ |
| 127 | [regexp {Autoindex Inserts} $res] |
| 128 | } {0 0 0} |
| 129 | |
| 130 | # make sure stats are present when on |
| 131 | do_test shell4-1.5.2 { |
| 132 | set res [catchcmd "test.db" {.stats ON |
| 133 | SELECT 1; |
| 134 | }] |
| 135 | list [regexp {Mem Used} $res] \ |
| 136 | [regexp {Heap Usage} $res] \ |
| 137 | [regexp {Autoindex Inserts} $res] |
| 138 | } {1 1 1} |
| 139 | |
| 140 | puts "CLI tests completed successfully" |