]> git.sven.stormbind.net Git - sven/mysqltcl.git/blob - tests/test.test
releasing package mysqltcl version 3.052-4
[sven/mysqltcl.git] / tests / test.test
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
18 # global connect variables
19 set dbuser root
20 set dbpassword ""
21 set dbank mysqltcltest
22
23 package require tcltest
24 variable SETUP {#common setup code}
25 variable CLEANUP {#common cleanup code}
26 tcltest::configure -verbose bet
27
28 proc getConnection {{addOptions {}} {withDB 1}} {
29     global dbuser dbpassword dbank
30     if {$withDB} {
31         append addOptions " -db $dbank"
32     }
33     if {$dbpassword ne ""} {
34             append addOptions " -password $dbpassword"
35     }
36     return [eval mysqlconnect -user $dbuser $addOptions]
37 }
38 proc prepareTestDB {} {
39     global dbank
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"
44     }
45     mysqluse $handle $dbank
46     
47     catch {mysqlexec $handle {drop table Student}}
48
49     mysqlexec $handle {
50         CREATE TABLE Student (
51                 MatrNr int NOT NULL auto_increment,
52                 Name varchar(20),
53                 Semester int,
54                 PRIMARY KEY (MatrNr)
55         )
56     }
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)"
66     mysqlclose $handle
67 }
68
69 prepareTestDB
70
71 tcltest::test {connect-1.1} {no encoding} -body {
72         getConnection {-encoding nonexisting}
73 } -returnCodes error -match glob -result "*unknown encoding*"
74
75 tcltest::test {connect-1.2} {binary encoding} -body {
76         set handle [getConnection {-encoding binary}]
77         mysqlclose $handle
78         return
79 }
80
81 tcltest::test {connect-1.3} {-ssl option} -body {
82         set handle [getConnection {-ssl 1}]
83         mysqlclose $handle
84         return
85 }
86
87 tcltest::test {connect-1.4} {-noschema option 1} -body {
88         set handle [getConnection {-noschema 1}]
89         mysqlclose $handle
90         return
91 }
92
93 tcltest::test {connect-1.5} {-noschema option 0} -body {
94         set handle [getConnection {-noschema 0}]
95         mysqlclose $handle
96         return
97 }
98
99 tcltest::test {connect-1.6} {-comperss option} -body {
100         set handle [getConnection {-compress 1}]
101         mysqlclose $handle
102         return
103 }
104
105 tcltest::test {connect-1.7} {-odbc option} -body {
106         set handle [getConnection {-odbc 1}]
107         mysqlclose $handle
108         return
109 }
110
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"
118         mysqlclose $handle
119         return $rname
120 } -result {Artur Trzewik}
121
122 tcltest::test {baseinfo-1.0} {base info} -body {
123         mysqlbaseinfo connectparameters
124         mysqlbaseinfo clientversion
125         return
126 }
127
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}
132    mysqlnext $handle
133    mysqlinfo $handle databases
134    mysqlclose $handle
135    return
136 }
137
138 set handle [getConnection]
139
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'"
143
144
145 tcltest::test {select-1.1} {Test sel and next functions} -body {
146    mysqluse $handle uni
147    set allrows [mysqlsel $handle {select * from Student}]
148    mysqlcol $handle -current {name type length table non_null prim_key decimals numeric}
149    # result status
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]
156    mysqlnext $handle
157    mysqlnext $handle
158    mysqlseek $handle 0
159    set scdcurrent2 [mysqlresult $handle current]
160    set isFirst [expr {[mysqlnext $handle] eq $firstRow}]
161    mysqlnext $handle
162    return [list $fstcurrent $scdcurrent $rowsComp $scdcurrent2 $isFirst]
163 } -result {0 1 1 0 1}
164
165 tcltest::test {map-1.0} {map function} -body {
166     mysqlsel $handle {
167        select MatrNr,Name from Student order by Name
168     }
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]
173     }
174     return
175 }
176
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]
182         }
183         mysqlseek $handle 0
184         mysqlmap $handle {nr name} {
185                 set row [format  "nr %16s  name:%s"  $nr $name]
186     }
187         return
188 }
189
190 # Test error in version 3.01
191 tcltest::test {map-1.2} {map function error 3.01} -body {
192     mysqlsel $handle {
193        select Name from Student order by Name
194     }
195     namespace eval :: {
196         mysqlmap $handle name {
197             set a $name
198         }
199     }
200     return
201 }
202
203
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]
207     }
208     return
209 }
210
211 tcltest::test {receive-1.1} {with break} -body {
212     set count 0
213     mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} {
214        set res [list $nr $name]
215        if ($count>0) break
216        incr count
217     }
218     return
219 }
220
221 tcltest::test {receive-1.2} {with error} -body {
222     set count 0
223     mysqlreceive $handle {select MatrNr,Name from Student order by Name} {nr name} {
224        set res [list $nr $name]
225        if ($count>0) {
226            error "Test Error"
227        }
228        incr count
229     }
230     return
231 } -returnCodes error -result "Test Error"
232
233 # test error in 3.01
234 tcltest::test {receive-1.3} {with error 3.01} -body {
235     set count 0
236     namespace eval :: {
237         mysqlreceive $handle {select Name from Student order by Name} name {
238             set res [list $name]
239             incr count
240         }
241     }
242     return
243 }
244
245
246 tcltest::test {query-1.0} {base case} -body {
247         set query1 [mysqlquery $handle {select MatrNr,Name From Student Order By Name}]
248     mysqlnext $query1
249     set query2 [mysqlquery $handle {select MatrNr,Name From Student Order By Name}]
250         mysqlnext $query2
251         mysqlendquery $query1
252         mysqlnext $query2
253         mysqlresult $query2 cols
254     mysqlresult $query2 rows
255     mysqlresult $query2 current
256     mysqlseek $query2 0
257         mysqlnext $query2
258         mysqlresult $query2 current
259         mysqlcol $query2 -current {name type length table non_null prim_key decimals numeric}
260         mysqlendquery $query2
261         return
262 }
263
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*"
269
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)"
272         return
273 }
274
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"
281 } -result 1
282
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]
289         lindex $res 1
290 } -result NULL
291
292 tcltest::test {schema-1.0} {querry on schema} -body {
293         # Metadata querries
294         mysqlcol $handle Student name
295         mysqlcol $handle Student {name type length table non_null prim_key decimals numeric}
296         return
297 }
298
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?
306         return
307 }
308
309 tcltest::test {state-1.0} {state} -body {
310         mysqlstate $handle
311         mysqlstate $handle -numeric
312         return
313 }
314
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*"
318
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*"
322
323 tcltest::test {errorhandling-1.2} {error in sql} -body {
324         mysqlexec $handle {unknown command}
325 } -returnCodes error -match glob -result "*SQL syntax*"
326
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]
334         }
335         mysqlnext $handle
336 } -result {}
337
338
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]
344         }
345         mysqlseek $handle 0
346         mysqlmap $handle {nr name err err2} {
347                 set row [format  "nr %16s  name:%s"  $nr $name]
348     }
349         return
350 } -returnCodes error -match glob -result "*too many variables*"
351
352 tcltest::test {sel-1.2} {-list option} -body {
353         mysqlsel $handle {select * from Student} -list
354         return
355 }
356
357 tcltest::test {sel-1.3} {-flatlist option} -body {
358         mysqlsel $handle {select * from Student} -flatlist
359         return
360 }
361
362 tcltest::test {handle-1.0} {interanl finding handle} -body {
363         set shandle [string trim " $handle "]
364         mysqlinfo $shandle databases
365         return
366 }
367
368 mysqlclose $handle
369
370 tcltest::test {handle-1.1} {operation on closed handle} -body {
371         mysqlinfo $handle tables
372         return
373 } -returnCodes error -match glob -result "*handle already closed*"
374
375 tcltest::test {handle-1.2} {operation on closed handle} -body {
376         set a " $handle "
377         unset handle
378         set a [string trim $a]
379         mysqlinfo $a tables
380 } -returnCodes error -match glob -result "*not mysqltcl handle*"
381
382
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]
386         }
387         foreach h $handles {
388             mysqlsel $h {select * from Student}
389         }
390         mysqlclose
391         return
392 }
393
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}]
398         }
399         for {set x 0} {$x<10} {incr x} {
400                 mysqlquery $handle {select * from Student}
401         }
402         mysqlclose $handle
403         mysqlnext [lindex $queries 0]
404 } -returnCodes error -match glob -result "*handle already closed*"
405
406 tcltest::test {handle-1.4} {10 queries, close all} -body {
407         set handle [getConnection]
408         mysqlquery $handle {select * from Student}
409         mysqlclose
410         return
411 }
412
413 tcltest::test {handle-1.5} {Testing false connecting} -body {
414         mysqlconnect -user nouser -db nodb
415 } -returnCodes error -match glob -result "*Unknown database*"
416
417
418 set handle [getConnection]
419
420 tcltest::test {escape-1.0} {escaping} -body {
421         mysqlescape "art\"ur"
422         mysqlescape $handle "art\"ur"
423         return
424 }
425
426 tcltest::test {ping-1.0} {escaping} -body {
427         mysqlping $handle
428         return
429 }
430
431 tcltest::test {changeuser-1.0} {escaping} -body {
432         mysqlchangeuser $handle root {}
433         mysqlchangeuser $handle root {} uni
434         return
435 }
436
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*"
441
442 tcltest::test {interpreter-1.0} {mysqltcl in slave interpreter} -body {
443         set handle [getConnection]
444         set i1 [interp create]
445         $i1 eval "
446           package require mysqltcl
447           set hdl [mysqlconnect -user $dbuser -db $dbank]
448         "
449         interp delete $i1
450         mysqlinfo $handle databases
451         mysqlclose $handle
452         return
453 }
454
455 tcltest::cleanupTests
456 puts "End of test"