]> git.sven.stormbind.net Git - sven/mysqltcl.git/blob - tests/test.tcl
Imported Upstream version 3.05
[sven/mysqltcl.git] / tests / test.tcl
1 #!/usr/bin/tcl
2 # Simple Test file to test all mysqltcl commands and parameters
3 # please create test database first
4 # from test.sql file
5 # >mysql -u root
6 # >create database uni;
7 #
8 # >mysql -u root <test.sql
9 # please adapt the parameters for mysqlconnect some lines above
10
11 if {[file exists libload.tcl]} {
12     source libload.tcl
13 } else {
14     source [file join [file dirname [info script]] libload.tcl]
15 }
16
17 # global connect variables
18 set dbuser root
19 set dbpassword ""
20 set dbank mysqltcltest
21
22 package require tcltest
23 variable SETUP {#common setup code}
24 variable CLEANUP {#common cleanup code}
25 tcltest::configure -verbose bet
26
27 proc getConnection {{addOptions {}} {withDB 1}} {
28     global dbuser dbpassword dbank
29     if {$withDB} {
30         append addOptions " -db $dbank"
31     }
32     if {$dbpassword ne ""} {
33             append addOptions " -password $dbpassword"
34     }
35     return [eval mysqlconnect -user $dbuser $addOptions]
36 }
37 proc prepareTestDB {} {
38     global dbank
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"
43     }
44     mysqluse $handle $dbank
45     
46     catch {mysqlexec $handle {drop table Student}}
47
48     mysqlexec $handle {
49         CREATE TABLE Student (
50                 MatrNr int NOT NULL auto_increment,
51                 Name varchar(20),
52                 Semester int,
53                 PRIMARY KEY (MatrNr)
54         )
55     }
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)"
65     mysqlclose $handle
66 }
67
68 prepareTestDB
69
70 tcltest::test {connect-1.1} {no encoding} -body {
71         getConnection {-encoding nonexisting}
72 } -returnCodes error -match glob -result "*unknown encoding*"
73
74 tcltest::test {connect-1.2} {binary encoding} -body {
75         set handle [getConnection {-encoding binary}]
76         mysqlclose $handle
77         return
78 }
79
80 tcltest::test {connect-1.3} {-ssl option} -body {
81         set handle [getConnection {-ssl 1}]
82         mysqlclose $handle
83         return
84 }
85
86 tcltest::test {connect-1.4} {-noschema option 1} -body {
87         set handle [getConnection {-noschema 1}]
88         mysqlclose $handle
89         return
90 }
91
92 tcltest::test {connect-1.5} {-noschema option 0} -body {
93         set handle [getConnection {-noschema 0}]
94         mysqlclose $handle
95         return
96 }
97
98 tcltest::test {connect-1.6} {-comperss option} -body {
99         set handle [getConnection {-compress 1}]
100         mysqlclose $handle
101         return
102 }
103
104 tcltest::test {connect-1.7} {-odbc option} -body {
105         set handle [getConnection {-odbc 1}]
106         mysqlclose $handle
107         return
108 }
109
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"
117         mysqlclose $handle
118         return $rname
119 } -result {Artur Trzewik}
120
121 tcltest::test {baseinfo-1.0} {base info} -body {
122         mysqlbaseinfo connectparameters
123         mysqlbaseinfo clientversion
124         return
125 }
126
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}
131    mysqlnext $handle
132    mysqlinfo $handle databases
133    mysqlclose $handle
134    return
135 }
136
137 set handle [getConnection]
138
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'"
142
143
144 tcltest::test {select-1.1} {Test sel and next functions} -body {
145    mysqluse $handle uni
146    set allrows [mysqlsel $handle {select * from Student}]
147    mysqlcol $handle -current {name type length table non_null prim_key decimals numeric}
148    # result status
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]
155    mysqlnext $handle
156    mysqlnext $handle
157    mysqlseek $handle 0
158    set scdcurrent2 [mysqlresult $handle current]
159    set isFirst [expr {[mysqlnext $handle] eq $firstRow}]
160    mysqlnext $handle
161    return [list $fstcurrent $scdcurrent $rowsComp $scdcurrent2 $isFirst]
162 } -result {0 1 1 0 1}
163
164 tcltest::test {map-1.0} {map function} -body {
165     mysqlsel $handle {
166        select MatrNr,Name from Student order by Name
167     }
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]
172     }
173     return
174 }
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]
180         }
181         mysqlseek $handle 0
182         mysqlmap $handle {nr name} {
183                 set row [format  "nr %16s  name:%s"  $nr $name]
184     }
185         return
186 }
187
188
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]
192     }
193     return
194 }
195
196 tcltest::test {receive-1.1} {with break} -body {
197     set count 0
198     mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} {
199        set res [list $nr $name]
200        if ($count>0) break
201        incr count
202     }
203     return
204 }
205
206 tcltest::test {receive-1.2} {with error} -body {
207     set count 0
208     mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} {
209        set res [list $nr $name]
210        if ($count>0) {
211            error "Test Error"
212        }
213        incr count
214     }
215     return
216 } -returnCodes error -result "Test Error"
217
218 tcltest::test {query-1.0} {base case} -body {
219         set query1 [mysqlquery $handle {select MatrNr,Name From Student Order By Name}]
220     mysqlnext $query1
221     set query2 [mysqlquery $handle {select MatrNr,Name From Student Order By Name}]
222         mysqlnext $query2
223         mysqlendquery $query1
224         mysqlnext $query2
225         mysqlresult $query2 cols
226     mysqlresult $query2 rows
227     mysqlresult $query2 current
228     mysqlseek $query2 0
229         mysqlnext $query2
230         mysqlresult $query2 current
231         mysqlcol $query2 -current {name type length table non_null prim_key decimals numeric}
232         mysqlendquery $query2
233         return
234 }
235
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*"
241
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)"
244         return
245 }
246
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"
253 } -result 1
254
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]
261         lindex $res 1
262 } -result NULL
263
264 tcltest::test {schema-1.0} {querry on schema} -body {
265         # Metadata querries
266         mysqlcol $handle Student name
267         mysqlcol $handle Student {name type length table non_null prim_key decimals numeric}
268         return
269 }
270
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?
278         return
279 }
280
281 tcltest::test {state-1.0} {state} -body {
282         mysqlstate $handle
283         mysqlstate $handle -numeric
284         return
285 }
286
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*"
290
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*"
294
295 tcltest::test {errorhandling-1.2} {error in sql} -body {
296         mysqlexec $handle {unknown command}
297 } -returnCodes error -match glob -result "*SQL syntax*"
298
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]
306         }
307         mysqlnext $handle
308 } -result {}
309
310
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]
316         }
317         mysqlseek $handle 0
318         mysqlmap $handle {nr name err err2} {
319                 set row [format  "nr %16s  name:%s"  $nr $name]
320     }
321         return
322 } -returnCodes error -match glob -result "*too many variables*"
323
324 tcltest::test {sel-1.2} {-list option} -body {
325         mysqlsel $handle {select * from Student} -list
326         return
327 }
328
329 tcltest::test {sel-1.3} {-flatlist option} -body {
330         mysqlsel $handle {select * from Student} -flatlist
331         return
332 }
333
334 tcltest::test {handle-1.0} {interanl finding handle} -body {
335         set shandle [string trim " $handle "]
336         mysqlinfo $shandle databases
337         return
338 }
339
340 mysqlclose $handle
341
342 tcltest::test {handle-1.1} {operation on closed handle} -body {
343         mysqlinfo $handle tables
344         return
345 } -returnCodes error -match glob -result "*handle already closed*"
346
347 tcltest::test {handle-1.2} {operation on closed handle} -body {
348         set a " $handle "
349         unset handle
350         set a [string trim $a]
351         mysqlinfo $a tables
352 } -returnCodes error -match glob -result "*not mysqltcl handle*"
353
354
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]
358         }
359         foreach h $handles {
360             mysqlsel $h {select * from Student}
361         }
362         mysqlclose
363         return
364 }
365
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}]
370         }
371         for {set x 0} {$x<10} {incr x} {
372                 mysqlquery $handle {select * from Student}
373         }
374         mysqlclose $handle
375         mysqlnext [lindex $queries 0]
376 } -returnCodes error -match glob -result "*handle already closed*"
377
378 tcltest::test {handle-1.4} {10 queries, close all} -body {
379         set handle [getConnection]
380         mysqlquery $handle {select * from Student}
381         mysqlclose
382         return
383 }
384
385 tcltest::test {handle-1.5} {Testing false connecting} -body {
386         mysqlconnect -user nouser -db nodb
387 } -returnCodes error -match glob -result "*Unknown database*"
388
389
390 set handle [getConnection]
391
392 tcltest::test {escape-1.0} {escaping} -body {
393         mysqlescape "art\"ur"
394         mysqlescape $handle "art\"ur"
395         return
396 }
397
398 tcltest::test {ping-1.0} {escaping} -body {
399         mysqlping $handle
400         return
401 }
402
403 tcltest::test {changeuser-1.0} {escaping} -body {
404         mysqlchangeuser $handle root {}
405         mysqlchangeuser $handle root {} uni
406         return
407 }
408
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*"
413
414 tcltest::test {interpreter-1.0} {mysqltcl in slave interpreter} -body {
415         set handle [getConnection]
416         set i1 [interp create]
417         $i1 eval "
418           package require mysqltcl
419           set hdl [mysqlconnect -user $dbuser -db $dbank]
420         "
421         interp delete $i1
422         mysqlinfo $handle databases
423         mysqlclose $handle
424         return
425 }
426
427 tcltest::test {mysql::state-1.0} {wrong parameter length} -body {
428         mysql::state
429         return
430 } -returnCodes error -match glob -result "*wrong*"
431
432 tcltest::cleanupTests
433 puts "End of test"