]> git.sven.stormbind.net Git - sven/mysqltcl.git/blob - tests/test41.tcl
Imported Upstream version 3.05
[sven/mysqltcl.git] / tests / test41.tcl
1 #!/usr/bin/tcl
2 # Simple Test file to test all mysqltcl commands and parameters
3 # up from version mysqltcl 3.0 and mysql 4.1
4 #
5 # please adapt the parameters for mysqlconnect some lines above
6
7 if {[file exists libload.tcl]} {
8     source libload.tcl
9 } else {
10     source [file join [file dirname [info script]] libload.tcl]
11 }
12
13 # global connect variables
14 set dbuser root
15 set dbpassword ""
16 set dbank mysqltcltest
17
18 package require tcltest
19 variable SETUP {#common setup code}
20 variable CLEANUP {#common cleanup code}
21 tcltest::configure -verbose bet
22
23 proc getConnection {{addOptions {}} {withDB 1}} {
24     global dbuser dbpassword dbank
25     if {$withDB} {
26         append addOptions " -db $dbank"
27     }
28     if {$dbpassword ne ""} {
29             append addOptions " -password $dbpassword"
30     }
31     return [eval mysqlconnect -user $dbuser $addOptions]
32 }
33 proc prepareTestDB {} {
34     global dbank
35     set handle [getConnection {} 0]
36     if {[lsearch [mysqlinfo $handle databases] $dbank]<0} {
37         puts "Testdatabase $dbank does not exist. Create it"
38         mysqlexec $handle "CREATE DATABASE $dbank"
39     }
40     mysqluse $handle $dbank
41
42     catch {mysql::exec $handle {drop table transtest}}
43     mysql::exec $handle {
44       create table transtest (
45          id int,
46          name varchar(20)
47       ) ENGINE=BerkeleyDB
48     }
49
50     catch {mysql::exec $handle {drop table Student}}
51     mysql::exec $handle {
52         CREATE TABLE Student (
53                 MatrNr int NOT NULL auto_increment,
54                 Name varchar(20),
55                 Semester int,
56                 PRIMARY KEY (MatrNr)
57         )
58     }
59     mysql::exec $handle "INSERT INTO Student VALUES (1,'Sojka',4)"
60     mysql::exec $handle "INSERT INTO Student VALUES (2,'Preisner',2)"
61     mysql::exec $handle "INSERT INTO Student VALUES (3,'Killar',2)"
62     mysql::exec $handle "INSERT INTO Student VALUES (4,'Penderecki',10)"
63     mysql::exec $handle "INSERT INTO Student VALUES (5,'Turnau',2)"
64     mysql::exec $handle "INSERT INTO Student VALUES (6,'Grechuta',3)"
65     mysql::exec $handle "INSERT INTO Student VALUES (7,'Gorniak',1)"
66     mysql::exec $handle "INSERT INTO Student VALUES (8,'Niemen',3)"
67     mysql::exec $handle "INSERT INTO Student VALUES (9,'Bem',5)"
68     mysql::close $handle
69 }
70
71 prepareTestDB
72 set conn [getConnection {-multistatement 1 -multiresult 1}]
73
74
75 tcltest::test {null-1.0} {creating of null} {
76   set null [mysql::newnull]
77   mysql::isnull $null
78 } {1}
79
80 tcltest::test {null-1.1} {null checking} {
81   mysql::isnull blabla
82 } {0}
83
84 tcltest::test {null-1.2} {null checking} {
85   mysql::isnull [mysql::newnull]
86 } {1}
87
88 tcltest::test {null-1.3} {null checking} {
89   mysql::isnull {}
90 } {0}
91
92 tcltest::test {null-1.4} {null checking} {
93   mysql::isnull [lindex [list [mysql::newnull]] 0]
94 } {1}
95
96
97 tcltest::test {autocommit} {setting autocommit} -body {
98    mysql::autocommit $conn 0
99 }
100 tcltest::test {autocommit} {setting autocommit} -body {
101    mysql::autocommit $conn 1
102 }
103 tcltest::test {autocommit} {setting false autocommit} -body {
104    mysql::autocommit $conn nobool
105 } -returnCodes error -match glob -result "expected boolean value*"
106
107 mysql::autocommit $conn 0
108
109 tcltest::test {commit} {commit} -body {
110    mysql::autocommit $conn 0
111    mysqlexec $conn {delete from transtest where name='committest'}
112    mysqlexec $conn {insert into transtest (name,id) values ('committest',2)}
113    mysql::commit $conn
114    set res [mysqlexec $conn {delete from transtest where name='committest'}]
115    mysql::commit $conn
116    return $res
117 } -result 1
118
119 tcltest::test {rollback-1.0} {roolback} -body {
120    mysql::autocommit $conn 0
121    mysqlexec $conn {delete from transtest where name='committest'}
122    mysqlexec $conn {insert into transtest (name,id) values ('committest',2)}
123    mysql::rollback $conn
124    set res [mysqlexec $conn {delete from transtest where name='committest'}]
125    mysql::commit $conn
126    return $res
127 } -result 0
128
129 tcltest::test {rollback-1.1} {roolback by auto-commit 1} -body {
130    mysql::autocommit $conn 1
131    mysqlexec $conn {delete from transtest where name='committest'}
132    mysqlexec $conn {insert into transtest (name,id) values ('committest',2)}
133    # rollback should not affect
134    mysql::rollback $conn
135    set res [mysqlexec $conn {delete from transtest where name='committest'}]
136    return $res
137 } -result 1
138
139
140 tcltest::test {warning-count-1.0} {check mysql::warningcount} -body {
141    set list [mysql::sel $conn {select * from Student} -list]
142    mysql::warningcount $conn
143 } -result 0
144
145 tcltest::test {multistatement-1.0} {inserting multi rows} -body {
146    mysql::exec $conn {
147       insert into transtest (name,id) values ('row1',31);
148       insert into transtest (name,id) values ('row2',32);
149       insert into transtest (name,id) values ('row3',33),('row4',34);
150    }
151
152 } -result {1 1 2}
153
154 tcltest::test {moreresult-1.3} {arg counts} -body {
155    mysql::moreresult
156 } -returnCodes error -match glob -result "wrong # args:*"
157
158 tcltest::test {moreresult-1.0} {only one result} -body {
159    mysql::ping $conn
160    mysql::sel $conn {select * from transtest}
161    mysql::moreresult $conn
162 } -result 0
163
164 tcltest::test {moreresult-1.1} {only one result} -body {
165    mysql::ping $conn
166    mysql::sel $conn {
167       select * from transtest;
168       select * from Student;
169    }
170    while {[llength [mysql::fetch $conn]]>0} {}
171    if {[set ret [mysql::moreresult $conn]]} {
172       # mysql::nextresult $conn
173    }
174    return $ret
175 } -result 1
176
177 tcltest::test {nextresult-1.0} {only one result} -body {
178    mysql::ping $conn
179    mysql::sel $conn {
180       select * from transtest;
181       select * from Student;
182    }
183    while {[llength [set row [mysql::fetch $conn]]]>0} {
184    }
185    mysql::nextresult $conn
186    set hadRow 0
187    while {[llength [set row [mysql::fetch $conn]]]>0} {
188       set hadRow 1
189    }
190    return $hadRow
191 } -result 1 -returnCodes 2
192
193 tcltest::test {setserveroption-1.0} {set multistatment off} -body {
194    mysql::setserveroption $conn -multi_statment_off
195    mysql::exec $conn {
196       insert into transtest (name,id) values ('row1',31);
197       insert into transtest (name,id) values ('row2',32);
198       insert into transtest (name,id) values ('row3',33);
199    }
200 } -returnCodes error -match glob -result "mysql::exec/db server*"
201
202 tcltest::test {setserveroption-1.1} {set multistatment on} -body {
203    mysql::setserveroption $conn -multi_statment_on
204    mysql::exec $conn {
205       insert into transtest (name,id) values ('row1',31);
206       insert into transtest (name,id) values ('row2',32);
207       insert into transtest (name,id) values ('row3',33);
208    }
209    return
210 }
211
212 tcltest::test {info-1.0} {asking about host} -body {
213    set res [mysql::info $conn host]
214    expr {[string length $res]>0}
215 } -result 1
216
217 tcltest::test {info-1.1} {serverversion} -body {
218   mysql::info $conn serverversion
219   expr {[mysql::info $conn serverversionid]>0}
220 } -result 1
221
222 tcltest::test {info-1.2} {sqlstate} -body {
223   mysql::info $conn sqlstate
224   return
225 }
226
227 tcltest::test {info-1.3} {sqlstate} -body {
228   mysql::info $conn state
229   return
230 }
231
232 tcltest::test {state-1.0} {reported bug in 3.51} -body {
233   mysql::state nothandle -numeric
234 } -result 0
235
236 tcltest::test {state-1.1} {reported bug in 3.51} -body {
237   mysql::state nothandle
238 } -result NOT_A_HANDLE
239
240 tcltest::test {null-2.0} {reading and checking null from database} -body {
241   mysql::ping $conn
242   mysql::autocommit $conn 1
243   mysql::exec $conn {
244        delete from transtest where name="nulltest"
245   }
246   mysql::exec $conn {
247        insert into transtest (name,id) values ('nulltest',NULL);
248   }
249   mysql::sel $conn {select id from transtest where name='nulltest'}
250   set res [lindex [mysql::fetch $conn] 0]
251   mysql::isnull $res
252 } -result 1
253
254 tcltest::test {baseinfo-1.0} {clientversionid} -body {
255   expr {[mysql::baseinfo clientversionid]>0}
256 } -result 1
257
258 tcltest::test {encoding-1.0} {read system encoding} -body {
259   mysql::encoding $conn
260 } -result [encoding system]
261
262 tcltest::test {encoding-1.1} {change to binary} -body {
263   mysql::encoding $conn binary
264   mysql::exec $conn "INSERT INTO Student (Name,Semester) VALUES ('Test',4)"
265   mysql::encoding $conn
266 } -result binary
267
268 tcltest::test {encoding-1.2} {change to binary} -body {
269   mysql::encoding $conn [encoding system]
270   mysql::exec $conn "INSERT INTO Student (Name,Semester) VALUES ('Test',4)"
271   mysql::encoding $conn
272 } -result [encoding system]
273
274 tcltest::test {encoding-1.3} {change to binary} -body {
275   mysql::encoding $conn iso8859-1
276   mysql::exec $conn "INSERT INTO Student (Name,Semester) VALUES ('Test',4)"
277   mysql::encoding $conn
278 } -result iso8859-1
279
280 tcltest::test {encoding-1.4} {unknown encoding} -body {
281   mysql::encoding $conn unknown
282 } -returnCodes error -match glob -result "unknown encoding*"
283
284 tcltest::test {encoding-1.5} {changing encoding of query handle} -body {
285   set q [mysql::query $conn "select * from Student"]
286   mysql::encoding $q iso8859-1
287 } -cleanup {
288   mysql::endquery $q
289 } -returnCodes error -result "encoding set can be used only on connection handle"
290
291 tcltest::test {encoding-1.6} {changing encoding of handle} -body {
292   mysql::encoding $conn iso8859-1
293   set q [mysql::query $conn "select * from Student"]
294   mysql::encoding $q
295 } -cleanup {
296   mysql::endquery $q
297 } -result iso8859-1
298
299 tcltest::test {encoding-1.7} {changing encoding of handle} -body {
300   set q [mysql::query $conn "select * from Student"]
301   mysql::encoding $conn iso8859-1
302   mysql::encoding $q
303 } -cleanup {
304   mysql::endquery $q
305 } -result iso8859-1
306
307 tcltest::test {encoding-1.8} {changing encoding of handle} -body {
308   mysql::encoding $conn utf-8
309   set q [mysql::query $conn "select * from Student"]
310   mysql::encoding $conn iso8859-1
311   mysql::encoding $q
312 } -cleanup {
313   mysql::endquery $q
314 } -result iso8859-1
315
316 tcltest::test {encoding-1.8} {changing encoding of handle} -body {
317   mysql::encoding $conn iso8859-5
318   set q [mysql::query $conn "select Name from Student"]
319   mysql::encoding $conn utf-8
320   mysql::fetch $q
321   mysql::endquery $q
322   return    
323 }
324
325 # no prepared statements in this version
326 if 0 {
327
328 tcltest::test {preparedstatment-1.0} {create test} -body {
329   set phandle [mysql::prepare $conn {insert into transtest (id,name) values (?,?)}]
330   mysql::close $phandle
331   return
332 }
333
334 tcltest::test {preparedstatment-1.1} {create errortest} -body {
335   set phandle [mysql::prepare $conn {nosql command ?,?}]
336   mysql::close $phandle
337   return
338 } -returnCodes error -match glob -result "*SQL*"
339
340 tcltest::test {preparedstatment-1.3} {select} -body {
341   set phandle [mysql::prepare $conn {select id,name from transtest}]
342   mysql::pselect $phandle
343   set rowcount 0
344   while {[llength [set row [mysql::fetch $phandle]]]>0} {
345          incr rowcount
346   }
347   mysql::close $phandle
348   return
349 }
350
351
352 tcltest::test {preparedstatment-1.2} {insert} -body {
353   set phandle [mysql::prepare $conn {insert into transtest (id,name) values (?,?)}]
354   set count [mysql::param $phandle count]
355   mysql::param $phandle type 0
356   mysql::param $phandle type 1
357   mysql::param $phandle type
358   mysql::pexecute $phandle 2 Artur
359   mysql::close $phandle
360   return $count
361 } -result 2
362
363
364 tcltest::test {preparedstatment-1.4} {select mit bind} -body {
365   set phandle [mysql::prepare $conn {select id,name from transtest where id=?}]
366   set countin [mysql::paramin $phandle count]
367   set countout [mysql::paramin $phandle count]
368   mysql::paramin $phandle type 0
369   mysql::paramin $phandle type
370   mysql::paramout $phandle type 0
371   mysql::paramout $phandle type 1
372   mysql::paramout $phandle type
373   mysql::execute $phandle
374   mysql::close $phandle
375   list $countin $countout
376 } -result {1 2}
377
378 }
379
380 tcltest::cleanupTests
381
382 puts "End of test"
383