sl@0: # 2001 September 15 sl@0: # sl@0: # Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved. sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for TCL interface to the sl@0: # SQLite library. sl@0: # sl@0: # Actually, all tests are based on the TCL interface, so the main sl@0: # interface is pretty well tested. This file contains some addition sl@0: # tests for fringe issues that the main test suite does not cover. sl@0: # sl@0: # $Id: tclsqlite.test,v 1.69 2008/09/09 12:31:34 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # Check the error messages generated by tclsqlite sl@0: # sl@0: if {[sqlite3 -has-codec]} { sl@0: set r "sqlite_orig HANDLE FILENAME ?-key CODEC-KEY?" sl@0: } else { sl@0: set r "sqlite3 HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?" sl@0: } sl@0: do_test tcl-1.1 { sl@0: set v [catch {sqlite3 bogus} msg] sl@0: regsub {really_sqlite3} $msg {sqlite3} msg sl@0: lappend v $msg sl@0: } [list 1 "wrong # args: should be \"$r\""] sl@0: do_test tcl-1.2 { sl@0: set v [catch {db bogus} msg] sl@0: lappend v $msg sl@0: } {1 {bad option "bogus": must be authorizer, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, copy, enable_load_extension, errorcode, eval, exists, function, incrblob, interrupt, last_insert_rowid, nullvalue, onecolumn, profile, progress, rekey, rollback_hook, timeout, total_changes, trace, transaction, update_hook, or version}} sl@0: do_test tcl-1.2.1 { sl@0: set v [catch {db cache bogus} msg] sl@0: lappend v $msg sl@0: } {1 {bad option "bogus": must be flush or size}} sl@0: do_test tcl-1.2.2 { sl@0: set v [catch {db cache} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db cache option ?arg?"}} sl@0: do_test tcl-1.3 { sl@0: execsql {CREATE TABLE t1(a int, b int)} sl@0: execsql {INSERT INTO t1 VALUES(10,20)} sl@0: set v [catch { sl@0: db eval {SELECT * FROM t1} data { sl@0: error "The error message" sl@0: } sl@0: } msg] sl@0: lappend v $msg sl@0: } {1 {The error message}} sl@0: do_test tcl-1.4 { sl@0: set v [catch { sl@0: db eval {SELECT * FROM t2} data { sl@0: error "The error message" sl@0: } sl@0: } msg] sl@0: lappend v $msg sl@0: } {1 {no such table: t2}} sl@0: do_test tcl-1.5 { sl@0: set v [catch { sl@0: db eval {SELECT * FROM t1} data { sl@0: break sl@0: } sl@0: } msg] sl@0: lappend v $msg sl@0: } {0 {}} sl@0: catch {expr x*} msg sl@0: do_test tcl-1.6 { sl@0: set v [catch { sl@0: db eval {SELECT * FROM t1} data { sl@0: expr x* sl@0: } sl@0: } msg] sl@0: lappend v $msg sl@0: } [list 1 $msg] sl@0: do_test tcl-1.7 { sl@0: set v [catch {db} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db SUBCOMMAND ..."}} sl@0: if {[catch {db auth {}}]==0} { sl@0: do_test tcl-1.8 { sl@0: set v [catch {db authorizer 1 2 3} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db authorizer ?CALLBACK?"}} sl@0: } sl@0: do_test tcl-1.9 { sl@0: set v [catch {db busy 1 2 3} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db busy CALLBACK"}} sl@0: do_test tcl-1.10 { sl@0: set v [catch {db progress 1} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db progress N CALLBACK"}} sl@0: do_test tcl-1.11 { sl@0: set v [catch {db changes xyz} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db changes "}} sl@0: do_test tcl-1.12 { sl@0: set v [catch {db commit_hook a b c} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db commit_hook ?CALLBACK?"}} sl@0: ifcapable {complete} { sl@0: do_test tcl-1.13 { sl@0: set v [catch {db complete} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db complete SQL"}} sl@0: } sl@0: do_test tcl-1.14 { sl@0: set v [catch {db eval} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db eval SQL ?ARRAY-NAME? ?SCRIPT?"}} sl@0: do_test tcl-1.15 { sl@0: set v [catch {db function} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db function NAME [-argcount N] SCRIPT"}} sl@0: do_test tcl-1.16 { sl@0: set v [catch {db last_insert_rowid xyz} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db last_insert_rowid "}} sl@0: do_test tcl-1.17 { sl@0: set v [catch {db rekey} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db rekey KEY"}} sl@0: do_test tcl-1.18 { sl@0: set v [catch {db timeout} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db timeout MILLISECONDS"}} sl@0: do_test tcl-1.19 { sl@0: set v [catch {db collate} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db collate NAME SCRIPT"}} sl@0: do_test tcl-1.20 { sl@0: set v [catch {db collation_needed} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db collation_needed SCRIPT"}} sl@0: do_test tcl-1.21 { sl@0: set v [catch {db total_changes xyz} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db total_changes "}} sl@0: do_test tcl-1.20 { sl@0: set v [catch {db copy} msg] sl@0: lappend v $msg sl@0: } {1 {wrong # args: should be "db copy CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"}} sl@0: do_test tcl-1.21 { sl@0: set v [catch {sqlite3 db2 test.db -vfs nosuchvfs} msg] sl@0: lappend v $msg sl@0: } {1 {no such vfs: nosuchvfs}} sl@0: sl@0: catch {unset ::result} sl@0: do_test tcl-2.1 { sl@0: execsql "CREATE TABLE t\u0123x(a int, b\u1235 float)" sl@0: } {} sl@0: ifcapable schema_pragmas { sl@0: do_test tcl-2.2 { sl@0: execsql "PRAGMA table_info(t\u0123x)" sl@0: } "0 a int 0 {} 0 1 b\u1235 float 0 {} 0" sl@0: } sl@0: do_test tcl-2.3 { sl@0: execsql "INSERT INTO t\u0123x VALUES(1,2.3)" sl@0: db eval "SELECT * FROM t\u0123x" result break sl@0: set result(*) sl@0: } "a b\u1235" sl@0: sl@0: sl@0: # Test the onecolumn method sl@0: # sl@0: do_test tcl-3.1 { sl@0: execsql { sl@0: INSERT INTO t1 SELECT a*2, b*2 FROM t1; sl@0: INSERT INTO t1 SELECT a*2+1, b*2+1 FROM t1; sl@0: INSERT INTO t1 SELECT a*2+3, b*2+3 FROM t1; sl@0: } sl@0: set rc [catch {db onecolumn {SELECT * FROM t1 ORDER BY a}} msg] sl@0: lappend rc $msg sl@0: } {0 10} sl@0: do_test tcl-3.2 { sl@0: db onecolumn {SELECT * FROM t1 WHERE a<0} sl@0: } {} sl@0: do_test tcl-3.3 { sl@0: set rc [catch {db onecolumn} errmsg] sl@0: lappend rc $errmsg sl@0: } {1 {wrong # args: should be "db onecolumn SQL"}} sl@0: do_test tcl-3.4 { sl@0: set rc [catch {db onecolumn {SELECT bogus}} errmsg] sl@0: lappend rc $errmsg sl@0: } {1 {no such column: bogus}} sl@0: ifcapable {tclvar} { sl@0: do_test tcl-3.5 { sl@0: set b 50 sl@0: set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg] sl@0: lappend rc $msg sl@0: } {0 41} sl@0: do_test tcl-3.6 { sl@0: set b 500 sl@0: set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg] sl@0: lappend rc $msg sl@0: } {0 {}} sl@0: do_test tcl-3.7 { sl@0: set b 500 sl@0: set rc [catch {db one { sl@0: INSERT INTO t1 VALUES(99,510); sl@0: SELECT * FROM t1 WHERE b>$b sl@0: }} msg] sl@0: lappend rc $msg sl@0: } {0 99} sl@0: } sl@0: ifcapable {!tclvar} { sl@0: execsql {INSERT INTO t1 VALUES(99,510)} sl@0: } sl@0: sl@0: # Turn the busy handler on and off sl@0: # sl@0: do_test tcl-4.1 { sl@0: proc busy_callback {cnt} { sl@0: break sl@0: } sl@0: db busy busy_callback sl@0: db busy sl@0: } {busy_callback} sl@0: do_test tcl-4.2 { sl@0: db busy {} sl@0: db busy sl@0: } {} sl@0: sl@0: ifcapable {tclvar} { sl@0: # Parsing of TCL variable names within SQL into bound parameters. sl@0: # sl@0: do_test tcl-5.1 { sl@0: execsql {CREATE TABLE t3(a,b,c)} sl@0: catch {unset x} sl@0: set x(1) A sl@0: set x(2) B sl@0: execsql { sl@0: INSERT INTO t3 VALUES($::x(1),$::x(2),$::x(3)); sl@0: SELECT * FROM t3 sl@0: } sl@0: } {A B {}} sl@0: do_test tcl-5.2 { sl@0: execsql { sl@0: SELECT typeof(a), typeof(b), typeof(c) FROM t3 sl@0: } sl@0: } {text text null} sl@0: do_test tcl-5.3 { sl@0: catch {unset x} sl@0: set x [binary format h12 686900686f00] sl@0: execsql { sl@0: UPDATE t3 SET a=$::x; sl@0: } sl@0: db eval { sl@0: SELECT a FROM t3 sl@0: } break sl@0: binary scan $a h12 adata sl@0: set adata sl@0: } {686900686f00} sl@0: do_test tcl-5.4 { sl@0: execsql { sl@0: SELECT typeof(a), typeof(b), typeof(c) FROM t3 sl@0: } sl@0: } {blob text null} sl@0: } sl@0: sl@0: # Operation of "break" and "continue" within row scripts sl@0: # sl@0: do_test tcl-6.1 { sl@0: db eval {SELECT * FROM t1} { sl@0: break sl@0: } sl@0: lappend a $b sl@0: } {10 20} sl@0: do_test tcl-6.2 { sl@0: set cnt 0 sl@0: db eval {SELECT * FROM t1} { sl@0: if {$a>40} continue sl@0: incr cnt sl@0: } sl@0: set cnt sl@0: } {4} sl@0: do_test tcl-6.3 { sl@0: set cnt 0 sl@0: db eval {SELECT * FROM t1} { sl@0: if {$a<40} continue sl@0: incr cnt sl@0: } sl@0: set cnt sl@0: } {5} sl@0: do_test tcl-6.4 { sl@0: proc return_test {x} { sl@0: db eval {SELECT * FROM t1} { sl@0: if {$a==$x} {return $b} sl@0: } sl@0: } sl@0: return_test 10 sl@0: } 20 sl@0: do_test tcl-6.5 { sl@0: return_test 20 sl@0: } 40 sl@0: do_test tcl-6.6 { sl@0: return_test 99 sl@0: } 510 sl@0: do_test tcl-6.7 { sl@0: return_test 0 sl@0: } {} sl@0: sl@0: do_test tcl-7.1 { sl@0: db version sl@0: expr 0 sl@0: } {0} sl@0: sl@0: # modify and reset the NULL representation sl@0: # sl@0: do_test tcl-8.1 { sl@0: db nullvalue NaN sl@0: execsql {INSERT INTO t1 VALUES(30,NULL)} sl@0: db eval {SELECT * FROM t1 WHERE b IS NULL} sl@0: } {30 NaN} sl@0: do_test tcl-8.2 { sl@0: db nullvalue NULL sl@0: db nullvalue sl@0: } {NULL} sl@0: do_test tcl-8.3 { sl@0: db nullvalue {} sl@0: db eval {SELECT * FROM t1 WHERE b IS NULL} sl@0: } {30 {}} sl@0: sl@0: # Test the return type of user-defined functions sl@0: # sl@0: do_test tcl-9.1 { sl@0: db function ret_str {return "hi"} sl@0: execsql {SELECT typeof(ret_str())} sl@0: } {text} sl@0: do_test tcl-9.2 { sl@0: db function ret_dbl {return [expr {rand()*0.5}]} sl@0: execsql {SELECT typeof(ret_dbl())} sl@0: } {real} sl@0: do_test tcl-9.3 { sl@0: db function ret_int {return [expr {int(rand()*200)}]} sl@0: execsql {SELECT typeof(ret_int())} sl@0: } {integer} sl@0: sl@0: # Recursive calls to the same user-defined function sl@0: # sl@0: ifcapable tclvar { sl@0: do_test tcl-9.10 { sl@0: proc userfunc_r1 {n} { sl@0: if {$n<=0} {return 0} sl@0: set nm1 [expr {$n-1}] sl@0: return [expr {[db eval {SELECT r1($nm1)}]+$n}] sl@0: } sl@0: db function r1 userfunc_r1 sl@0: execsql {SELECT r1(10)} sl@0: } {55} sl@0: if {$::tcl_platform(platform)!="symbian"} { sl@0: do_test tcl-9.11 { sl@0: execsql {SELECT r1(100)} sl@0: } {5050} sl@0: } sl@0: } sl@0: sl@0: # Tests for the new transaction method sl@0: # sl@0: do_test tcl-10.1 { sl@0: db transaction {} sl@0: } {} sl@0: do_test tcl-10.2 { sl@0: db transaction deferred {} sl@0: } {} sl@0: do_test tcl-10.3 { sl@0: db transaction immediate {} sl@0: } {} sl@0: do_test tcl-10.4 { sl@0: db transaction exclusive {} sl@0: } {} sl@0: do_test tcl-10.5 { sl@0: set rc [catch {db transaction xyzzy {}} msg] sl@0: lappend rc $msg sl@0: } {1 {bad transaction type "xyzzy": must be deferred, exclusive, or immediate}} sl@0: do_test tcl-10.6 { sl@0: set rc [catch {db transaction {error test-error}} msg] sl@0: lappend rc $msg sl@0: } {1 test-error} sl@0: do_test tcl-10.7 { sl@0: db transaction { sl@0: db eval {CREATE TABLE t4(x)} sl@0: db transaction { sl@0: db eval {INSERT INTO t4 VALUES(1)} sl@0: } sl@0: } sl@0: db eval {SELECT * FROM t4} sl@0: } 1 sl@0: do_test tcl-10.8 { sl@0: catch { sl@0: db transaction { sl@0: db eval {INSERT INTO t4 VALUES(2)} sl@0: db eval {INSERT INTO t4 VALUES(3)} sl@0: db eval {INSERT INTO t4 VALUES(4)} sl@0: error test-error sl@0: } sl@0: } sl@0: db eval {SELECT * FROM t4} sl@0: } 1 sl@0: do_test tcl-10.9 { sl@0: db transaction { sl@0: db eval {INSERT INTO t4 VALUES(2)} sl@0: catch { sl@0: db transaction { sl@0: db eval {INSERT INTO t4 VALUES(3)} sl@0: db eval {INSERT INTO t4 VALUES(4)} sl@0: error test-error sl@0: } sl@0: } sl@0: } sl@0: db eval {SELECT * FROM t4} sl@0: } {1 2 3 4} sl@0: do_test tcl-10.10 { sl@0: for {set i 0} {$i<1} {incr i} { sl@0: db transaction { sl@0: db eval {INSERT INTO t4 VALUES(5)} sl@0: continue sl@0: } sl@0: } sl@0: db eval {SELECT * FROM t4} sl@0: } {1 2 3 4 5} sl@0: do_test tcl-10.11 { sl@0: for {set i 0} {$i<10} {incr i} { sl@0: db transaction { sl@0: db eval {INSERT INTO t4 VALUES(6)} sl@0: break sl@0: } sl@0: } sl@0: db eval {SELECT * FROM t4} sl@0: } {1 2 3 4 5 6} sl@0: do_test tcl-10.12 { sl@0: set rc [catch { sl@0: for {set i 0} {$i<10} {incr i} { sl@0: db transaction { sl@0: db eval {INSERT INTO t4 VALUES(7)} sl@0: return sl@0: } sl@0: } sl@0: }] sl@0: } {2} sl@0: do_test tcl-10.13 { sl@0: db eval {SELECT * FROM t4} sl@0: } {1 2 3 4 5 6 7} sl@0: sl@0: do_test tcl-11.1 { sl@0: db exists {SELECT x,x*2,x+x FROM t4 WHERE x==4} sl@0: } {1} sl@0: do_test tcl-11.2 { sl@0: db exists {SELECT 0 FROM t4 WHERE x==4} sl@0: } {1} sl@0: do_test tcl-11.3 { sl@0: db exists {SELECT 1 FROM t4 WHERE x==8} sl@0: } {0} sl@0: sl@0: do_test tcl-12.1 { sl@0: unset -nocomplain a b c version sl@0: set version [db version] sl@0: scan $version "%d.%d.%d" a b c sl@0: expr $a*1000000 + $b*1000 + $c sl@0: } [sqlite3_libversion_number] sl@0: sl@0: sl@0: # Check to see that when bindings of the form @aaa are used instead sl@0: # of $aaa, that objects are treated as bytearray and are inserted sl@0: # as BLOBs. sl@0: # sl@0: ifcapable tclvar { sl@0: do_test tcl-13.1 { sl@0: db eval {CREATE TABLE t5(x BLOB)} sl@0: set x abc123 sl@0: db eval {INSERT INTO t5 VALUES($x)} sl@0: db eval {SELECT typeof(x) FROM t5} sl@0: } {text} sl@0: do_test tcl-13.2 { sl@0: binary scan $x H notUsed sl@0: db eval { sl@0: DELETE FROM t5; sl@0: INSERT INTO t5 VALUES($x); sl@0: SELECT typeof(x) FROM t5; sl@0: } sl@0: } {text} sl@0: do_test tcl-13.3 { sl@0: db eval { sl@0: DELETE FROM t5; sl@0: INSERT INTO t5 VALUES(@x); sl@0: SELECT typeof(x) FROM t5; sl@0: } sl@0: } {blob} sl@0: do_test tcl-13.4 { sl@0: set y 1234 sl@0: db eval { sl@0: DELETE FROM t5; sl@0: INSERT INTO t5 VALUES(@y); sl@0: SELECT hex(x), typeof(x) FROM t5 sl@0: } sl@0: } {31323334 blob} sl@0: } sl@0: sl@0: sl@0: finish_test