2 package require sqlite3
4 proc do_test {name cmd expected} {
5 puts -nonewline "$name ..."
7 if {$res eq $expected} {
12 puts " Expected: $expected"
18 uplevel [list db eval $sql]
22 set rc [catch {uplevel [list db eval $sql]} msg]
26 file delete -force test.db test.db.journal
29 # The following tests - genfkey-1.* - test RESTRICT foreign keys.
33 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, UNIQUE(b, c));
34 CREATE TABLE t2(e REFERENCES t1, f);
35 CREATE TABLE t3(g, h, i, FOREIGN KEY (h, i) REFERENCES t1(b, c));
39 execsql [exec ./sqlite3 test.db .genfkey]
42 catchsql { INSERT INTO t2 VALUES(1, 2) }
43 } {1 {constraint failed}}
46 INSERT INTO t1 VALUES(1, 2, 3);
47 INSERT INTO t2 VALUES(1, 2);
51 execsql { INSERT INTO t2 VALUES(NULL, 3) }
54 catchsql { UPDATE t2 SET e = 5 WHERE e IS NULL }
55 } {1 {constraint failed}}
57 execsql { UPDATE t2 SET e = 1 WHERE e IS NULL }
60 execsql { UPDATE t2 SET e = NULL WHERE f = 3 }
63 catchsql { UPDATE t1 SET a = 10 }
64 } {1 {constraint failed}}
65 do_test genfkey-1.9a {
66 catchsql { UPDATE t1 SET a = NULL }
67 } {1 {datatype mismatch}}
68 do_test genfkey-1.10 {
69 catchsql { DELETE FROM t1 }
70 } {1 {constraint failed}}
71 do_test genfkey-1.11 {
72 execsql { UPDATE t2 SET e = NULL }
74 do_test genfkey-1.12 {
76 UPDATE t1 SET a = 10 ;
82 do_test genfkey-1.13 {
84 INSERT INTO t3 VALUES(1, NULL, NULL);
85 INSERT INTO t3 VALUES(1, 2, NULL);
86 INSERT INTO t3 VALUES(1, NULL, 3);
89 do_test genfkey-1.14 {
90 catchsql { INSERT INTO t3 VALUES(3, 1, 4) }
91 } {1 {constraint failed}}
92 do_test genfkey-1.15 {
94 INSERT INTO t1 VALUES(1, 1, 4);
95 INSERT INTO t3 VALUES(3, 1, 4);
98 do_test genfkey-1.16 {
99 catchsql { DELETE FROM t1 }
100 } {1 {constraint failed}}
101 do_test genfkey-1.17 {
102 catchsql { UPDATE t1 SET b = 10}
103 } {1 {constraint failed}}
104 do_test genfkey-1.18 {
105 execsql { UPDATE t1 SET a = 10}
107 do_test genfkey-1.19 {
108 catchsql { UPDATE t3 SET h = 'hello' WHERE i = 3}
109 } {1 {constraint failed}}
111 do_test genfkey-1.X {
119 # The following tests - genfkey-2.* - test CASCADE foreign keys.
121 do_test genfkey-2.1 {
123 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, UNIQUE(b, c));
124 CREATE TABLE t2(e REFERENCES t1 ON UPDATE CASCADE ON DELETE CASCADE, f);
125 CREATE TABLE t3(g, h, i,
127 REFERENCES t1(b, c) ON UPDATE CASCADE ON DELETE CASCADE
131 do_test genfkey-2.2 {
132 execsql [exec ./sqlite3 test.db .genfkey]
134 do_test genfkey-2.3 {
136 INSERT INTO t1 VALUES(1, 2, 3);
137 INSERT INTO t1 VALUES(4, 5, 6);
138 INSERT INTO t2 VALUES(1, 'one');
139 INSERT INTO t2 VALUES(4, 'four');
142 do_test genfkey-2.4 {
144 UPDATE t1 SET a = 2 WHERE a = 1;
148 do_test genfkey-2.5 {
150 DELETE FROM t1 WHERE a = 4;
154 do_test genfkey-2.6 {
156 INSERT INTO t3 VALUES('hello', 2, 3);
161 do_test genfkey-2.7 {
167 do_test genfkey-2.X {
176 # The following tests - genfkey-3.* - test SET NULL foreign keys.
178 do_test genfkey-3.1 {
180 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, UNIQUE(c, b));
181 CREATE TABLE t2(e REFERENCES t1 ON UPDATE SET NULL ON DELETE SET NULL, f);
182 CREATE TABLE t3(g, h, i,
184 REFERENCES t1(b, c) ON UPDATE SET NULL ON DELETE SET NULL
188 do_test genfkey-3.2 {
189 execsql [exec ./sqlite3 test.db .genfkey]
191 do_test genfkey-3.3 {
193 INSERT INTO t1 VALUES(1, 2, 3);
194 INSERT INTO t1 VALUES(4, 5, 6);
195 INSERT INTO t2 VALUES(1, 'one');
196 INSERT INTO t2 VALUES(4, 'four');
199 do_test genfkey-3.4 {
201 UPDATE t1 SET a = 2 WHERE a = 1;
205 do_test genfkey-3.5 {
207 DELETE FROM t1 WHERE a = 4;
211 do_test genfkey-3.6 {
213 INSERT INTO t3 VALUES('hello', 2, 3);
218 do_test genfkey-2.7 {
220 UPDATE t3 SET h = 2, i = 2;
225 do_test genfkey-3.X {
233 # The following tests - genfkey-4.* - test that errors in the schema
234 # are detected correctly.
236 do_test genfkey-4.1 {
238 CREATE TABLE t1(a REFERENCES nosuchtable, b);
239 CREATE TABLE t2(a REFERENCES t1, b);
241 CREATE TABLE t3(a, b, c, PRIMARY KEY(a, b));
242 CREATE TABLE t4(a, b, c, FOREIGN KEY(c, b) REFERENCES t3);
244 CREATE TABLE t5(a REFERENCES t4(d), b, c);
245 CREATE TABLE t6(a REFERENCES t4(a), b, c);
246 CREATE TABLE t7(a REFERENCES t3(a), b, c);
247 CREATE TABLE t8(a REFERENCES nosuchtable(a), b, c);
251 do_test genfkey-4.X {
252 set rc [catch {exec ./sqlite3 test.db .genfkey} msg]
255 Error in table t5: foreign key columns do not exist
256 Error in table t8: foreign key columns do not exist
257 Error in table t4: implicit mapping to composite primary key
258 Error in table t1: implicit mapping to non-existant primary key
259 Error in table t2: implicit mapping to non-existant primary key
260 Error in table t6: foreign key is not unique
261 Error in table t7: foreign key is not unique
264 # Test that ticket #3800 has been resolved.
266 do_test genfkey-5.1 {
268 DROP TABLE t1; DROP TABLE t2; DROP TABLE t3;
269 DROP TABLE t4; DROP TABLE t5; DROP TABLE t6;
270 DROP TABLE t7; DROP TABLE t8;
273 do_test genfkey-5.2 {
275 CREATE TABLE "t.3" (c1 PRIMARY KEY);
276 CREATE TABLE t13 (c1, foreign key(c1) references "t.3"(c1));
279 do_test genfkey-5.3 {
280 set rc [catch {exec ./sqlite3 test.db .genfkey} msg]
282 do_test genfkey-5.4 {
285 do_test genfkey-5.5 {
286 catchsql { INSERT INTO t13 VALUES(1) }
287 } {1 {constraint failed}}
288 do_test genfkey-5.5 {
290 INSERT INTO "t.3" VALUES(1);
291 INSERT INTO t13 VALUES(1);
295 # Test also column names that require quoting.
296 do_test genfkey-6.1 {
301 "a.1 first", "b.2 second",
302 UNIQUE("a.1 first", "b.2 second")
306 FOREIGN KEY("c.1 I", "d.2 II")
307 REFERENCES p("a.1 first", "b.2 second")
308 ON UPDATE CASCADE ON DELETE CASCADE
312 do_test genfkey-6.2 {
313 set rc [catch {exec ./sqlite3 test.db .genfkey} msg]
315 do_test genfkey-6.3 {
318 INSERT INTO p VALUES('A', 'B');
319 INSERT INTO p VALUES('C', 'D');
320 INSERT INTO c VALUES('A', 'B');
321 INSERT INTO c VALUES('C', 'D');
322 UPDATE p SET "a.1 first" = 'X' WHERE rowid = 1;
323 DELETE FROM p WHERE rowid = 2;
325 execsql { SELECT * FROM c }
328 do_test genfkey-6.4 {
332 CREATE TABLE parent("a.1", PRIMARY KEY("a.1"));
333 CREATE TABLE child("b.2", FOREIGN KEY("b.2") REFERENCES parent("a.1"));
335 set rc [catch {exec ./sqlite3 test.db .genfkey} msg]
337 do_test genfkey-6.5 {
340 INSERT INTO parent VALUES(1);
341 INSERT INTO child VALUES(1);
343 catchsql { UPDATE parent SET "a.1"=0 }
344 } {1 {constraint failed}}
345 do_test genfkey-6.6 {
346 catchsql { UPDATE child SET "b.2"=7 }
347 } {1 {constraint failed}}
348 do_test genfkey-6.7 {
350 SELECT * FROM parent;