3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # This file implements regression tests for SQLite library. The
13 # focus of this script is transactions
15 # $Id: trans2.test,v 1.1 2008/08/27 18:56:36 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # A procedure to scramble the elements of list $inlist into a random order.
22 proc scramble {inlist} {
25 lappend y [list [expr {rand()}] $x]
30 lappend outlist [lindex $x 1]
35 # Generate a UUID using randomness.
40 for {set i 0} {$i<5} {incr i} {
41 append u [format %06x [expr {int(rand()*16777216)}]]
46 # Compute hashes on the u1 and u2 fields of the sample data.
51 foreach rec [lsort -integer -index 0 $data] {
52 append x [lindex $rec 1]
59 foreach rec [lsort -integer -index 0 $data] {
60 append x [lindex $rec 3]
65 # Create the initial data set
67 unset -nocomplain data i max_rowid todel n rec max1 id origres newres
68 unset -nocomplain inssql modsql s j z
70 for {set i 0} {$i<400} {incr i} {
71 set rec [list $i [random_uuid] [expr {int(rand()*5000)+1000}] [random_uuid]]
74 set max_rowid [expr {$i-1}]
76 # Create the T1 table used to hold test data. Populate that table with
77 # the initial data set and check hashes to make sure everything is correct.
81 PRAGMA cache_size=100;
83 id INTEGER PRIMARY KEY,
89 foreach rec [scramble $data] {
90 foreach {id u1 z u2} $rec break
91 db eval {INSERT INTO t1 VALUES($id,$u1,zeroblob($z),$u2)}
93 db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
94 } [list [hash1] [hash2]]
96 # Repeat the main test loop multiple times.
98 for {set i 2} {$i<=30} {incr i} {
99 # Delete one row out of every 10 in the database. This will add
100 # many pages to the freelist.
103 set n [expr {[llength $data]/10}]
104 set data [scramble $data]
105 foreach rec [lrange $data 0 $n] {
106 lappend todel [lindex $rec 0]
108 set data [lrange $data [expr {$n+1}] end]
109 set max1 [lindex [lindex $data 0] 0]
111 set id [lindex $rec 0]
112 if {$id>$max1} {set max1 $id}
114 set origres [list [hash1] [hash2]]
115 do_test trans2-$i.1 {
116 db eval "DELETE FROM t1 WHERE id IN ([join $todel ,])"
117 db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
119 integrity_check trans2-$i.2
121 # Begin a transaction and insert many new records.
125 set rec [list $id [random_uuid] \
126 [expr {int(rand()*5000)+1000}] [random_uuid]]
130 for {set j 1} {$j<50} {incr j} {
131 set id [expr {$max_rowid+$j}]
133 set rec [list $id [random_uuid] \
134 [expr {int(rand()*5000)+1000}] [random_uuid]]
138 set max_rowid [expr {$max_rowid+$j-1}]
141 set newres [list [hash1] [hash2]]
142 do_test trans2-$i.3 {
144 foreach rec [scramble $newdata] {
145 foreach {id u1 z u2} $rec break
146 set s "INSERT INTO t1 VALUES($id,'$u1',zeroblob($z),'$u2');"
151 db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
153 integrity_check trans2-$i.4
155 # Do a large update that aborts do to a constraint failure near
156 # the end. This stresses the statement journal mechanism.
158 do_test trans2-$i.10 {
160 UPDATE t1 SET u1=u1||'x',
161 z = CASE WHEN id<$max_rowid
162 THEN zeroblob((random()&65535)%5000 + 1000) END;
164 } {1 {NOT NULL constraint failed: t1.z}}
165 do_test trans2-$i.11 {
166 db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
169 # Delete all of the newly inserted records. Verify that the database
170 # is back to its original state.
172 do_test trans2-$i.20 {
173 set s "DELETE FROM t1 WHERE id IN ([join $todel ,]);"
176 db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
179 # Do another large update that aborts do to a constraint failure near
180 # the end. This stresses the statement journal mechanism.
182 do_test trans2-$i.30 {
184 UPDATE t1 SET u1=u1||'x',
185 z = CASE WHEN id<$max1
186 THEN zeroblob((random()&65535)%5000 + 1000) END;
188 } {1 {NOT NULL constraint failed: t1.z}}
189 do_test trans2-$i.31 {
190 db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
195 do_test trans2-$i.40 {
197 append modsql $inssql
198 db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
201 # Rollback the transaction. Verify that the content is restored.
203 do_test trans2-$i.90 {
205 db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
207 integrity_check trans2-$i.91
209 # Repeat all the changes, but this time commit.
211 do_test trans2-$i.92 {
214 UPDATE t1 SET u1=u1||'x',
215 z = CASE WHEN id<$max1
216 THEN zeroblob((random()&65535)%5000 + 1000) END;
220 UPDATE t1 SET u1=u1||'x',
221 z = CASE WHEN id<$max1
222 THEN zeroblob((random()&65535)%5000 + 1000) END;
225 db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
227 integrity_check trans2-$i.93
230 unset -nocomplain data i max_rowid todel n rec max1 id origres newres
231 unset -nocomplain inssql modsql s j z