blob: b4bef3afa4fa0bcc49adc86eaf8f5d5673785cbb [file] [log] [blame]
shaneh642d8b82010-07-28 16:05:34 +00001# 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
22package require sqlite3
23
24set CLI "./sqlite3"
25
26proc 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
39proc execsql {sql} {
40 uplevel [list db eval $sql]
41}
42
43proc catchsql {sql} {
44 set rc [catch {uplevel [list db eval $sql]} msg]
45 list $rc $msg
46}
47
48proc 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
58file delete -force test.db test.db.journal
59sqlite3 db test.db
60
61#----------------------------------------------------------------------------
62# Test cases shell4-1.*: Tests specific to the "stats" command.
63#
64
65# should default to off
66do_test shell4-1.1.1 {
67 set res [catchcmd "test.db" ".show"]
68 list [regexp {stats: off} $res]
69} {1}
70
71do_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
77do_test shell4-1.2.1 {
78 set res [catchcmd "-stats test.db" ".show"]
79 list [regexp {stats: on} $res]
80} {1}
81
82do_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
88do_test shell4-1.3.1 {
89 catchcmd "test.db" ".stats"
90} {1 {Error: unknown command or invalid arguments: "stats". Enter ".help" for help}}
91do_test shell4-1.3.2 {
92 catchcmd "test.db" ".stats ON"
93} {0 {}}
94do_test shell4-1.3.3 {
95 catchcmd "test.db" ".stats OFF"
96} {0 {}}
97do_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
103do_test shell4-1.4.1 {
104 set res [catchcmd "test.db" {.show}]
105 list [regexp {stats: off} $res]
106} {1}
107
108do_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
115do_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
123do_test shell4-1.5.1 {
124 set res [catchcmd "test.db" {SELECT 1;}]
drhabc97a72010-07-28 17:16:41 +0000125 list [regexp {Memory Used} $res] \
shaneh642d8b82010-07-28 16:05:34 +0000126 [regexp {Heap Usage} $res] \
127 [regexp {Autoindex Inserts} $res]
128} {0 0 0}
129
130# make sure stats are present when on
131do_test shell4-1.5.2 {
132 set res [catchcmd "test.db" {.stats ON
133SELECT 1;
134}]
drhabc97a72010-07-28 17:16:41 +0000135 list [regexp {Memory Used} $res] \
shaneh642d8b82010-07-28 16:05:34 +0000136 [regexp {Heap Usage} $res] \
137 [regexp {Autoindex Inserts} $res]
138} {1 1 1}
139
140puts "CLI tests completed successfully"