#!/usr/bin/tcl # Simple Test file to test all mysqltcl commands and parameters # please create test database first # from test.sql file # >mysql -u root # >create database uni; # # >mysql -u root 0) break incr count } return } tcltest::test {receive-1.2} {with error} -body { set count 0 mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} { set res [list $nr $name] if ($count>0) { error "Test Error" } incr count } return } -returnCodes error -result "Test Error" # test error in 3.01 tcltest::test {receive-1.3} {with error 3.01} -body { set count 0 namespace eval :: { mysqlreceive $handle {select Name from Student order by Name} name { set res [list $name] incr count } } return } tcltest::test {query-1.0} {base case} -body { set query1 [mysqlquery $handle {select MatrNr,Name From Student Order By Name}] mysqlnext $query1 set query2 [mysqlquery $handle {select MatrNr,Name From Student Order By Name}] mysqlnext $query2 mysqlendquery $query1 mysqlnext $query2 mysqlresult $query2 cols mysqlresult $query2 rows mysqlresult $query2 current mysqlseek $query2 0 mysqlnext $query2 mysqlresult $query2 current mysqlcol $query2 -current {name type length table non_null prim_key decimals numeric} mysqlendquery $query2 return } tcltest::test {query-1.1} {endquery on handle} -body { mysqlsel $handle {select * from Student} mysqlendquery $handle mysqlresult $handle current } -returnCodes error -match glob -result "*no result*" tcltest::test {status-1.0} {read status array} -body { set ret "code=$mysqlstatus(code) command=$mysqlstatus(command) message=$mysqlstatus(message) nullvalue=$mysqlstatus(nullvalue)" return } tcltest::test {insert-1.0} {new insert id check} -body { mysqlexec $handle {INSERT INTO Student (Name,Semester) VALUES ('Artur Trzewik',11)} set newid [mysqlinsertid $handle] mysqlexec $handle "UPDATE Student SET Semester=12 WHERE MatrNr=$newid" mysqlinfo $handle info mysqlexec $handle "DELETE FROM Student WHERE MatrNr=$newid" } -result 1 tcltest::test {nullvalue-1.0} {null value handling} -body { # Test NULL Value setting mysqlexec $handle {INSERT INTO Student (Name) VALUES (Null)} set id [mysqlinsertid $handle] set mysqlstatus(nullvalue) NULL set res [lindex [mysqlsel $handle "select Name,Semester from Student where MatrNr=$id" -list] 0] lindex $res 1 } -result NULL tcltest::test {schema-1.0} {querry on schema} -body { # Metadata querries mysqlcol $handle Student name mysqlcol $handle Student {name type length table non_null prim_key decimals numeric} return } tcltest::test {info-1.0} {info} -body { mysqlinfo $handle databases mysqlinfo $handle dbname mysqlinfo $handle host mysqlinfo $handle tables mysqlinfo $handle dbname? mysqlinfo $handle host? return } tcltest::test {state-1.0} {state} -body { mysqlstate $handle mysqlstate $handle -numeric return } tcltest::test {errorhandling-1.0} {not a handle} -body { mysqlsel bad0 {select * from Student} } -returnCodes error -match glob -result "*not mysqltcl handle*" tcltest::test {errorhandling-1.1} {error in sql select, no table} -body { mysqlsel $handle {select * from Unknown} } -returnCodes error -match glob -result "*Table*" tcltest::test {errorhandling-1.2} {error in sql} -body { mysqlexec $handle {unknown command} } -returnCodes error -match glob -result "*SQL syntax*" tcltest::test {errorhandling-1.3} {read after end} -body { set rows [mysqlsel $handle {select * from Student}] for {set x 0} {$x<$rows} {incr x} { set res [mysqlnext $handle] set nr [lindex $res 0] set name [lindex $res 1] set sem [lindex $res 2] } mysqlnext $handle } -result {} tcltest::test {errorhandling-1.4} {false map binding} -body { #read after end by map mysqlsel $handle {select * from Student} mysqlmap $handle {nr name} { set row [format "nr %16s name:%s" $nr $name] } mysqlseek $handle 0 mysqlmap $handle {nr name err err2} { set row [format "nr %16s name:%s" $nr $name] } return } -returnCodes error -match glob -result "*too many variables*" tcltest::test {sel-1.2} {-list option} -body { mysqlsel $handle {select * from Student} -list return } tcltest::test {sel-1.3} {-flatlist option} -body { mysqlsel $handle {select * from Student} -flatlist return } tcltest::test {handle-1.0} {interanl finding handle} -body { set shandle [string trim " $handle "] mysqlinfo $shandle databases return } mysqlclose $handle tcltest::test {handle-1.1} {operation on closed handle} -body { mysqlinfo $handle tables return } -returnCodes error -match glob -result "*handle already closed*" tcltest::test {handle-1.2} {operation on closed handle} -body { set a " $handle " unset handle set a [string trim $a] mysqlinfo $a tables } -returnCodes error -match glob -result "*not mysqltcl handle*" tcltest::test {handle-1.2} {open 20 connection, close all} -body { for {set x 0} {$x<20} {incr x} { lappend handles [getConnection] } foreach h $handles { mysqlsel $h {select * from Student} } mysqlclose return } tcltest::test {handle-1.3} {10 queries, close all} -body { set handle [getConnection] for {set x 0} {$x<10} {incr x} { lappend queries [mysqlquery $handle {select * from Student}] } for {set x 0} {$x<10} {incr x} { mysqlquery $handle {select * from Student} } mysqlclose $handle mysqlnext [lindex $queries 0] } -returnCodes error -match glob -result "*handle already closed*" tcltest::test {handle-1.4} {10 queries, close all} -body { set handle [getConnection] mysqlquery $handle {select * from Student} mysqlclose return } tcltest::test {handle-1.5} {Testing false connecting} -body { mysqlconnect -user nouser -db nodb } -returnCodes error -match glob -result "*Unknown database*" set handle [getConnection] tcltest::test {escape-1.0} {escaping} -body { mysqlescape "art\"ur" mysqlescape $handle "art\"ur" return } tcltest::test {ping-1.0} {escaping} -body { mysqlping $handle return } tcltest::test {changeuser-1.0} {escaping} -body { mysqlchangeuser $handle root {} mysqlchangeuser $handle root {} uni return } # does not work for mysql4.1 tcltest::test {changeuser-1.1} {no such user} -body { mysqlchangeuser $handle root {} nodb } -returnCodes error -match glob -result "*Unknown database*" tcltest::test {interpreter-1.0} {mysqltcl in slave interpreter} -body { set handle [getConnection] set i1 [interp create] $i1 eval " package require mysqltcl set hdl [mysqlconnect -user $dbuser -db $dbank] " interp delete $i1 mysqlinfo $handle databases mysqlclose $handle return } tcltest::cleanupTests puts "End of test"