sl@0: # 2001 September 15 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this script is in-memory database backend. sl@0: # sl@0: # $Id: memdb.test,v 1.15 2006/01/30 22:48:44 drh Exp $ sl@0: sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable memorydb { sl@0: sl@0: # In the following sequence of tests, compute the MD5 sum of the content sl@0: # of a table, make lots of modifications to that table, then do a rollback. sl@0: # Verify that after the rollback, the MD5 checksum is unchanged. sl@0: # sl@0: # These tests were browed from trans.tcl. sl@0: # sl@0: do_test memdb-1.1 { sl@0: db close sl@0: sqlite3 db :memory: sl@0: # sqlite3 db test.db sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t3(x TEXT); sl@0: INSERT INTO t3 VALUES(randstr(10,400)); sl@0: INSERT INTO t3 VALUES(randstr(10,400)); sl@0: INSERT INTO t3 SELECT randstr(10,400) FROM t3; sl@0: INSERT INTO t3 SELECT randstr(10,400) FROM t3; sl@0: INSERT INTO t3 SELECT randstr(10,400) FROM t3; sl@0: INSERT INTO t3 SELECT randstr(10,400) FROM t3; sl@0: INSERT INTO t3 SELECT randstr(10,400) FROM t3; sl@0: INSERT INTO t3 SELECT randstr(10,400) FROM t3; sl@0: INSERT INTO t3 SELECT randstr(10,400) FROM t3; sl@0: INSERT INTO t3 SELECT randstr(10,400) FROM t3; sl@0: INSERT INTO t3 SELECT randstr(10,400) FROM t3; sl@0: COMMIT; sl@0: SELECT count(*) FROM t3; sl@0: } sl@0: } {1024} sl@0: sl@0: # The following procedure computes a "signature" for table "t3". If sl@0: # T3 changes in any way, the signature should change. sl@0: # sl@0: # This is used to test ROLLBACK. We gather a signature for t3, then sl@0: # make lots of changes to t3, then rollback and take another signature. sl@0: # The two signatures should be the same. sl@0: # sl@0: proc signature {{fn {}}} { sl@0: set rx [db eval {SELECT x FROM t3}] sl@0: # set r1 [md5 $rx\n] sl@0: if {$fn!=""} { sl@0: # set fd [open $fn w] sl@0: # puts $fd $rx sl@0: # close $fd sl@0: } sl@0: # set r [db eval {SELECT count(*), md5sum(x) FROM t3}] sl@0: # puts "SIG($fn)=$r1" sl@0: return [list [string length $rx] $rx] sl@0: } sl@0: sl@0: # Do rollbacks. Make sure the signature does not change. sl@0: # sl@0: set limit 10 sl@0: for {set i 2} {$i<=$limit} {incr i} { sl@0: set ::sig [signature one] sl@0: # puts "sig=$sig" sl@0: set cnt [lindex $::sig 0] sl@0: if {$i%2==0} { sl@0: execsql {PRAGMA synchronous=FULL} sl@0: } else { sl@0: execsql {PRAGMA synchronous=NORMAL} sl@0: } sl@0: do_test memdb-1.$i.1-$cnt { sl@0: execsql { sl@0: BEGIN; sl@0: DELETE FROM t3 WHERE random()%10!=0; sl@0: INSERT INTO t3 SELECT randstr(10,10)||x FROM t3; sl@0: INSERT INTO t3 SELECT randstr(10,10)||x FROM t3; sl@0: ROLLBACK; sl@0: } sl@0: set sig2 [signature two] sl@0: } $sig sl@0: # puts "sig2=$sig2" sl@0: # if {$sig2!=$sig} exit sl@0: do_test memdb-1.$i.2-$cnt { sl@0: execsql { sl@0: BEGIN; sl@0: DELETE FROM t3 WHERE random()%10!=0; sl@0: INSERT INTO t3 SELECT randstr(10,10)||x FROM t3; sl@0: DELETE FROM t3 WHERE random()%10!=0; sl@0: INSERT INTO t3 SELECT randstr(10,10)||x FROM t3; sl@0: ROLLBACK; sl@0: } sl@0: signature sl@0: } $sig sl@0: if {$i<$limit} { sl@0: do_test memdb-1.$i.9-$cnt { sl@0: execsql { sl@0: INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE random()%10==0; sl@0: } sl@0: } {} sl@0: } sl@0: set ::pager_old_format 0 sl@0: } sl@0: sl@0: integrity_check memdb-2.1 sl@0: sl@0: do_test memdb-3.1 { sl@0: execsql { sl@0: CREATE TABLE t4(a,b,c,d); sl@0: BEGIN; sl@0: INSERT INTO t4 VALUES(1,2,3,4); sl@0: SELECT * FROM t4; sl@0: } sl@0: } {1 2 3 4} sl@0: do_test memdb-3.2 { sl@0: execsql { sl@0: SELECT name FROM sqlite_master WHERE type='table'; sl@0: } sl@0: } {t3 t4} sl@0: do_test memdb-3.3 { sl@0: execsql { sl@0: DROP TABLE t4; sl@0: SELECT name FROM sqlite_master WHERE type='table'; sl@0: } sl@0: } {t3} sl@0: do_test memdb-3.4 { sl@0: execsql { sl@0: ROLLBACK; sl@0: SELECT name FROM sqlite_master WHERE type='table'; sl@0: } sl@0: } {t3 t4} sl@0: sl@0: # Create tables for the first group of tests. sl@0: # sl@0: do_test memdb-4.0 { sl@0: execsql { sl@0: CREATE TABLE t1(a, b, c, UNIQUE(a,b)); sl@0: CREATE TABLE t2(x); sl@0: SELECT c FROM t1 ORDER BY c; sl@0: } sl@0: } {} sl@0: sl@0: # Six columns of configuration data as follows: sl@0: # sl@0: # i The reference number of the test sl@0: # conf The conflict resolution algorithm on the BEGIN statement sl@0: # cmd An INSERT or REPLACE command to execute against table t1 sl@0: # t0 True if there is an error from $cmd sl@0: # t1 Content of "c" column of t1 assuming no error in $cmd sl@0: # t2 Content of "x" column of t2 sl@0: # sl@0: foreach {i conf cmd t0 t1 t2} { sl@0: 1 {} INSERT 1 {} 1 sl@0: 2 {} {INSERT OR IGNORE} 0 3 1 sl@0: 3 {} {INSERT OR REPLACE} 0 4 1 sl@0: 4 {} REPLACE 0 4 1 sl@0: 5 {} {INSERT OR FAIL} 1 {} 1 sl@0: 6 {} {INSERT OR ABORT} 1 {} 1 sl@0: 7 {} {INSERT OR ROLLBACK} 1 {} {} sl@0: } { sl@0: sl@0: # All tests after test 1 depend on conflict resolution. So end the sl@0: # loop if that is not available in this build. sl@0: ifcapable !conflict {if {$i>1} break} sl@0: sl@0: do_test memdb-4.$i { sl@0: if {$conf!=""} {set conf "ON CONFLICT $conf"} sl@0: set r0 [catch {execsql [subst { sl@0: DELETE FROM t1; sl@0: DELETE FROM t2; sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: BEGIN $conf; sl@0: INSERT INTO t2 VALUES(1); sl@0: $cmd INTO t1 VALUES(1,2,4); sl@0: }]} r1] sl@0: catch {execsql {COMMIT}} sl@0: if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]} sl@0: set r2 [execsql {SELECT x FROM t2}] sl@0: list $r0 $r1 $r2 sl@0: } [list $t0 $t1 $t2] sl@0: } sl@0: sl@0: do_test memdb-5.0 { sl@0: execsql { sl@0: DROP TABLE t2; sl@0: DROP TABLE t3; sl@0: CREATE TABLE t2(a,b,c); sl@0: INSERT INTO t2 VALUES(1,2,1); sl@0: INSERT INTO t2 VALUES(2,3,2); sl@0: INSERT INTO t2 VALUES(3,4,1); sl@0: INSERT INTO t2 VALUES(4,5,4); sl@0: SELECT c FROM t2 ORDER BY b; sl@0: CREATE TABLE t3(x); sl@0: INSERT INTO t3 VALUES(1); sl@0: } sl@0: } {1 2 1 4} sl@0: sl@0: # Six columns of configuration data as follows: sl@0: # sl@0: # i The reference number of the test sl@0: # conf1 The conflict resolution algorithm on the UNIQUE constraint sl@0: # conf2 The conflict resolution algorithm on the BEGIN statement sl@0: # cmd An UPDATE command to execute against table t1 sl@0: # t0 True if there is an error from $cmd sl@0: # t1 Content of "b" column of t1 assuming no error in $cmd sl@0: # t2 Content of "x" column of t3 sl@0: # sl@0: foreach {i conf1 conf2 cmd t0 t1 t2} { sl@0: 1 {} {} UPDATE 1 {6 7 8 9} 1 sl@0: 2 REPLACE {} UPDATE 0 {7 6 9} 1 sl@0: 3 IGNORE {} UPDATE 0 {6 7 3 9} 1 sl@0: 4 FAIL {} UPDATE 1 {6 7 3 4} 1 sl@0: 5 ABORT {} UPDATE 1 {1 2 3 4} 1 sl@0: 6 ROLLBACK {} UPDATE 1 {1 2 3 4} 0 sl@0: 7 REPLACE {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 sl@0: 8 IGNORE {} {UPDATE OR REPLACE} 0 {7 6 9} 1 sl@0: 9 FAIL {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 sl@0: 10 ABORT {} {UPDATE OR REPLACE} 0 {7 6 9} 1 sl@0: 11 ROLLBACK {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 sl@0: 12 {} {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 sl@0: 13 {} {} {UPDATE OR REPLACE} 0 {7 6 9} 1 sl@0: 14 {} {} {UPDATE OR FAIL} 1 {6 7 3 4} 1 sl@0: 15 {} {} {UPDATE OR ABORT} 1 {1 2 3 4} 1 sl@0: 16 {} {} {UPDATE OR ROLLBACK} 1 {1 2 3 4} 0 sl@0: } { sl@0: # All tests after test 1 depend on conflict resolution. So end the sl@0: # loop if that is not available in this build. sl@0: ifcapable !conflict { sl@0: if {$i>1} break sl@0: } sl@0: sl@0: if {$t0} {set t1 {column a is not unique}} sl@0: do_test memdb-5.$i { sl@0: if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"} sl@0: if {$conf2!=""} {set conf2 "ON CONFLICT $conf2"} sl@0: set r0 [catch {execsql [subst { sl@0: DROP TABLE t1; sl@0: CREATE TABLE t1(a,b,c, UNIQUE(a) $conf1); sl@0: INSERT INTO t1 SELECT * FROM t2; sl@0: UPDATE t3 SET x=0; sl@0: BEGIN $conf2; sl@0: $cmd t3 SET x=1; sl@0: $cmd t1 SET b=b*2; sl@0: $cmd t1 SET a=c+5; sl@0: }]} r1] sl@0: catch {execsql {COMMIT}} sl@0: if {!$r0} {set r1 [execsql {SELECT a FROM t1 ORDER BY b}]} sl@0: set r2 [execsql {SELECT x FROM t3}] sl@0: list $r0 $r1 $r2 sl@0: } [list $t0 $t1 $t2] sl@0: } sl@0: sl@0: do_test memdb-6.1 { sl@0: execsql { sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 2 1 2 3 2 3 4 1 4 5 4} sl@0: do_test memdb-6.2 { sl@0: execsql { sl@0: BEGIN; sl@0: DROP TABLE t2; sl@0: SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1; sl@0: } sl@0: } {t1 t3 t4} sl@0: do_test memdb-6.3 { sl@0: execsql { sl@0: ROLLBACK; sl@0: SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1; sl@0: } sl@0: } {t1 t2 t3 t4} sl@0: do_test memdb-6.4 { sl@0: execsql { sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 2 1 2 3 2 3 4 1 4 5 4} sl@0: ifcapable compound { sl@0: do_test memdb-6.5 { sl@0: execsql { sl@0: SELECT a FROM t2 UNION SELECT b FROM t2 ORDER BY 1; sl@0: } sl@0: } {1 2 3 4 5} sl@0: } ;# ifcapable compound sl@0: do_test memdb-6.6 { sl@0: execsql { sl@0: CREATE INDEX i2 ON t2(c); sl@0: SELECT a FROM t2 ORDER BY c; sl@0: } sl@0: } {1 3 2 4} sl@0: do_test memdb-6.6 { sl@0: execsql { sl@0: SELECT a FROM t2 ORDER BY c DESC; sl@0: } sl@0: } {4 2 3 1} sl@0: do_test memdb-6.7 { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t5(x,y); sl@0: INSERT INTO t5 VALUES(1,2); sl@0: SELECT * FROM t5; sl@0: } sl@0: } {1 2} sl@0: do_test memdb-6.8 { sl@0: execsql { sl@0: SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1; sl@0: } sl@0: } {t1 t2 t3 t4 t5} sl@0: do_test memdb-6.9 { sl@0: execsql { sl@0: ROLLBACK; sl@0: SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1; sl@0: } sl@0: } {t1 t2 t3 t4} sl@0: do_test memdb-6.10 { sl@0: execsql { sl@0: CREATE TABLE t5(x PRIMARY KEY, y UNIQUE); sl@0: SELECT * FROM t5; sl@0: } sl@0: } {} sl@0: do_test memdb-6.11 { sl@0: execsql { sl@0: SELECT * FROM t5 ORDER BY y DESC; sl@0: } sl@0: } {} sl@0: sl@0: ifcapable conflict { sl@0: do_test memdb-6.12 { sl@0: execsql { sl@0: INSERT INTO t5 VALUES(1,2); sl@0: INSERT INTO t5 VALUES(3,4); sl@0: REPLACE INTO t5 VALUES(1,4); sl@0: SELECT rowid,* FROM t5; sl@0: } sl@0: } {3 1 4} sl@0: do_test memdb-6.13 { sl@0: execsql { sl@0: DELETE FROM t5 WHERE x>5; sl@0: SELECT * FROM t5; sl@0: } sl@0: } {1 4} sl@0: do_test memdb-6.14 { sl@0: execsql { sl@0: DELETE FROM t5 WHERE y<3; sl@0: SELECT * FROM t5; sl@0: } sl@0: } {1 4} sl@0: } sl@0: sl@0: do_test memdb-6.15 { sl@0: execsql { sl@0: DELETE FROM t5 WHERE x>0; sl@0: SELECT * FROM t5; sl@0: } sl@0: } {} sl@0: sl@0: ifcapable subquery { sl@0: do_test memdb-7.1 { sl@0: execsql { sl@0: CREATE TABLE t6(x); sl@0: INSERT INTO t6 VALUES(1); sl@0: INSERT INTO t6 SELECT x+1 FROM t6; sl@0: INSERT INTO t6 SELECT x+2 FROM t6; sl@0: INSERT INTO t6 SELECT x+4 FROM t6; sl@0: INSERT INTO t6 SELECT x+8 FROM t6; sl@0: INSERT INTO t6 SELECT x+16 FROM t6; sl@0: INSERT INTO t6 SELECT x+32 FROM t6; sl@0: INSERT INTO t6 SELECT x+64 FROM t6; sl@0: INSERT INTO t6 SELECT x+128 FROM t6; sl@0: SELECT count(*) FROM (SELECT DISTINCT x FROM t6); sl@0: } sl@0: } {256} sl@0: for {set i 1} {$i<=256} {incr i} { sl@0: do_test memdb-7.2.$i { sl@0: execsql "DELETE FROM t6 WHERE x=\ sl@0: (SELECT x FROM t6 ORDER BY random() LIMIT 1)" sl@0: execsql {SELECT count(*) FROM t6} sl@0: } [expr {256-$i}] sl@0: } sl@0: } sl@0: sl@0: # Ticket #1524 sl@0: # sl@0: do_test memdb-8.1 { sl@0: db close sl@0: sqlite3 db {:memory:} sl@0: execsql { sl@0: PRAGMA auto_vacuum=TRUE; sl@0: CREATE TABLE t1(a); sl@0: INSERT INTO t1 VALUES(randstr(5000,6000)); sl@0: INSERT INTO t1 VALUES(randstr(5000,6000)); sl@0: INSERT INTO t1 VALUES(randstr(5000,6000)); sl@0: INSERT INTO t1 VALUES(randstr(5000,6000)); sl@0: INSERT INTO t1 VALUES(randstr(5000,6000)); sl@0: SELECT count(*) FROM t1; sl@0: } sl@0: } 5 sl@0: do_test memdb-8.2 { sl@0: execsql { sl@0: DELETE FROM t1; sl@0: SELECT count(*) FROM t1; sl@0: } sl@0: } 0 sl@0: sl@0: sl@0: } ;# ifcapable memorydb sl@0: sl@0: finish_test