1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
# 2008 August 27
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file implements regression tests for SQLite library. The
# focus of this script is transactions
#
# $Id: trans2.test,v 1.1 2008/08/27 18:56:36 drh Exp $
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# A procedure to scramble the elements of list $inlist into a random order.
#
proc scramble {inlist} {
set y {}
foreach x $inlist {
lappend y [list [expr {rand()}] $x]
}
set y [lsort $y]
set outlist {}
foreach x $y {
lappend outlist [lindex $x 1]
}
return $outlist
}
# Generate a UUID using randomness.
#
expr srand(1)
proc random_uuid {} {
set u {}
for {set i 0} {$i<5} {incr i} {
append u [format %06x [expr {int(rand()*16777216)}]]
}
return $u
}
# Compute hashes on the u1 and u2 fields of the sample data.
#
proc hash1 {} {
global data
set x ""
foreach rec [lsort -integer -index 0 $data] {
append x [lindex $rec 1]
}
return [md5 $x]
}
proc hash2 {} {
global data
set x ""
foreach rec [lsort -integer -index 0 $data] {
append x [lindex $rec 3]
}
return [md5 $x]
}
# Create the initial data set
#
unset -nocomplain data i max_rowid todel n rec max1 id origres newres
unset -nocomplain inssql modsql s j z
set data {}
for {set i 0} {$i<400} {incr i} {
set rec [list $i [random_uuid] [expr {int(rand()*5000)+1000}] [random_uuid]]
lappend data $rec
}
set max_rowid [expr {$i-1}]
# Create the T1 table used to hold test data. Populate that table with
# the initial data set and check hashes to make sure everything is correct.
#
do_test trans2-1.1 {
execsql {
PRAGMA cache_size=100;
CREATE TABLE t1(
id INTEGER PRIMARY KEY,
u1 TEXT UNIQUE,
z BLOB NOT NULL,
u2 TEXT UNIQUE
);
}
foreach rec [scramble $data] {
foreach {id u1 z u2} $rec break
db eval {INSERT INTO t1 VALUES($id,$u1,zeroblob($z),$u2)}
}
db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
} [list [hash1] [hash2]]
# Repeat the main test loop multiple times.
#
for {set i 2} {$i<=30} {incr i} {
# Delete one row out of every 10 in the database. This will add
# many pages to the freelist.
#
set todel {}
set n [expr {[llength $data]/10}]
set data [scramble $data]
foreach rec [lrange $data 0 $n] {
lappend todel [lindex $rec 0]
}
set data [lrange $data [expr {$n+1}] end]
set max1 [lindex [lindex $data 0] 0]
foreach rec $data {
set id [lindex $rec 0]
if {$id>$max1} {set max1 $id}
}
set origres [list [hash1] [hash2]]
do_test trans2-$i.1 {
db eval "DELETE FROM t1 WHERE id IN ([join $todel ,])"
db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
} $origres
integrity_check trans2-$i.2
# Begin a transaction and insert many new records.
#
set newdata {}
foreach id $todel {
set rec [list $id [random_uuid] \
[expr {int(rand()*5000)+1000}] [random_uuid]]
lappend newdata $rec
lappend data $rec
}
for {set j 1} {$j<50} {incr j} {
set id [expr {$max_rowid+$j}]
lappend todel $id
set rec [list $id [random_uuid] \
[expr {int(rand()*5000)+1000}] [random_uuid]]
lappend newdata $rec
lappend data $rec
}
set max_rowid [expr {$max_rowid+$j-1}]
set modsql {}
set inssql {}
set newres [list [hash1] [hash2]]
do_test trans2-$i.3 {
db eval BEGIN
foreach rec [scramble $newdata] {
foreach {id u1 z u2} $rec break
set s "INSERT INTO t1 VALUES($id,'$u1',zeroblob($z),'$u2');"
append modsql $s\n
append inssql $s\n
db eval $s
}
db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
} $newres
integrity_check trans2-$i.4
# Do a large update that aborts do to a constraint failure near
# the end. This stresses the statement journal mechanism.
#
do_test trans2-$i.10 {
catchsql {
UPDATE t1 SET u1=u1||'x',
z = CASE WHEN id<$max_rowid
THEN zeroblob((random()&65535)%5000 + 1000) END;
}
} {1 {NOT NULL constraint failed: t1.z}}
do_test trans2-$i.11 {
db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
} $newres
# Delete all of the newly inserted records. Verify that the database
# is back to its original state.
#
do_test trans2-$i.20 {
set s "DELETE FROM t1 WHERE id IN ([join $todel ,]);"
append modsql $s\n
db eval $s
db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
} $origres
# Do another large update that aborts do to a constraint failure near
# the end. This stresses the statement journal mechanism.
#
do_test trans2-$i.30 {
catchsql {
UPDATE t1 SET u1=u1||'x',
z = CASE WHEN id<$max1
THEN zeroblob((random()&65535)%5000 + 1000) END;
}
} {1 {NOT NULL constraint failed: t1.z}}
do_test trans2-$i.31 {
db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
} $origres
# Redo the inserts
#
do_test trans2-$i.40 {
db eval $inssql
append modsql $inssql
db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
} $newres
# Rollback the transaction. Verify that the content is restored.
#
do_test trans2-$i.90 {
db eval ROLLBACK
db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
} $origres
integrity_check trans2-$i.91
# Repeat all the changes, but this time commit.
#
do_test trans2-$i.92 {
db eval BEGIN
catchsql {
UPDATE t1 SET u1=u1||'x',
z = CASE WHEN id<$max1
THEN zeroblob((random()&65535)%5000 + 1000) END;
}
db eval $modsql
catchsql {
UPDATE t1 SET u1=u1||'x',
z = CASE WHEN id<$max1
THEN zeroblob((random()&65535)%5000 + 1000) END;
}
db eval COMMIT
db eval {SELECT md5sum(u1), md5sum(u2) FROM t1 ORDER BY id}
} $newres
integrity_check trans2-$i.93
}
unset -nocomplain data i max_rowid todel n rec max1 id origres newres
unset -nocomplain inssql modsql s j z
finish_test
|