blob: 1a3b53249ef9b7dc5579ed72b8107d6fc36d0ae6 [file] [log] [blame]
shaneha05e0c42009-11-06 03:22:54 +00001# 2009 Nov 11
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: shell1.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $
15#
16
17# Test plan:
18#
19# shell-1.*: Basic test that command can be called.
20#
21
22package require sqlite3
23
24proc do_test {name cmd expected} {
25 puts -nonewline "$name ..."
26 set res [uplevel $cmd]
27 if {$res eq $expected} {
28 puts Ok
29 } else {
30 puts Error
31 puts " Got: $res"
32 puts " Expected: $expected"
33 }
34}
35
36proc execsql {sql} {
37 uplevel [list db eval $sql]
38}
39
40proc catchsql {sql} {
41 set rc [catch {uplevel [list db eval $sql]} msg]
42 list $rc $msg
43}
44
45proc catchcmd {cmd} {
46 set out [open cmds.txt w]
47 puts $out $cmd
48 close $out
49 set rc [catch { exec ./sqlite3 test.db < cmds.txt } msg]
50 list $rc $msg
51}
52
53file delete -force test.db test.db.journal
54sqlite3 db test.db
55
56#----------------------------------------------------------------------------
57# Test cases shell-1.* Basic test that command can be called.
58#
59
60# .backup ?DB? FILE Backup DB (default "main") to FILE
61do_test shell-1.1.1 {
62} {}
63
64# .bail ON|OFF Stop after hitting an error. Default OFF
65do_test shell-1.2.1 {
66 catchcmd ".bail"
67} {1 {Error: unknown command or invalid arguments: "bail". Enter ".help" for help}}
68do_test shell-1.2.2 {
69 catchcmd ".bail ON"
70} {0 {}}
71do_test shell-1.2.3 {
72 catchcmd ".bail OFF"
73} {0 {}}
74
75# .databases List names and files of attached databases
76do_test shell-1.3.1 {
77 set res [catchcmd ".databases"]
78 regexp {0.*main.*test\.db} $res
79} {1}
80
81# .dump ?TABLE? ... Dump the database in an SQL text format
82# If TABLE specified, only dump tables matching
83# LIKE pattern TABLE.
84do_test shell-1.4.1 {
85 set res [catchcmd ".dump"]
86 list [regexp {BEGIN TRANSACTION;} $res] \
87 [regexp {COMMIT;} $res]
88} {1 1}
89do_test shell-1.4.2 {
90 set res [catchcmd ".dump FOO"]
91 list [regexp {BEGIN TRANSACTION;} $res] \
92 [regexp {COMMIT;} $res]
93} {1 1}
94
95# .echo ON|OFF Turn command echo on or off
96do_test shell-1.5.1 {
97 catchcmd ".echo"
98} {1 {Error: unknown command or invalid arguments: "echo". Enter ".help" for help}}
99do_test shell-1.5.2 {
100 catchcmd ".echo ON"
101} {0 {}}
102do_test shell-1.5.3 {
103 catchcmd ".echo OFF"
104} {0 {}}
105
106# .exit Exit this program
107do_test shell-1.6.1 {
108 catchcmd ".exit"
109} {0 {}}
110
111# .explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
112do_test shell-1.7.1 {
113 catchcmd ".echo"
114} {1 {Error: unknown command or invalid arguments: "echo". Enter ".help" for help}}
115do_test shell-1.7.2 {
116 catchcmd ".explain ON"
117} {0 {}}
118do_test shell-1.7.3 {
119 catchcmd ".explain OFF"
120} {0 {}}
121
122# .genfkey ?OPTIONS? Options are:
123# --no-drop: Do not drop old fkey triggers.
124# --ignore-errors: Ignore tables with fkey errors
125# --exec: Execute generated SQL immediately
126# See file tool/genfkey.README in the source
127# distribution for further information.
128do_test shell-1.8.1 {
129} {}
130
131# .header(s) ON|OFF Turn display of headers on or off
132do_test shell-1.9.1 {
133 catchcmd ".header"
134} {1 {Error: unknown command or invalid arguments: "header". Enter ".help" for help}}
135do_test shell-1.9.2 {
136 catchcmd ".header ON"
137} {0 {}}
138do_test shell-1.9.3 {
139 catchcmd ".header OFF"
140} {0 {}}
141do_test shell-1.9.4 {
142 catchcmd ".headers"
143} {1 {Error: unknown command or invalid arguments: "headers". Enter ".help" for help}}
144do_test shell-1.9.5 {
145 catchcmd ".headers ON"
146} {0 {}}
147do_test shell-1.9.6 {
148 catchcmd ".headers OFF"
149} {0 {}}
150
151# .help Show this message
152do_test shell-1.10.1 {
153 set res [catchcmd ".help"]
154 # look for a few of the possible help commands
155 list [regexp {.help} $res] \
156 [regexp {.quit} $res] \
157 [regexp {.show} $res]
158} {1 1 1}
159
160# .import FILE TABLE Import data from FILE into TABLE
161do_test shell-1.11.1 {
162 catchcmd ".import"
163} {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
164do_test shell-1.11.2 {
165 catchcmd ".import FOO"
166} {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
167do_test shell-1.11.2 {
168 catchcmd ".import FOO BAR"
169} {1 {Error: no such table: BAR}}
170
171# .indices ?TABLE? Show names of all indices
172# If TABLE specified, only show indices for tables
173# matching LIKE pattern TABLE.
174do_test shell-1.12.1 {
175 catchcmd ".indices"
176} {0 {}}
177do_test shell-1.12.2 {
178 catchcmd ".indices FOO"
179} {0 {}}
180
181# .mode MODE ?TABLE? Set output mode where MODE is one of:
182# csv Comma-separated values
183# column Left-aligned columns. (See .width)
184# html HTML <table> code
185# insert SQL insert statements for TABLE
186# line One value per line
187# list Values delimited by .separator string
188# tabs Tab-separated values
189# tcl TCL list elements
190do_test shell-1.13.1 {
191 catchcmd ".mode"
192} {1 {Error: unknown command or invalid arguments: "mode". Enter ".help" for help}}
193do_test shell-1.13.2 {
194 catchcmd ".mode FOO"
195} {1 {Error: mode should be one of: column csv html insert line list tabs tcl}}
196do_test shell-1.13.3 {
197 catchcmd ".mode csv"
198} {0 {}}
199do_test shell-1.13.4 {
200 catchcmd ".mode column"
201} {0 {}}
202do_test shell-1.13.5 {
203 catchcmd ".mode html"
204} {0 {}}
205do_test shell-1.13.6 {
206 catchcmd ".mode insert"
207} {0 {}}
208do_test shell-1.13.7 {
209 catchcmd ".mode line"
210} {0 {}}
211do_test shell-1.13.8 {
212 catchcmd ".mode list"
213} {0 {}}
214do_test shell-1.13.9 {
215 catchcmd ".mode tabs"
216} {0 {}}
217do_test shell-1.13.10 {
218 catchcmd ".mode tcl"
219} {0 {}}
220
221# .nullvalue STRING Print STRING in place of NULL values
222do_test shell-1.14.1 {
223 catchcmd ".nullvalue"
224} {1 {Error: unknown command or invalid arguments: "nullvalue". Enter ".help" for help}}
225do_test shell-1.14.2 {
226 catchcmd ".nullvalue FOO"
227} {0 {}}
228
229# .output FILENAME Send output to FILENAME
230do_test shell-1.15.1 {
231 catchcmd ".output"
232} {1 {Error: unknown command or invalid arguments: "output". Enter ".help" for help}}
233do_test shell-1.15.2 {
234 catchcmd ".output FOO"
235} {0 {}}
236
237# .output stdout Send output to the screen
238do_test shell-1.16.1 {
239 catchcmd ".output stdout"
240} {0 {}}
241
242# .prompt MAIN CONTINUE Replace the standard prompts
243do_test shell-1.17.1 {
244 catchcmd ".prompt"
245} {1 {Error: unknown command or invalid arguments: "prompt". Enter ".help" for help}}
246do_test shell-1.17.2 {
247 catchcmd ".prompt FOO"
248} {0 {}}
249do_test shell-1.17.3 {
250 catchcmd ".prompt FOO BAR"
251} {0 {}}
252
253# .quit Exit this program
254do_test shell-1.18.1 {
255 catchcmd ".quit"
256} {0 {}}
257
258# .read FILENAME Execute SQL in FILENAME
259do_test shell-1.19.1 {
260 catchcmd ".read"
261} {1 {Error: unknown command or invalid arguments: "read". Enter ".help" for help}}
262do_test shell-1.19.2 {
263 file delete -force FOO
264 catchcmd ".read FOO"
265} {1 {Error: cannot open "FOO"}}
266
267# .restore ?DB? FILE Restore content of DB (default "main") from FILE
268do_test shell-1.20.1 {
269 catchcmd ".restore"
270} {1 {Error: unknown command or invalid arguments: "restore". Enter ".help" for help}}
271do_test shell-1.20.2 {
272 # catchcmd ".restore FOO"
273 #TBD!!! this asserts currently
274} {}
275
276# .schema ?TABLE? Show the CREATE statements
277# If TABLE specified, only show tables matching
278# LIKE pattern TABLE.
279do_test shell-1.21.1 {
280 catchcmd ".schema"
281} {0 {}}
282do_test shell-1.21.2 {
283 catchcmd ".schema FOO"
284} {0 {}}
285
286# .separator STRING Change separator used by output mode and .import
287do_test shell-1.22.1 {
288 catchcmd ".separator"
289} {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" for help}}
290do_test shell-1.22.2 {
291 catchcmd ".separator FOO"
292} {0 {}}
293
294# .show Show the current values for various settings
295do_test shell-1.23.1 {
296 set res [catchcmd ".show"]
297 list [regexp {echo:} $res] \
298 [regexp {explain:} $res] \
299 [regexp {headers:} $res] \
300 [regexp {mode:} $res] \
301 [regexp {nullvalue:} $res] \
302 [regexp {output:} $res] \
303 [regexp {separator:} $res] \
304 [regexp {width:} $res]
305} {1 1 1 1 1 1 1 1}
306
307# .tables ?TABLE? List names of tables
308# If TABLE specified, only list tables matching
309# LIKE pattern TABLE.
310do_test shell-1.24.1 {
311 catchcmd ".tables"
312} {0 {}}
313do_test shell-1.24.2 {
314 catchcmd ".tables FOO"
315} {0 {}}
316
317# .timeout MS Try opening locked tables for MS milliseconds
318do_test shell-1.25.1 {
319 catchcmd ".timeout"
320} {1 {Error: unknown command or invalid arguments: "timeout". Enter ".help" for help}}
321do_test shell-1.25.2 {
322 catchcmd ".timeout zzz"
323 #TBD!!! this should probably produce an error
324} {0 {}}
325do_test shell-1.25.2 {
326 catchcmd ".timeout 1"
327} {0 {}}
328
329# .width NUM NUM ... Set column widths for "column" mode
330do_test shell-1.26.1 {
331 catchcmd ".width"
332 #TBD!!! this should probably produce an error
333} {0 {}}
334do_test shell-1.26.2 {
335 catchcmd ".width xxx"
336 #TBD!!! this should probably produce an error
337} {0 {}}
338do_test shell-1.26.3 {
339 catchcmd ".width xxx yyy"
340 #TBD!!! this should probably produce an error
341} {0 {}}
342do_test shell-1.26.4 {
343 catchcmd ".width 1 1"
344} {0 {}}
345
346# .timer ON|OFF Turn the CPU timer measurement on or off
347do_test shell-1.27.1 {
348 catchcmd ".timer"
349} {1 {Error: unknown command or invalid arguments: "timer". Enter ".help" for help}}
350do_test shell-1.27.2 {
351 catchcmd ".timer ON"
352} {0 {}}
353do_test shell-1.27.3 {
354 catchcmd ".timer OFF"
355} {0 {}}
356
357#