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 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this script is in-memory database backend.
14 # $Id: memdb.test,v 1.13 2005/01/21 04:25:47 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
22 # In the following sequence of tests, compute the MD5 sum of the content
23 # of a table, make lots of modifications to that table, then do a rollback.
24 # Verify that after the rollback, the MD5 checksum is unchanged.
26 # These tests were browed from trans.tcl.
34 CREATE TABLE t3(x TEXT);
35 INSERT INTO t3 VALUES(randstr(10,400));
36 INSERT INTO t3 VALUES(randstr(10,400));
37 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
38 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
39 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
40 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
41 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
42 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
43 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
44 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
45 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
47 SELECT count(*) FROM t3;
51 # The following procedure computes a "signature" for table "t3". If
52 # T3 changes in any way, the signature should change.
54 # This is used to test ROLLBACK. We gather a signature for t3, then
55 # make lots of changes to t3, then rollback and take another signature.
56 # The two signatures should be the same.
58 proc signature {{fn {}}} {
59 set rx [db eval {SELECT x FROM t3}]
66 # set r [db eval {SELECT count(*), md5sum(x) FROM t3}]
68 return [list [string length $rx] $rx]
71 # Do rollbacks. Make sure the signature does not change.
74 for {set i 2} {$i<=$limit} {incr i} {
75 set ::sig [signature one]
77 set cnt [lindex $::sig 0]
79 execsql {PRAGMA synchronous=FULL}
81 execsql {PRAGMA synchronous=NORMAL}
83 do_test memdb-1.$i.1-$cnt {
86 DELETE FROM t3 WHERE random()%10!=0;
87 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
88 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
91 set sig2 [signature two]
94 # if {$sig2!=$sig} exit
95 do_test memdb-1.$i.2-$cnt {
98 DELETE FROM t3 WHERE random()%10!=0;
99 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
100 DELETE FROM t3 WHERE random()%10!=0;
101 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
107 do_test memdb-1.$i.9-$cnt {
109 INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE random()%10==0;
113 set ::pager_old_format 0
116 integrity_check memdb-2.1
120 CREATE TABLE t4(a,b,c,d);
122 INSERT INTO t4 VALUES(1,2,3,4);
128 SELECT name FROM sqlite_master WHERE type='table';
134 SELECT name FROM sqlite_master WHERE type='table';
140 SELECT name FROM sqlite_master WHERE type='table';
144 # Create tables for the first group of tests.
148 CREATE TABLE t1(a, b, c, UNIQUE(a,b));
150 SELECT c FROM t1 ORDER BY c;
154 # Six columns of configuration data as follows:
156 # i The reference number of the test
157 # conf The conflict resolution algorithm on the BEGIN statement
158 # cmd An INSERT or REPLACE command to execute against table t1
159 # t0 True if there is an error from $cmd
160 # t1 Content of "c" column of t1 assuming no error in $cmd
161 # t2 Content of "x" column of t2
163 foreach {i conf cmd t0 t1 t2} {
165 2 {} {INSERT OR IGNORE} 0 3 1
166 3 {} {INSERT OR REPLACE} 0 4 1
168 5 {} {INSERT OR FAIL} 1 {} 1
169 6 {} {INSERT OR ABORT} 1 {} 1
170 7 {} {INSERT OR ROLLBACK} 1 {} {}
173 if {$conf!=""} {set conf "ON CONFLICT $conf"}
174 set r0 [catch {execsql [subst {
177 INSERT INTO t1 VALUES(1,2,3);
179 INSERT INTO t2 VALUES(1);
180 $cmd INTO t1 VALUES(1,2,4);
182 catch {execsql {COMMIT}}
183 if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]}
184 set r2 [execsql {SELECT x FROM t2}]
193 CREATE TABLE t2(a,b,c);
194 INSERT INTO t2 VALUES(1,2,1);
195 INSERT INTO t2 VALUES(2,3,2);
196 INSERT INTO t2 VALUES(3,4,1);
197 INSERT INTO t2 VALUES(4,5,4);
198 SELECT c FROM t2 ORDER BY b;
200 INSERT INTO t3 VALUES(1);
204 # Six columns of configuration data as follows:
206 # i The reference number of the test
207 # conf1 The conflict resolution algorithm on the UNIQUE constraint
208 # conf2 The conflict resolution algorithm on the BEGIN statement
209 # cmd An UPDATE command to execute against table t1
210 # t0 True if there is an error from $cmd
211 # t1 Content of "b" column of t1 assuming no error in $cmd
212 # t2 Content of "x" column of t3
214 foreach {i conf1 conf2 cmd t0 t1 t2} {
215 1 {} {} UPDATE 1 {6 7 8 9} 1
216 2 REPLACE {} UPDATE 0 {7 6 9} 1
217 3 IGNORE {} UPDATE 0 {6 7 3 9} 1
218 4 FAIL {} UPDATE 1 {6 7 3 4} 1
219 5 ABORT {} UPDATE 1 {1 2 3 4} 1
220 6 ROLLBACK {} UPDATE 1 {1 2 3 4} 0
221 7 REPLACE {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1
222 8 IGNORE {} {UPDATE OR REPLACE} 0 {7 6 9} 1
223 9 FAIL {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1
224 10 ABORT {} {UPDATE OR REPLACE} 0 {7 6 9} 1
225 11 ROLLBACK {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1
226 12 {} {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1
227 13 {} {} {UPDATE OR REPLACE} 0 {7 6 9} 1
228 14 {} {} {UPDATE OR FAIL} 1 {6 7 3 4} 1
229 15 {} {} {UPDATE OR ABORT} 1 {1 2 3 4} 1
230 16 {} {} {UPDATE OR ROLLBACK} 1 {1 2 3 4} 0
232 if {$t0} {set t1 {column a is not unique}}
234 if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"}
235 if {$conf2!=""} {set conf2 "ON CONFLICT $conf2"}
236 set r0 [catch {execsql [subst {
238 CREATE TABLE t1(a,b,c, UNIQUE(a) $conf1);
239 INSERT INTO t1 SELECT * FROM t2;
246 catch {execsql {COMMIT}}
247 if {!$r0} {set r1 [execsql {SELECT a FROM t1 ORDER BY b}]}
248 set r2 [execsql {SELECT x FROM t3}]
257 } {1 2 1 2 3 2 3 4 1 4 5 4}
262 SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1;
268 SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1;
275 } {1 2 1 2 3 2 3 4 1 4 5 4}
279 SELECT a FROM t2 UNION SELECT b FROM t2 ORDER BY 1;
282 } ;# ifcapable compound
285 CREATE INDEX i2 ON t2(c);
286 SELECT a FROM t2 ORDER BY c;
291 SELECT a FROM t2 ORDER BY c DESC;
297 CREATE TABLE t5(x,y);
298 INSERT INTO t5 VALUES(1,2);
304 SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1;
310 SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1;
315 CREATE TABLE t5(x PRIMARY KEY, y UNIQUE);
321 SELECT * FROM t5 ORDER BY y DESC;
326 INSERT INTO t5 VALUES(1,2);
327 INSERT INTO t5 VALUES(3,4);
328 REPLACE INTO t5 VALUES(1,4);
329 SELECT rowid,* FROM t5;
334 DELETE FROM t5 WHERE x>5;
340 DELETE FROM t5 WHERE y<3;
346 DELETE FROM t5 WHERE x>0;
355 INSERT INTO t6 VALUES(1);
356 INSERT INTO t6 SELECT x+1 FROM t6;
357 INSERT INTO t6 SELECT x+2 FROM t6;
358 INSERT INTO t6 SELECT x+4 FROM t6;
359 INSERT INTO t6 SELECT x+8 FROM t6;
360 INSERT INTO t6 SELECT x+16 FROM t6;
361 INSERT INTO t6 SELECT x+32 FROM t6;
362 INSERT INTO t6 SELECT x+64 FROM t6;
363 INSERT INTO t6 SELECT x+128 FROM t6;
364 SELECT count(*) FROM (SELECT DISTINCT x FROM t6);
367 for {set i 1} {$i<=256} {incr i} {
368 do_test memdb-7.2.$i {
369 execsql "DELETE FROM t6 WHERE x=\
370 (SELECT x FROM t6 ORDER BY random() LIMIT 1)"
371 execsql {SELECT count(*) FROM t6}
376 } ;# ifcapable memorydb