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]
17 # global connect variables
20 set dbank mysqltcltest
22 package require tcltest
23 variable SETUP {#common setup code}
24 variable CLEANUP {#common cleanup code}
25 tcltest::configure -verbose bet
27 proc getConnection {{addOptions {}} {withDB 1}} {
28 global dbuser dbpassword dbank
30 append addOptions " -db $dbank"
32 if {$dbpassword ne ""} {
33 append addOptions " -password $dbpassword"
35 return [eval mysqlconnect -user $dbuser $addOptions]
37 proc prepareTestDB {} {
39 set handle [getConnection {} 0]
40 if {[lsearch [mysqlinfo $handle databases] $dbank]<0} {
41 puts "Testdatabase $dbank does not exist. Create it"
42 mysqlexec $handle "CREATE DATABASE $dbank"
44 mysqluse $handle $dbank
46 catch {mysqlexec $handle {drop table Student}}
49 CREATE TABLE Student (
50 MatrNr int NOT NULL auto_increment,
56 mysqlexec $handle "INSERT INTO Student VALUES (1,'Sojka',4)"
57 mysqlexec $handle "INSERT INTO Student VALUES (2,'Preisner',2)"
58 mysqlexec $handle "INSERT INTO Student VALUES (3,'Killar',2)"
59 mysqlexec $handle "INSERT INTO Student VALUES (4,'Penderecki',10)"
60 mysqlexec $handle "INSERT INTO Student VALUES (5,'Turnau',2)"
61 mysqlexec $handle "INSERT INTO Student VALUES (6,'Grechuta',3)"
62 mysqlexec $handle "INSERT INTO Student VALUES (7,'Gorniak',1)"
63 mysqlexec $handle "INSERT INTO Student VALUES (8,'Niemen',3)"
64 mysqlexec $handle "INSERT INTO Student VALUES (9,'Bem',5)"
70 tcltest::test {connect-1.1} {no encoding} -body {
71 getConnection {-encoding nonexisting}
72 } -returnCodes error -match glob -result "*unknown encoding*"
74 tcltest::test {connect-1.2} {binary encoding} -body {
75 set handle [getConnection {-encoding binary}]
80 tcltest::test {connect-1.3} {-ssl option} -body {
81 set handle [getConnection {-ssl 1}]
86 tcltest::test {connect-1.4} {-noschema option 1} -body {
87 set handle [getConnection {-noschema 1}]
92 tcltest::test {connect-1.5} {-noschema option 0} -body {
93 set handle [getConnection {-noschema 0}]
98 tcltest::test {connect-1.6} {-comperss option} -body {
99 set handle [getConnection {-compress 1}]
104 tcltest::test {connect-1.7} {-odbc option} -body {
105 set handle [getConnection {-odbc 1}]
110 tcltest::test {connect-1.8} {Test of encondig option} -body {
111 set handle [getConnection {-encoding iso8859-1}]
112 set name {Artur Trzewik}
113 mysqlexec $handle "INSERT INTO Student (Name,Semester) VALUES ('$name',11)"
114 set newid [mysqlinsertid $handle]
115 set rname [lindex [lindex [mysqlsel $handle "select Name from Student where MatrNr = $newid" -list] 0] 0]
116 mysqlexec $handle "DELETE FROM Student WHERE MatrNr = $newid"
119 } -result {Artur Trzewik}
121 tcltest::test {baseinfo-1.0} {base info} -body {
122 mysqlbaseinfo connectparameters
123 mysqlbaseinfo clientversion
127 tcltest::test {select-1.0} {use implicit database notation} -body {
128 set handle [getConnection {} 0]
129 mysqlsel $handle "select * from $dbank.Student"
130 mysqlcol $handle -current {name type length table non_null prim_key decimals numeric}
132 mysqlinfo $handle databases
137 set handle [getConnection]
139 tcltest::test {use-1.0} {false use} -body {
140 mysqluse $handle notdb2
141 } -returnCodes error -match glob -result "mysqluse/db server: Unknown database 'notdb2'"
144 tcltest::test {select-1.1} {Test sel and next functions} -body {
146 set allrows [mysqlsel $handle {select * from Student}]
147 mysqlcol $handle -current {name type length table non_null prim_key decimals numeric}
149 mysqlresult $handle cols
150 set rows [mysqlresult $handle rows]
151 set rowsComp [expr {$allrows==$rows}]
152 set fstcurrent [mysqlresult $handle current]
153 set firstRow [mysqlnext $handle]
154 set scdcurrent [mysqlresult $handle current]
158 set scdcurrent2 [mysqlresult $handle current]
159 set isFirst [expr {[mysqlnext $handle] eq $firstRow}]
161 return [list $fstcurrent $scdcurrent $rowsComp $scdcurrent2 $isFirst]
162 } -result {0 1 1 0 1}
164 tcltest::test {map-1.0} {map function} -body {
166 select MatrNr,Name from Student order by Name
168 mysqlmap $handle {nr name} {
169 if {$nr == {}} continue
170 set tempr [list $nr $name]
171 set row [format "nr %16s name:%s" $nr $name]
175 tcltest::test {map-1.1} {double map with seek} -body {
176 #read after end by map
177 mysqlsel $handle {select * from Student}
178 mysqlmap $handle {nr name} {
179 set row [format "nr %16s name:%s" $nr $name]
182 mysqlmap $handle {nr name} {
183 set row [format "nr %16s name:%s" $nr $name]
189 tcltest::test {receive-1.0} {base case} -body {
190 mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} {
191 set res [list $nr $name]
196 tcltest::test {receive-1.1} {with break} -body {
198 mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} {
199 set res [list $nr $name]
206 tcltest::test {receive-1.2} {with error} -body {
208 mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} {
209 set res [list $nr $name]
216 } -returnCodes error -result "Test Error"
218 tcltest::test {query-1.0} {base case} -body {
219 set query1 [mysqlquery $handle {select MatrNr,Name From Student Order By Name}]
221 set query2 [mysqlquery $handle {select MatrNr,Name From Student Order By Name}]
223 mysqlendquery $query1
225 mysqlresult $query2 cols
226 mysqlresult $query2 rows
227 mysqlresult $query2 current
230 mysqlresult $query2 current
231 mysqlcol $query2 -current {name type length table non_null prim_key decimals numeric}
232 mysqlendquery $query2
236 tcltest::test {query-1.1} {endquery on handle} -body {
237 mysqlsel $handle {select * from Student}
238 mysqlendquery $handle
239 mysqlresult $handle current
240 } -returnCodes error -match glob -result "*no result*"
242 tcltest::test {status-1.0} {read status array} -body {
243 set ret "code=$mysqlstatus(code) command=$mysqlstatus(command) message=$mysqlstatus(message) nullvalue=$mysqlstatus(nullvalue)"
247 tcltest::test {insert-1.0} {new insert id check} -body {
248 mysqlexec $handle {INSERT INTO Student (Name,Semester) VALUES ('Artur Trzewik',11)}
249 set newid [mysqlinsertid $handle]
250 mysqlexec $handle "UPDATE Student SET Semester=12 WHERE MatrNr=$newid"
251 mysqlinfo $handle info
252 mysqlexec $handle "DELETE FROM Student WHERE MatrNr=$newid"
255 tcltest::test {nullvalue-1.0} {null value handling} -body {
256 # Test NULL Value setting
257 mysqlexec $handle {INSERT INTO Student (Name) VALUES (Null)}
258 set id [mysqlinsertid $handle]
259 set mysqlstatus(nullvalue) NULL
260 set res [lindex [mysqlsel $handle "select Name,Semester from Student where MatrNr=$id" -list] 0]
264 tcltest::test {schema-1.0} {querry on schema} -body {
266 mysqlcol $handle Student name
267 mysqlcol $handle Student {name type length table non_null prim_key decimals numeric}
271 tcltest::test {info-1.0} {info} -body {
272 mysqlinfo $handle databases
273 mysqlinfo $handle dbname
274 mysqlinfo $handle host
275 mysqlinfo $handle tables
276 mysqlinfo $handle dbname?
277 mysqlinfo $handle host?
281 tcltest::test {state-1.0} {state} -body {
283 mysqlstate $handle -numeric
287 tcltest::test {errorhandling-1.0} {not a handle} -body {
288 mysqlsel bad0 {select * from Student}
289 } -returnCodes error -match glob -result "*not mysqltcl handle*"
291 tcltest::test {errorhandling-1.1} {error in sql select, no table} -body {
292 mysqlsel $handle {select * from Unknown}
293 } -returnCodes error -match glob -result "*Table*"
295 tcltest::test {errorhandling-1.2} {error in sql} -body {
296 mysqlexec $handle {unknown command}
297 } -returnCodes error -match glob -result "*SQL syntax*"
299 tcltest::test {errorhandling-1.3} {read after end} -body {
300 set rows [mysqlsel $handle {select * from Student}]
301 for {set x 0} {$x<$rows} {incr x} {
302 set res [mysqlnext $handle]
303 set nr [lindex $res 0]
304 set name [lindex $res 1]
305 set sem [lindex $res 2]
311 tcltest::test {errorhandling-1.4} {false map binding} -body {
312 #read after end by map
313 mysqlsel $handle {select * from Student}
314 mysqlmap $handle {nr name} {
315 set row [format "nr %16s name:%s" $nr $name]
318 mysqlmap $handle {nr name err err2} {
319 set row [format "nr %16s name:%s" $nr $name]
322 } -returnCodes error -match glob -result "*too many variables*"
324 tcltest::test {sel-1.2} {-list option} -body {
325 mysqlsel $handle {select * from Student} -list
329 tcltest::test {sel-1.3} {-flatlist option} -body {
330 mysqlsel $handle {select * from Student} -flatlist
334 tcltest::test {handle-1.0} {interanl finding handle} -body {
335 set shandle [string trim " $handle "]
336 mysqlinfo $shandle databases
342 tcltest::test {handle-1.1} {operation on closed handle} -body {
343 mysqlinfo $handle tables
345 } -returnCodes error -match glob -result "*handle already closed*"
347 tcltest::test {handle-1.2} {operation on closed handle} -body {
350 set a [string trim $a]
352 } -returnCodes error -match glob -result "*not mysqltcl handle*"
355 tcltest::test {handle-1.2} {open 20 connection, close all} -body {
356 for {set x 0} {$x<20} {incr x} {
357 lappend handles [getConnection]
360 mysqlsel $h {select * from Student}
366 tcltest::test {handle-1.3} {10 queries, close all} -body {
367 set handle [getConnection]
368 for {set x 0} {$x<10} {incr x} {
369 lappend queries [mysqlquery $handle {select * from Student}]
371 for {set x 0} {$x<10} {incr x} {
372 mysqlquery $handle {select * from Student}
375 mysqlnext [lindex $queries 0]
376 } -returnCodes error -match glob -result "*handle already closed*"
378 tcltest::test {handle-1.4} {10 queries, close all} -body {
379 set handle [getConnection]
380 mysqlquery $handle {select * from Student}
385 tcltest::test {handle-1.5} {Testing false connecting} -body {
386 mysqlconnect -user nouser -db nodb
387 } -returnCodes error -match glob -result "*Unknown database*"
390 set handle [getConnection]
392 tcltest::test {escape-1.0} {escaping} -body {
393 mysqlescape "art\"ur"
394 mysqlescape $handle "art\"ur"
398 tcltest::test {ping-1.0} {escaping} -body {
403 tcltest::test {changeuser-1.0} {escaping} -body {
404 mysqlchangeuser $handle root {}
405 mysqlchangeuser $handle root {} uni
409 # does not work for mysql4.1
410 tcltest::test {changeuser-1.1} {no such user} -body {
411 mysqlchangeuser $handle root {} nodb
412 } -returnCodes error -match glob -result "*Unknown database*"
414 tcltest::test {interpreter-1.0} {mysqltcl in slave interpreter} -body {
415 set handle [getConnection]
416 set i1 [interp create]
418 package require mysqltcl
419 set hdl [mysqlconnect -user $dbuser -db $dbank]
422 mysqlinfo $handle databases
427 tcltest::test {mysql::state-1.0} {wrong parameter length} -body {
430 } -returnCodes error -match glob -result "*wrong*"
432 tcltest::cleanupTests