drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 9 | # |
| 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 | # |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 18 | # $Id: tclsqlite.test,v 1.37 2005/01/24 00:28:43 drh Exp $ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 19 | |
| 20 | set testdir [file dirname $argv0] |
| 21 | source $testdir/tester.tcl |
| 22 | |
| 23 | # Check the error messages generated by tclsqlite |
| 24 | # |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 25 | if {[sqlite3 -has-codec]} { |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 26 | set r "sqlite_orig HANDLE FILENAME ?-key CODEC-KEY?" |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 27 | } else { |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 28 | set r "sqlite3 HANDLE FILENAME ?MODE?" |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 29 | } |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 30 | do_test tcl-1.1 { |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 31 | set v [catch {sqlite3 bogus} msg] |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 32 | lappend v $msg |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 33 | } [list 1 "wrong # args: should be \"$r\""] |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 34 | do_test tcl-1.2 { |
| 35 | set v [catch {db bogus} msg] |
| 36 | lappend v $msg |
drh | fb7e765 | 2005-01-24 00:28:42 +0000 | [diff] [blame] | 37 | } {1 {bad option "bogus": must be authorizer, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, copy, errorcode, eval, function, last_insert_rowid, onecolumn, progress, rekey, timeout, total_changes, trace, or version}} |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 38 | do_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}} |
| 48 | do_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}} |
| 56 | do_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 {}} |
| 64 | do_test tcl-1.6 { |
| 65 | set v [catch { |
| 66 | db eval {SELECT * FROM t1} data { |
| 67 | expr x* |
| 68 | } |
| 69 | } msg] |
drh | a297b5c | 2002-01-15 18:39:43 +0000 | [diff] [blame] | 70 | regsub {:.*$} $msg {} msg |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 71 | lappend v $msg |
| 72 | } {1 {syntax error in expression "x*"}} |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 73 | do_test tcl-1.7 { |
| 74 | set v [catch {db} msg] |
| 75 | lappend v $msg |
| 76 | } {1 {wrong # args: should be "db SUBCOMMAND ..."}} |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 77 | if {[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 | } |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 83 | do_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"}} |
| 87 | do_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"}} |
| 91 | do_test tcl-1.11 { |
| 92 | set v [catch {db changes xyz} msg] |
| 93 | lappend v $msg |
| 94 | } {1 {wrong # args: should be "db changes "}} |
| 95 | do_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?"}} |
| 99 | do_test tcl-1.13 { |
| 100 | set v [catch {db complete} msg] |
| 101 | lappend v $msg |
| 102 | } {1 {wrong # args: should be "db complete SQL"}} |
| 103 | do_test tcl-1.14 { |
| 104 | set v [catch {db eval} msg] |
| 105 | lappend v $msg |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 106 | } {1 {wrong # args: should be "db eval SQL ?ARRAY-NAME? ?SCRIPT?"}} |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 107 | do_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"}} |
| 111 | do_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 "}} |
| 115 | do_test tcl-1.15 { |
| 116 | set v [catch {db rekey} msg] |
| 117 | lappend v $msg |
| 118 | } {1 {wrong # args: should be "db rekey KEY"}} |
| 119 | do_test tcl-1.16 { |
| 120 | set v [catch {db timeout} msg] |
| 121 | lappend v $msg |
| 122 | } {1 {wrong # args: should be "db timeout MILLISECONDS"}} |
| 123 | do_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"}} |
| 127 | do_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"}} |
| 131 | do_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 "}} |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 135 | do_test tcl-1.20 { |
| 136 | set v [catch {db copy} msg] |
| 137 | lappend v $msg |
| 138 | } {1 {wrong # args: should be "db copy CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"}} |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 139 | |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 140 | |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 141 | if {[sqlite3 -tcl-uses-utf]} { |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 142 | catch {unset ::result} |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 143 | do_test tcl-2.1 { |
| 144 | execsql "CREATE TABLE t\u0123x(a int, b\u1235 float)" |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 145 | } {} |
| 146 | ifcapable schema_pragmas { |
| 147 | do_test tcl-2.2 { |
| 148 | execsql "PRAGMA table_info(t\u0123x)" |
| 149 | } "0 a int 0 {} 0 1 b\u1235 float 0 {} 0" |
| 150 | } |
| 151 | do_test tcl-2.3 { |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 152 | execsql "INSERT INTO t\u0123x VALUES(1,2.3)" |
| 153 | db eval "SELECT * FROM t\u0123x" result break |
| 154 | set result(*) |
| 155 | } "a b\u1235" |
| 156 | } |
| 157 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 158 | |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 159 | # Test the onecolumn method |
| 160 | # |
| 161 | do_test tcl-3.1 { |
| 162 | execsql { |
| 163 | INSERT INTO t1 SELECT a*2, b*2 FROM t1; |
| 164 | INSERT INTO t1 SELECT a*2+1, b*2+1 FROM t1; |
| 165 | INSERT INTO t1 SELECT a*2+3, b*2+3 FROM t1; |
| 166 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 167 | set rc [catch {db onecolumn {SELECT * FROM t1 ORDER BY a}} msg] |
| 168 | lappend rc $msg |
| 169 | } {0 10} |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 170 | do_test tcl-3.2 { |
| 171 | db onecolumn {SELECT * FROM t1 WHERE a<0} |
| 172 | } {} |
| 173 | do_test tcl-3.3 { |
| 174 | set rc [catch {db onecolumn} errmsg] |
| 175 | lappend rc $errmsg |
| 176 | } {1 {wrong # args: should be "db onecolumn SQL"}} |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 177 | do_test tcl-3.4 { |
| 178 | set rc [catch {db onecolumn {SELECT bogus}} errmsg] |
| 179 | lappend rc $errmsg |
| 180 | } {1 {no such column: bogus}} |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 181 | ifcapable {tclvar} { |
| 182 | do_test tcl-3.5 { |
| 183 | set b 50 |
| 184 | set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg] |
| 185 | lappend rc $msg |
| 186 | } {0 41} |
| 187 | do_test tcl-3.6 { |
| 188 | set b 500 |
| 189 | set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg] |
| 190 | lappend rc $msg |
| 191 | } {0 {}} |
| 192 | do_test tcl-3.7 { |
| 193 | set b 500 |
| 194 | set rc [catch {db one { |
| 195 | INSERT INTO t1 VALUES(99,510); |
| 196 | SELECT * FROM t1 WHERE b>$b |
| 197 | }} msg] |
| 198 | lappend rc $msg |
| 199 | } {0 99} |
| 200 | } |
| 201 | ifcapable {!tclvar} { |
| 202 | execsql {INSERT INTO t1 VALUES(99,510)} |
| 203 | } |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 204 | |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 205 | # Turn the busy handler on and off |
| 206 | # |
| 207 | do_test tcl-4.1 { |
| 208 | proc busy_callback {cnt} { |
| 209 | break |
| 210 | } |
| 211 | db busy busy_callback |
| 212 | db busy |
| 213 | } {busy_callback} |
| 214 | do_test tcl-4.2 { |
| 215 | db busy {} |
| 216 | db busy |
| 217 | } {} |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 218 | |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 219 | ifcapable {tclvar} { |
| 220 | # Parsing of TCL variable names within SQL into bound parameters. |
| 221 | # |
| 222 | do_test tcl-5.1 { |
| 223 | execsql {CREATE TABLE t3(a,b,c)} |
| 224 | catch {unset x} |
| 225 | set x(1) 5 |
| 226 | set x(2) 7 |
| 227 | execsql { |
| 228 | INSERT INTO t3 VALUES($::x(1),$::x(2),$::x(3)); |
| 229 | SELECT * FROM t3 |
| 230 | } |
| 231 | } {5 7 {}} |
| 232 | do_test tcl-5.2 { |
| 233 | execsql { |
| 234 | SELECT typeof(a), typeof(b), typeof(c) FROM t3 |
| 235 | } |
| 236 | } {text text null} |
| 237 | do_test tcl-5.3 { |
| 238 | catch {unset x} |
| 239 | set x [binary format h12 686900686f00] |
| 240 | execsql { |
| 241 | UPDATE t3 SET a=$::x; |
| 242 | } |
| 243 | db eval { |
| 244 | SELECT a FROM t3 |
| 245 | } break |
| 246 | binary scan $a h12 adata |
| 247 | set adata |
| 248 | } {686900686f00} |
| 249 | do_test tcl-5.4 { |
| 250 | execsql { |
| 251 | SELECT typeof(a), typeof(b), typeof(c) FROM t3 |
| 252 | } |
| 253 | } {blob text null} |
| 254 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 255 | |
drh | fd241b0 | 2004-09-13 13:46:01 +0000 | [diff] [blame] | 256 | # Operation of "break" and "continue" within row scripts |
| 257 | # |
| 258 | do_test tcl-6.1 { |
| 259 | db eval {SELECT * FROM t1} { |
| 260 | break |
| 261 | } |
| 262 | lappend a $b |
| 263 | } {10 20} |
| 264 | do_test tcl-6.2 { |
| 265 | set cnt 0 |
| 266 | db eval {SELECT * FROM t1} { |
| 267 | if {$a>40} continue |
| 268 | incr cnt |
| 269 | } |
| 270 | set cnt |
| 271 | } {4} |
| 272 | do_test tcl-6.3 { |
| 273 | set cnt 0 |
| 274 | db eval {SELECT * FROM t1} { |
| 275 | if {$a<40} continue |
| 276 | incr cnt |
| 277 | } |
| 278 | set cnt |
| 279 | } {5} |
| 280 | do_test tcl-6.4 { |
| 281 | proc return_test {x} { |
| 282 | db eval {SELECT * FROM t1} { |
| 283 | if {$a==$x} {return $b} |
| 284 | } |
| 285 | } |
| 286 | return_test 10 |
| 287 | } 20 |
| 288 | do_test tcl-6.5 { |
| 289 | return_test 20 |
| 290 | } 40 |
| 291 | do_test tcl-6.6 { |
| 292 | return_test 99 |
| 293 | } 510 |
| 294 | do_test tcl-6.7 { |
| 295 | return_test 0 |
| 296 | } {} |
| 297 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 298 | do_test tcl-7.1 { |
| 299 | db version |
| 300 | expr 0 |
| 301 | } {0} |
| 302 | |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 303 | finish_test |