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 the sessions module.
13 # Specifically, it tests that UNIQUE constraints are dealt with correctly.
18 if {![info exists testdir]} {
19 set testdir [file join [file dirname [info script]] .. .. test]
21 source [file join [file dirname [info script]] session_common.tcl]
22 source $testdir/tester.tcl
23 ifcapable !session {finish_test; return}
24 set testprefix sessionG
32 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
33 INSERT INTO t1 VALUES(1, 'one');
34 INSERT INTO t1 VALUES(2, 'two');
35 INSERT INTO t1 VALUES(3, 'three');
37 do_then_apply_sql -ignorenoop {
38 DELETE FROM t1 WHERE a=1;
39 INSERT INTO t1 VALUES(4, 'one');
45 do_then_apply_sql -ignorenoop {
46 DELETE FROM t1 WHERE a=4;
47 INSERT INTO t1 VALUES(1, 'one');
53 execsql { INSERT INTO t1 VALUES(5, 'five') } db2
54 do_then_apply_sql -ignorenoop {
55 INSERT INTO t1 VALUES(11, 'eleven');
56 INSERT INTO t1 VALUES(12, 'five');
58 execsql { SELECT * FROM t1 } db2
59 } {2 two 3 three 1 one 5 five 11 eleven}
62 execsql { SELECT * FROM t1 }
63 } {2 two 3 three 1 one 11 eleven 12 five}
65 #-------------------------------------------------------------------------
74 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE, c UNIQUE);
75 INSERT INTO t1 VALUES(1, 1, 1);
76 INSERT INTO t1 VALUES(2, 2, 2);
77 INSERT INTO t1 VALUES(3, 3, 3);
82 # It is not possible to apply the changeset generated by the following
83 # SQL, as none of the three updated rows may be updated as part of the
85 do_then_apply_sql -ignorenoop {
86 UPDATE t1 SET b=0 WHERE a=1;
87 UPDATE t1 SET b=1 WHERE a=2;
88 UPDATE t1 SET b=2 WHERE a=3;
89 UPDATE t1 SET b=3 WHERE a=1;
91 db2 eval { SELECT a, b FROM t1 }
93 do_test 2.2.2 { db eval { SELECT a, b FROM t1 } } {1 3 2 1 3 2}
95 #-------------------------------------------------------------------------
104 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE, c UNIQUE);
105 INSERT INTO t1 VALUES(1, 1, 1);
106 INSERT INTO t1 VALUES(2, 2, 2);
107 INSERT INTO t1 VALUES(3, 3, 3);
112 do_then_apply_sql -ignorenoop {
113 UPDATE t1 SET b=4 WHERE a=3;
114 UPDATE t1 SET b=3 WHERE a=2;
115 UPDATE t1 SET b=2 WHERE a=1;
121 do_then_apply_sql -ignorenoop {
122 UPDATE t1 SET b=1 WHERE a=1;
123 UPDATE t1 SET b=2 WHERE a=2;
124 UPDATE t1 SET b=3 WHERE a=3;
129 #-------------------------------------------------------------------------
138 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
139 INSERT INTO t1 VALUES(1, 1);
140 INSERT INTO t1 VALUES(2, 2);
141 INSERT INTO t1 VALUES(3, 3);
143 CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
144 INSERT INTO t2 VALUES(1, 1);
145 INSERT INTO t2 VALUES(2, 2);
146 INSERT INTO t2 VALUES(3, 3);
151 do_then_apply_sql -ignorenoop {
152 UPDATE t1 SET b=4 WHERE a=3;
153 UPDATE t1 SET b=3 WHERE a=2;
154 UPDATE t1 SET b=2 WHERE a=1;
156 UPDATE t2 SET b=0 WHERE a=1;
157 UPDATE t2 SET b=1 WHERE a=2;
158 UPDATE t2 SET b=2 WHERE a=3;
164 do_then_apply_sql -ignorenoop {
165 UPDATE t1 SET b=1 WHERE a=1;
166 UPDATE t1 SET b=2 WHERE a=2;
167 UPDATE t1 SET b=3 WHERE a=3;
169 UPDATE t2 SET b=3 WHERE a=3;
170 UPDATE t2 SET b=2 WHERE a=2;
171 UPDATE t2 SET b=1 WHERE a=1;
176 #-------------------------------------------------------------------------
182 do_execsql_test 5.0.1 {
183 CREATE TABLE t1(a PRIMARY KEY, b, c);
184 CREATE TABLE t2(a, b, c PRIMARY KEY);
185 CREATE TABLE t3(a, b PRIMARY KEY, c);
187 do_execsql_test -db db2 5.0.2 {
188 CREATE TABLE t1(a PRIMARY KEY, b, c);
189 CREATE TABLE t2(a, b, c);
190 CREATE TABLE t3(a, b PRIMARY KEY, c);
194 do_then_apply_sql -ignorenoop {
195 INSERT INTO t1 VALUES(1, 2, 3);
196 INSERT INTO t2 VALUES(4, 5, 6);
197 INSERT INTO t3 VALUES(7, 8, 9);
207 #-------------------------------------------------------------------------
210 db func number_name number_name
211 do_execsql_test 6.0 {
212 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
213 CREATE UNIQUE INDEX t1b ON t1(b);
217 SELECT i+1 FROM s WHERE i<1000
219 INSERT INTO t1 SELECT i, number_name(i) FROM s;
224 set ::C [changeset_from_sql {
229 SELECT i+1 FROM s WHERE i<1000
231 INSERT INTO t1 SELECT i, number_name(i+1) FROM s;
234 execsql { SELECT count(*) FROM t1 WHERE number_name(a) IS NOT b }
237 proc xConflict {args} { exit ; return "OMIT" }
239 sqlite3changeset_apply db $C xConflict
242 do_execsql_test 6.3 { SELECT count(*) FROM t1; } {1000}
243 do_execsql_test 6.4 {
244 SELECT count(*) FROM t1 WHERE number_name(a+1) IS NOT b;
247 # db eval { SELECT * FROM t1 } { puts "$a || $b" }