xref: /sqlite-3.40.0/test/shell4.test (revision dedd51ae)
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# These tests are specific to the .stats command.
14#
15# 2015-03-19:  Added tests for .trace
16
17# Test plan:
18#
19#   shell4-1.*: Basic tests specific to the "stats" command.
20#   shell4-2.*: Basic tests for ".trace"
21#   shell4-3.*: The ".read" command takes the shell out of interactive mode
22#   shell4-4.*: Input redirects cannot recurse too much
23#
24set testdir [file dirname $argv0]
25source $testdir/tester.tcl
26set CLI [test_find_cli]
27db close
28forcedelete test.db test.db-journal test.db-wal
29sqlite3 db test.db
30
31#----------------------------------------------------------------------------
32# Test cases shell4-1.*: Tests specific to the "stats" command.
33#
34
35# should default to off
36do_test shell4-1.1.1 {
37  set res [catchcmd "test.db" ".show"]
38  list [regexp {stats: off} $res]
39} {1}
40
41do_test shell4-1.1.2 {
42  set res [catchcmd "test.db" ".show"]
43  list [regexp {stats: on} $res]
44} {0}
45
46# -stats should turn it on
47do_test shell4-1.2.1 {
48  set res [catchcmd "-stats test.db" ".show"]
49  list [regexp {stats: on} $res]
50} {1}
51
52do_test shell4-1.2.2 {
53  set res [catchcmd "-stats test.db" ".show"]
54  list [regexp {stats: off} $res]
55} {0}
56
57# .stats ON|OFF          Turn stats on or off
58#do_test shell4-1.3.1 {
59#  catchcmd "test.db" ".stats"
60#} {1 {Usage: .stats on|off}}
61do_test shell4-1.3.2 {
62  catchcmd "test.db" ".stats ON"
63} {0 {}}
64do_test shell4-1.3.3 {
65  catchcmd "test.db" ".stats OFF"
66} {0 {}}
67do_test shell4-1.3.4 {
68  # too many arguments
69  catchcmd "test.db" ".stats OFF BAD"
70} {1 {Usage: .stats ?on|off|stmt|vmstep?}}
71
72# NB. whitespace is important
73do_test shell4-1.4.1 {
74  set res [catchcmd "test.db" {.show}]
75  list [regexp {stats: off} $res]
76} {1}
77
78do_test shell4-1.4.2 {
79  set res [catchcmd "test.db" {.stats ON
80.show
81}]
82  list [regexp {stats: on} $res]
83} {1}
84
85do_test shell4-1.4.3 {
86  set res [catchcmd "test.db" {.stats OFF
87.show
88}]
89  list [regexp {stats: off} $res]
90} {1}
91
92# make sure stats not present when off
93do_test shell4-1.5.1 {
94  set res [catchcmd "test.db" {SELECT 1;}]
95  list [regexp {Memory Used} $res] \
96       [regexp {Heap Usage} $res] \
97       [regexp {Autoindex Inserts} $res]
98} {0 0 0}
99
100# make sure stats are present when on
101do_test shell4-1.5.2 {
102  set res [catchcmd "test.db" {.stats ON
103SELECT 1;
104}]
105  list [regexp {Memory Used} $res] \
106       [regexp {Heap Usage} $res] \
107       [regexp {Autoindex Inserts} $res]
108} {1 1 1}
109
110ifcapable trace {
111do_test shell4-2.1 {
112  catchcmd ":memory:" "CREATE TABLE t1(x);\n.trace --unknown"
113} {1 {Unknown option "--unknown" on ".trace"}}
114do_test shell4-2.2 {
115  catchcmd ":memory:" "CREATE TABLE t1(x);\n.trace off\n.trace off\n"
116} {0 {}}
117do_test shell4-2.3 {
118  catchcmd ":memory:" ".trace stdout\n.dump\n.trace off\n"
119} {/^0 {PRAGMA.*}$/}
120do_test shell4-2.4 {
121  catchcmd ":memory:" ".trace stdout\nCREATE TABLE t1(x);SELECT * FROM t1;"
122} {0 {CREATE TABLE t1(x);
123SELECT * FROM t1;}}
124do_test shell4-2.5 {
125  catchcmd ":memory:" "CREATE TABLE t1(x);\n.trace stdout\nSELECT * FROM t1;"
126} {0 {SELECT * FROM t1;}}
127}
128
129do_test shell4-3.1 {
130  set fd [open t1.txt wb]
131  puts $fd "SELECT 'squirrel';"
132  close $fd
133  exec $::CLI :memory: --interactive ".read t1.txt"
134} {squirrel}
135do_test shell4-3.2 {
136  set fd [open t1.txt wb]
137  puts $fd "SELECT 'pound: \302\243';"
138  close $fd
139  exec $::CLI :memory: --interactive ".read t1.txt"
140} {pound: £}
141
142do_test shell4-4.1 {
143  set fd [open t1.txt wb]
144  puts $fd ".read t1.txt"
145  close $fd
146  catchcmd ":memory:" ".read t1.txt"
147} {1 {Input nesting limit (25) reached at line 1. Check recursion.}}
148
149finish_test
150