blob: 40d23c34e6acd0c1563b5300119d11ac00843510 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh960e8c62001-04-03 16:53:21 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh960e8c62001-04-03 16:53:21 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh960e8c62001-04-03 16:53:21 +00009#
10#***********************************************************************
11# This file implements regression tests for TCL interface to the
12# SQLite library.
13#
14# Actually, all tests are based on the TCL interface, so the main
15# interface is pretty well tested. This file contains some addition
16# tests for fringe issues that the main test suite does not cover.
17#
drhfd241b02004-09-13 13:46:01 +000018# $Id: tclsqlite.test,v 1.32 2004/09/13 13:46:01 drh Exp $
drh960e8c62001-04-03 16:53:21 +000019
20set testdir [file dirname $argv0]
21source $testdir/tester.tcl
22
23# Check the error messages generated by tclsqlite
24#
drhef4ac8f2004-06-19 00:16:31 +000025if {[sqlite3 -has-codec]} {
drh9eb9e262004-02-11 02:18:05 +000026 set r "sqlite_orig HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +000027} else {
drhef4ac8f2004-06-19 00:16:31 +000028 set r "sqlite3 HANDLE FILENAME ?MODE?"
drh22fbcb82004-02-01 01:22:50 +000029}
drh960e8c62001-04-03 16:53:21 +000030do_test tcl-1.1 {
drhef4ac8f2004-06-19 00:16:31 +000031 set v [catch {sqlite3 bogus} msg]
drh960e8c62001-04-03 16:53:21 +000032 lappend v $msg
drh22fbcb82004-02-01 01:22:50 +000033} [list 1 "wrong # args: should be \"$r\""]
drh960e8c62001-04-03 16:53:21 +000034do_test tcl-1.2 {
35 set v [catch {db bogus} msg]
36 lappend v $msg
drh0f14e2e2004-06-29 12:39:08 +000037} {1 {bad option "bogus": must be authorizer, busy, changes, close, collate, collation_needed, commit_hook, complete, errorcode, eval, function, last_insert_rowid, onecolumn, progress, rekey, timeout, total_changes, or trace}}
drh960e8c62001-04-03 16:53:21 +000038do_test tcl-1.3 {
39 execsql {CREATE TABLE t1(a int, b int)}
40 execsql {INSERT INTO t1 VALUES(10,20)}
41 set v [catch {
42 db eval {SELECT * FROM t1} data {
43 error "The error message"
44 }
45 } msg]
46 lappend v $msg
47} {1 {The error message}}
48do_test tcl-1.4 {
49 set v [catch {
50 db eval {SELECT * FROM t2} data {
51 error "The error message"
52 }
53 } msg]
54 lappend v $msg
55} {1 {no such table: t2}}
56do_test tcl-1.5 {
57 set v [catch {
58 db eval {SELECT * FROM t1} data {
59 break
60 }
61 } msg]
62 lappend v $msg
63} {0 {}}
64do_test tcl-1.6 {
65 set v [catch {
66 db eval {SELECT * FROM t1} data {
67 expr x*
68 }
69 } msg]
drha297b5c2002-01-15 18:39:43 +000070 regsub {:.*$} $msg {} msg
drh960e8c62001-04-03 16:53:21 +000071 lappend v $msg
72} {1 {syntax error in expression "x*"}}
drh0f14e2e2004-06-29 12:39:08 +000073do_test tcl-1.7 {
74 set v [catch {db} msg]
75 lappend v $msg
76} {1 {wrong # args: should be "db SUBCOMMAND ..."}}
drh1211de32004-07-26 12:24:22 +000077if {[catch {db auth {}}]==0} {
78 do_test tcl-1.8 {
79 set v [catch {db authorizer 1 2 3} msg]
80 lappend v $msg
81 } {1 {wrong # args: should be "db authorizer ?CALLBACK?"}}
82}
drh0f14e2e2004-06-29 12:39:08 +000083do_test tcl-1.9 {
84 set v [catch {db busy 1 2 3} msg]
85 lappend v $msg
86} {1 {wrong # args: should be "db busy CALLBACK"}}
87do_test tcl-1.10 {
88 set v [catch {db progress 1} msg]
89 lappend v $msg
90} {1 {wrong # args: should be "db progress N CALLBACK"}}
91do_test tcl-1.11 {
92 set v [catch {db changes xyz} msg]
93 lappend v $msg
94} {1 {wrong # args: should be "db changes "}}
95do_test tcl-1.12 {
96 set v [catch {db commit_hook a b c} msg]
97 lappend v $msg
98} {1 {wrong # args: should be "db commit_hook ?CALLBACK?"}}
99do_test tcl-1.13 {
100 set v [catch {db complete} msg]
101 lappend v $msg
102} {1 {wrong # args: should be "db complete SQL"}}
103do_test tcl-1.14 {
104 set v [catch {db eval} msg]
105 lappend v $msg
drh895d7472004-08-20 16:02:39 +0000106} {1 {wrong # args: should be "db eval SQL ?ARRAY-NAME? ?SCRIPT?"}}
drh0f14e2e2004-06-29 12:39:08 +0000107do_test tcl-1.15 {
108 set v [catch {db function} msg]
109 lappend v $msg
110} {1 {wrong # args: should be "db function NAME SCRIPT"}}
111do_test tcl-1.14 {
112 set v [catch {db last_insert_rowid xyz} msg]
113 lappend v $msg
114} {1 {wrong # args: should be "db last_insert_rowid "}}
115do_test tcl-1.15 {
116 set v [catch {db rekey} msg]
117 lappend v $msg
118} {1 {wrong # args: should be "db rekey KEY"}}
119do_test tcl-1.16 {
120 set v [catch {db timeout} msg]
121 lappend v $msg
122} {1 {wrong # args: should be "db timeout MILLISECONDS"}}
123do_test tcl-1.17 {
124 set v [catch {db collate} msg]
125 lappend v $msg
126} {1 {wrong # args: should be "db collate NAME SCRIPT"}}
127do_test tcl-1.18 {
128 set v [catch {db collation_needed} msg]
129 lappend v $msg
130} {1 {wrong # args: should be "db collation_needed SCRIPT"}}
131do_test tcl-1.19 {
132 set v [catch {db total_changes xyz} msg]
133 lappend v $msg
134} {1 {wrong # args: should be "db total_changes "}}
135
drh960e8c62001-04-03 16:53:21 +0000136
drhef4ac8f2004-06-19 00:16:31 +0000137if {[sqlite3 -tcl-uses-utf]} {
drhc275b4e2004-07-19 17:25:24 +0000138 catch {unset ::result}
drh6d4abfb2001-10-22 02:58:08 +0000139 do_test tcl-2.1 {
140 execsql "CREATE TABLE t\u0123x(a int, b\u1235 float)"
141 execsql "PRAGMA table_info(t\u0123x)"
drh78100cc2003-08-23 22:40:53 +0000142 } "0 a int 0 {} 0 1 b\u1235 float 0 {} 0"
drh6d4abfb2001-10-22 02:58:08 +0000143 do_test tcl-2.2 {
144 execsql "INSERT INTO t\u0123x VALUES(1,2.3)"
145 db eval "SELECT * FROM t\u0123x" result break
146 set result(*)
147 } "a b\u1235"
148}
149
drh6d4abfb2001-10-22 02:58:08 +0000150
drh5d9d7572003-08-19 14:31:01 +0000151# Test the onecolumn method
152#
153do_test tcl-3.1 {
154 execsql {
155 INSERT INTO t1 SELECT a*2, b*2 FROM t1;
156 INSERT INTO t1 SELECT a*2+1, b*2+1 FROM t1;
157 INSERT INTO t1 SELECT a*2+3, b*2+3 FROM t1;
158 }
drh22fbcb82004-02-01 01:22:50 +0000159 set rc [catch {db onecolumn {SELECT * FROM t1 ORDER BY a}} msg]
160 lappend rc $msg
161} {0 10}
drh5d9d7572003-08-19 14:31:01 +0000162do_test tcl-3.2 {
163 db onecolumn {SELECT * FROM t1 WHERE a<0}
164} {}
165do_test tcl-3.3 {
166 set rc [catch {db onecolumn} errmsg]
167 lappend rc $errmsg
168} {1 {wrong # args: should be "db onecolumn SQL"}}
drh0f14e2e2004-06-29 12:39:08 +0000169do_test tcl-3.4 {
170 set rc [catch {db onecolumn {SELECT bogus}} errmsg]
171 lappend rc $errmsg
172} {1 {no such column: bogus}}
drh1807ce32004-09-07 13:20:35 +0000173do_test tcl-3.5 {
174 set b 50
175 set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg]
176 lappend rc $msg
177} {0 41}
178do_test tcl-3.6 {
179 set b 500
180 set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg]
181 lappend rc $msg
182} {0 {}}
183do_test tcl-3.7 {
184 set b 500
185 set rc [catch {db one {
186 INSERT INTO t1 VALUES(99,510);
187 SELECT * FROM t1 WHERE b>$b
188 }} msg]
189 lappend rc $msg
190} {0 99}
drh5d9d7572003-08-19 14:31:01 +0000191
drh0f14e2e2004-06-29 12:39:08 +0000192# Turn the busy handler on and off
193#
194do_test tcl-4.1 {
195 proc busy_callback {cnt} {
196 break
197 }
198 db busy busy_callback
199 db busy
200} {busy_callback}
201do_test tcl-4.2 {
202 db busy {}
203 db busy
204} {}
drh6d4abfb2001-10-22 02:58:08 +0000205
drh92febd92004-08-20 18:34:20 +0000206# Parsing of TCL variable names within SQL into bound parameters.
207#
208do_test tcl-5.1 {
209 execsql {CREATE TABLE t3(a,b,c)}
210 catch {unset x}
211 set x(1) 5
212 set x(2) 7
213 execsql {
214 INSERT INTO t3 VALUES($::x(1),$::x(2),$::x(3));
215 SELECT * FROM t3
216 }
217} {5 7 {}}
218do_test tcl-5.2 {
219 execsql {
220 SELECT typeof(a), typeof(b), typeof(c) FROM t3
221 }
222} {text text null}
223do_test tcl-5.3 {
224 catch {unset x}
225 set x [binary format h12 686900686f00]
226 execsql {
227 UPDATE t3 SET a=$::x;
228 }
229 db eval {
230 SELECT a FROM t3
231 } break
232 binary scan $a h12 adata
233 set adata
234} {686900686f00}
235do_test tcl-5.4 {
drhfd241b02004-09-13 13:46:01 +0000236 execsql {
drh92febd92004-08-20 18:34:20 +0000237 SELECT typeof(a), typeof(b), typeof(c) FROM t3
238 }
239} {blob text null}
240
drhfd241b02004-09-13 13:46:01 +0000241# Operation of "break" and "continue" within row scripts
242#
243do_test tcl-6.1 {
244 db eval {SELECT * FROM t1} {
245 break
246 }
247 lappend a $b
248} {10 20}
249do_test tcl-6.2 {
250 set cnt 0
251 db eval {SELECT * FROM t1} {
252 if {$a>40} continue
253 incr cnt
254 }
255 set cnt
256} {4}
257do_test tcl-6.3 {
258 set cnt 0
259 db eval {SELECT * FROM t1} {
260 if {$a<40} continue
261 incr cnt
262 }
263 set cnt
264} {5}
265do_test tcl-6.4 {
266 proc return_test {x} {
267 db eval {SELECT * FROM t1} {
268 if {$a==$x} {return $b}
269 }
270 }
271 return_test 10
272} 20
273do_test tcl-6.5 {
274 return_test 20
275} 40
276do_test tcl-6.6 {
277 return_test 99
278} 510
279do_test tcl-6.7 {
280 return_test 0
281} {}
282
drh960e8c62001-04-03 16:53:21 +0000283finish_test