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