2 # Simple Test file to test all mysqltcl commands and parameters
3 # please create test database first
6 # >create database uni;
8 # >mysql -u root <test.sql
9 # please adapt the parameters for mysqlconnect some lines above
11 if {[file exists libload.tcl]} {
14 source [file join [file dirname [info script]] libload.tcl]
18 # global connect variables
21 set dbank mysqltcltest
23 package require tcltest
24 variable SETUP {#common setup code}
25 variable CLEANUP {#common cleanup code}
26 tcltest::configure -verbose bet
28 proc getConnection {{addOptions {}} {withDB 1}} {
29 global dbuser dbpassword dbank
31 append addOptions " -db $dbank"
33 if {$dbpassword ne ""} {
34 append addOptions " -password $dbpassword"
36 return [eval mysqlconnect -user $dbuser $addOptions]
38 proc prepareTestDB {} {
40 set handle [getConnection {} 0]
41 if {[lsearch [mysqlinfo $handle databases] $dbank]<0} {
42 puts "Testdatabase $dbank does not exist. Create it"
43 mysqlexec $handle "CREATE DATABASE $dbank"
45 mysqluse $handle $dbank
47 catch {mysqlexec $handle {drop table Student}}
50 CREATE TABLE Student (
51 MatrNr int NOT NULL auto_increment,
57 mysqlexec $handle "INSERT INTO Student VALUES (1,'Sojka',4)"
58 mysqlexec $handle "INSERT INTO Student VALUES (2,'Preisner',2)"
59 mysqlexec $handle "INSERT INTO Student VALUES (3,'Killar',2)"
60 mysqlexec $handle "INSERT INTO Student VALUES (4,'Penderecki',10)"
61 mysqlexec $handle "INSERT INTO Student VALUES (5,'Turnau',2)"
62 mysqlexec $handle "INSERT INTO Student VALUES (6,'Grechuta',3)"
63 mysqlexec $handle "INSERT INTO Student VALUES (7,'Gorniak',1)"
64 mysqlexec $handle "INSERT INTO Student VALUES (8,'Niemen',3)"
65 mysqlexec $handle "INSERT INTO Student VALUES (9,'Bem',5)"
71 tcltest::test {connect-1.1} {no encoding} -body {
72 getConnection {-encoding nonexisting}
73 } -returnCodes error -match glob -result "*unknown encoding*"
75 tcltest::test {connect-1.2} {binary encoding} -body {
76 set handle [getConnection {-encoding binary}]
81 tcltest::test {connect-1.3} {-ssl option} -body {
82 set handle [getConnection {-ssl 1}]
87 tcltest::test {connect-1.4} {-noschema option 1} -body {
88 set handle [getConnection {-noschema 1}]
93 tcltest::test {connect-1.5} {-noschema option 0} -body {
94 set handle [getConnection {-noschema 0}]
99 tcltest::test {connect-1.6} {-comperss option} -body {
100 set handle [getConnection {-compress 1}]
105 tcltest::test {connect-1.7} {-odbc option} -body {
106 set handle [getConnection {-odbc 1}]
111 tcltest::test {connect-1.8} {Test of encondig option} -body {
112 set handle [getConnection {-encoding iso8859-1}]
113 set name {Artur Trzewik}
114 mysqlexec $handle "INSERT INTO Student (Name,Semester) VALUES ('$name',11)"
115 set newid [mysqlinsertid $handle]
116 set rname [lindex [lindex [mysqlsel $handle "select Name from Student where MatrNr = $newid" -list] 0] 0]
117 mysqlexec $handle "DELETE FROM Student WHERE MatrNr = $newid"
120 } -result {Artur Trzewik}
122 tcltest::test {baseinfo-1.0} {base info} -body {
123 mysqlbaseinfo connectparameters
124 mysqlbaseinfo clientversion
128 tcltest::test {select-1.0} {use implicit database notation} -body {
129 set handle [getConnection {} 0]
130 mysqlsel $handle "select * from $dbank.Student"
131 mysqlcol $handle -current {name type length table non_null prim_key decimals numeric}
133 mysqlinfo $handle databases
138 set handle [getConnection]
140 tcltest::test {use-1.0} {false use} -body {
141 mysqluse $handle notdb2
142 } -returnCodes error -match glob -result "mysqluse/db server: Unknown database 'notdb2'"
145 tcltest::test {select-1.1} {Test sel and next functions} -body {
147 set allrows [mysqlsel $handle {select * from Student}]
148 mysqlcol $handle -current {name type length table non_null prim_key decimals numeric}
150 mysqlresult $handle cols
151 set rows [mysqlresult $handle rows]
152 set rowsComp [expr {$allrows==$rows}]
153 set fstcurrent [mysqlresult $handle current]
154 set firstRow [mysqlnext $handle]
155 set scdcurrent [mysqlresult $handle current]
159 set scdcurrent2 [mysqlresult $handle current]
160 set isFirst [expr {[mysqlnext $handle] eq $firstRow}]
162 return [list $fstcurrent $scdcurrent $rowsComp $scdcurrent2 $isFirst]
163 } -result {0 1 1 0 1}
165 tcltest::test {map-1.0} {map function} -body {
167 select MatrNr,Name from Student order by Name
169 mysqlmap $handle {nr name} {
170 if {$nr == {}} continue
171 set tempr [list $nr $name]
172 set row [format "nr %16s name:%s" $nr $name]
177 tcltest::test {map-1.1} {double map with seek} -body {
178 #read after end by map
179 mysqlsel $handle {select * from Student}
180 mysqlmap $handle {nr name} {
181 set row [format "nr %16s name:%s" $nr $name]
184 mysqlmap $handle {nr name} {
185 set row [format "nr %16s name:%s" $nr $name]
190 # Test error in version 3.01
191 tcltest::test {map-1.2} {map function error 3.01} -body {
193 select Name from Student order by Name
196 mysqlmap $handle name {
204 tcltest::test {receive-1.0} {base case} -body {
205 mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} {
206 set res [list $nr $name]
211 tcltest::test {receive-1.1} {with break} -body {
213 mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} {
214 set res [list $nr $name]
221 tcltest::test {receive-1.2} {with error} -body {
223 mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} {
224 set res [list $nr $name]
231 } -returnCodes error -result "Test Error"
234 tcltest::test {receive-1.3} {with error 3.01} -body {
237 mysqlreceive $handle {select Name from Student order by Name} name {
246 tcltest::test {query-1.0} {base case} -body {
247 set query1 [mysqlquery $handle {select MatrNr,Name From Student Order By Name}]
249 set query2 [mysqlquery $handle {select MatrNr,Name From Student Order By Name}]
251 mysqlendquery $query1
253 mysqlresult $query2 cols
254 mysqlresult $query2 rows
255 mysqlresult $query2 current
258 mysqlresult $query2 current
259 mysqlcol $query2 -current {name type length table non_null prim_key decimals numeric}
260 mysqlendquery $query2
264 tcltest::test {query-1.1} {endquery on handle} -body {
265 mysqlsel $handle {select * from Student}
266 mysqlendquery $handle
267 mysqlresult $handle current
268 } -returnCodes error -match glob -result "*no result*"
270 tcltest::test {status-1.0} {read status array} -body {
271 set ret "code=$mysqlstatus(code) command=$mysqlstatus(command) message=$mysqlstatus(message) nullvalue=$mysqlstatus(nullvalue)"
275 tcltest::test {insert-1.0} {new insert id check} -body {
276 mysqlexec $handle {INSERT INTO Student (Name,Semester) VALUES ('Artur Trzewik',11)}
277 set newid [mysqlinsertid $handle]
278 mysqlexec $handle "UPDATE Student SET Semester=12 WHERE MatrNr=$newid"
279 mysqlinfo $handle info
280 mysqlexec $handle "DELETE FROM Student WHERE MatrNr=$newid"
283 tcltest::test {nullvalue-1.0} {null value handling} -body {
284 # Test NULL Value setting
285 mysqlexec $handle {INSERT INTO Student (Name) VALUES (Null)}
286 set id [mysqlinsertid $handle]
287 set mysqlstatus(nullvalue) NULL
288 set res [lindex [mysqlsel $handle "select Name,Semester from Student where MatrNr=$id" -list] 0]
292 tcltest::test {schema-1.0} {querry on schema} -body {
294 mysqlcol $handle Student name
295 mysqlcol $handle Student {name type length table non_null prim_key decimals numeric}
299 tcltest::test {info-1.0} {info} -body {
300 mysqlinfo $handle databases
301 mysqlinfo $handle dbname
302 mysqlinfo $handle host
303 mysqlinfo $handle tables
304 mysqlinfo $handle dbname?
305 mysqlinfo $handle host?
309 tcltest::test {state-1.0} {state} -body {
311 mysqlstate $handle -numeric
315 tcltest::test {errorhandling-1.0} {not a handle} -body {
316 mysqlsel bad0 {select * from Student}
317 } -returnCodes error -match glob -result "*not mysqltcl handle*"
319 tcltest::test {errorhandling-1.1} {error in sql select, no table} -body {
320 mysqlsel $handle {select * from Unknown}
321 } -returnCodes error -match glob -result "*Table*"
323 tcltest::test {errorhandling-1.2} {error in sql} -body {
324 mysqlexec $handle {unknown command}
325 } -returnCodes error -match glob -result "*SQL syntax*"
327 tcltest::test {errorhandling-1.3} {read after end} -body {
328 set rows [mysqlsel $handle {select * from Student}]
329 for {set x 0} {$x<$rows} {incr x} {
330 set res [mysqlnext $handle]
331 set nr [lindex $res 0]
332 set name [lindex $res 1]
333 set sem [lindex $res 2]
339 tcltest::test {errorhandling-1.4} {false map binding} -body {
340 #read after end by map
341 mysqlsel $handle {select * from Student}
342 mysqlmap $handle {nr name} {
343 set row [format "nr %16s name:%s" $nr $name]
346 mysqlmap $handle {nr name err err2} {
347 set row [format "nr %16s name:%s" $nr $name]
350 } -returnCodes error -match glob -result "*too many variables*"
352 tcltest::test {sel-1.2} {-list option} -body {
353 mysqlsel $handle {select * from Student} -list
357 tcltest::test {sel-1.3} {-flatlist option} -body {
358 mysqlsel $handle {select * from Student} -flatlist
362 tcltest::test {handle-1.0} {interanl finding handle} -body {
363 set shandle [string trim " $handle "]
364 mysqlinfo $shandle databases
370 tcltest::test {handle-1.1} {operation on closed handle} -body {
371 mysqlinfo $handle tables
373 } -returnCodes error -match glob -result "*handle already closed*"
375 tcltest::test {handle-1.2} {operation on closed handle} -body {
378 set a [string trim $a]
380 } -returnCodes error -match glob -result "*not mysqltcl handle*"
383 tcltest::test {handle-1.2} {open 20 connection, close all} -body {
384 for {set x 0} {$x<20} {incr x} {
385 lappend handles [getConnection]
388 mysqlsel $h {select * from Student}
394 tcltest::test {handle-1.3} {10 queries, close all} -body {
395 set handle [getConnection]
396 for {set x 0} {$x<10} {incr x} {
397 lappend queries [mysqlquery $handle {select * from Student}]
399 for {set x 0} {$x<10} {incr x} {
400 mysqlquery $handle {select * from Student}
403 mysqlnext [lindex $queries 0]
404 } -returnCodes error -match glob -result "*handle already closed*"
406 tcltest::test {handle-1.4} {10 queries, close all} -body {
407 set handle [getConnection]
408 mysqlquery $handle {select * from Student}
413 tcltest::test {handle-1.5} {Testing false connecting} -body {
414 mysqlconnect -user nouser -db nodb
415 } -returnCodes error -match glob -result "*Unknown database*"
418 set handle [getConnection]
420 tcltest::test {escape-1.0} {escaping} -body {
421 mysqlescape "art\"ur"
422 mysqlescape $handle "art\"ur"
426 tcltest::test {ping-1.0} {escaping} -body {
431 tcltest::test {changeuser-1.0} {escaping} -body {
432 mysqlchangeuser $handle root {}
433 mysqlchangeuser $handle root {} uni
437 # does not work for mysql4.1
438 tcltest::test {changeuser-1.1} {no such user} -body {
439 mysqlchangeuser $handle root {} nodb
440 } -returnCodes error -match glob -result "*Unknown database*"
442 tcltest::test {interpreter-1.0} {mysqltcl in slave interpreter} -body {
443 set handle [getConnection]
444 set i1 [interp create]
446 package require mysqltcl
447 set hdl [mysqlconnect -user $dbuser -db $dbank]
450 mysqlinfo $handle databases
455 tcltest::cleanupTests