OLD | NEW |
1 # 2009 September 15 | 1 # 2009 September 15 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
11 # This file implements regression tests for SQLite library. | 11 # This file implements regression tests for SQLite library. |
12 # | 12 # |
13 # This file implements tests for foreign keys. | 13 # This file implements tests for foreign keys. |
14 # | 14 # |
15 | 15 |
16 set testdir [file dirname $argv0] | 16 set testdir [file dirname $argv0] |
17 source $testdir/tester.tcl | 17 source $testdir/tester.tcl |
18 | 18 |
19 ifcapable {!foreignkey||!trigger} { | 19 ifcapable {!foreignkey||!trigger} { |
20 finish_test | 20 finish_test |
21 return | 21 return |
22 } | 22 } |
23 | 23 |
| 24 set testprefix fkey3 |
| 25 |
24 # Create a table and some data to work with. | 26 # Create a table and some data to work with. |
25 # | 27 # |
26 do_test fkey3-1.1 { | 28 do_test fkey3-1.1 { |
27 execsql { | 29 execsql { |
28 PRAGMA foreign_keys=ON; | 30 PRAGMA foreign_keys=ON; |
29 CREATE TABLE t1(x INTEGER PRIMARY KEY); | 31 CREATE TABLE t1(x INTEGER PRIMARY KEY); |
30 INSERT INTO t1 VALUES(100); | 32 INSERT INTO t1 VALUES(100); |
31 INSERT INTO t1 VALUES(101); | 33 INSERT INTO t1 VALUES(101); |
32 CREATE TABLE t2(y INTEGER REFERENCES t1 (x)); | 34 CREATE TABLE t2(y INTEGER REFERENCES t1 (x)); |
33 INSERT INTO t2 VALUES(100); | 35 INSERT INTO t2 VALUES(100); |
34 INSERT INTO t2 VALUES(101); | 36 INSERT INTO t2 VALUES(101); |
35 SELECT 1, x FROM t1; | 37 SELECT 1, x FROM t1; |
36 SELECT 2, y FROM t2; | 38 SELECT 2, y FROM t2; |
37 } | 39 } |
38 } {1 100 1 101 2 100 2 101} | 40 } {1 100 1 101 2 100 2 101} |
39 | 41 |
40 do_test fkey3-1.2 { | 42 do_test fkey3-1.2 { |
41 catchsql { | 43 catchsql { |
42 DELETE FROM t1 WHERE x=100; | 44 DELETE FROM t1 WHERE x=100; |
43 } | 45 } |
44 } {1 {foreign key constraint failed}} | 46 } {1 {FOREIGN KEY constraint failed}} |
45 | 47 |
46 do_test fkey3-1.3 { | 48 do_test fkey3-1.3 { |
47 catchsql { | 49 catchsql { |
48 DROP TABLE t1; | 50 DROP TABLE t1; |
49 } | 51 } |
50 } {1 {foreign key constraint failed}} | 52 } {1 {FOREIGN KEY constraint failed}} |
51 | 53 |
52 do_test fkey3-1.4 { | 54 do_test fkey3-1.4 { |
53 execsql { | 55 execsql { |
54 DROP TABLE t2; | 56 DROP TABLE t2; |
55 } | 57 } |
56 } {} | 58 } {} |
57 | 59 |
58 do_test fkey3-1.5 { | 60 do_test fkey3-1.5 { |
59 execsql { | 61 execsql { |
60 DROP TABLE t1; | 62 DROP TABLE t1; |
61 } | 63 } |
62 } {} | 64 } {} |
63 | 65 |
64 do_test fkey3-2.1 { | 66 do_test fkey3-2.1 { |
65 execsql { | 67 execsql { |
66 PRAGMA foreign_keys=ON; | 68 PRAGMA foreign_keys=ON; |
67 CREATE TABLE t1(x INTEGER PRIMARY KEY); | 69 CREATE TABLE t1(x INTEGER PRIMARY KEY); |
68 INSERT INTO t1 VALUES(100); | 70 INSERT INTO t1 VALUES(100); |
69 INSERT INTO t1 VALUES(101); | 71 INSERT INTO t1 VALUES(101); |
70 CREATE TABLE t2(y INTEGER PRIMARY KEY REFERENCES t1 (x) ON UPDATE SET NULL); | 72 CREATE TABLE t2(y INTEGER PRIMARY KEY REFERENCES t1 (x) ON UPDATE SET NULL); |
71 } | 73 } |
72 execsql { | 74 execsql { |
73 INSERT INTO t2 VALUES(100); | 75 INSERT INTO t2 VALUES(100); |
74 INSERT INTO t2 VALUES(101); | 76 INSERT INTO t2 VALUES(101); |
75 SELECT 1, x FROM t1; | 77 SELECT 1, x FROM t1; |
76 SELECT 2, y FROM t2; | 78 SELECT 2, y FROM t2; |
77 } | 79 } |
78 } {1 100 1 101 2 100 2 101} | 80 } {1 100 1 101 2 100 2 101} |
79 | 81 |
| 82 |
| 83 #------------------------------------------------------------------------- |
| 84 # The following tests - fkey-3.* - test some edge cases to do with |
| 85 # inserting rows into tables that have foreign keys where the parent |
| 86 # table is the same as the child table. Especially cases where the |
| 87 # new row being inserted matches itself. |
| 88 # |
| 89 do_execsql_test 3.1.1 { |
| 90 CREATE TABLE t3(a, b, c, d, |
| 91 UNIQUE(a, b), |
| 92 FOREIGN KEY(c, d) REFERENCES t3(a, b) |
| 93 ); |
| 94 INSERT INTO t3 VALUES(1, 2, 1, 2); |
| 95 } {} |
| 96 do_catchsql_test 3.1.2 { |
| 97 INSERT INTO t3 VALUES(NULL, 2, 5, 2); |
| 98 } {1 {FOREIGN KEY constraint failed}} |
| 99 do_catchsql_test 3.1.3 { |
| 100 INSERT INTO t3 VALUES(NULL, 3, 5, 2); |
| 101 } {1 {FOREIGN KEY constraint failed}} |
| 102 |
| 103 do_execsql_test 3.2.1 { |
| 104 CREATE TABLE t4(a UNIQUE, b REFERENCES t4(a)); |
| 105 } |
| 106 do_catchsql_test 3.2.2 { |
| 107 INSERT INTO t4 VALUES(NULL, 1); |
| 108 } {1 {FOREIGN KEY constraint failed}} |
| 109 |
| 110 do_execsql_test 3.3.1 { |
| 111 CREATE TABLE t5(a INTEGER PRIMARY KEY, b REFERENCES t5(a)); |
| 112 INSERT INTO t5 VALUES(NULL, 1); |
| 113 } {} |
| 114 do_catchsql_test 3.3.2 { |
| 115 INSERT INTO t5 VALUES(NULL, 3); |
| 116 } {1 {FOREIGN KEY constraint failed}} |
| 117 |
| 118 do_execsql_test 3.4.1 { |
| 119 CREATE TABLE t6(a INTEGER PRIMARY KEY, b, c, d, |
| 120 FOREIGN KEY(c, d) REFERENCES t6(a, b) |
| 121 ); |
| 122 CREATE UNIQUE INDEX t6i ON t6(b, a); |
| 123 } |
| 124 do_execsql_test 3.4.2 { INSERT INTO t6 VALUES(NULL, 'a', 1, 'a'); } {} |
| 125 do_execsql_test 3.4.3 { INSERT INTO t6 VALUES(2, 'a', 2, 'a'); } {} |
| 126 do_execsql_test 3.4.4 { INSERT INTO t6 VALUES(NULL, 'a', 1, 'a'); } {} |
| 127 do_execsql_test 3.4.5 { INSERT INTO t6 VALUES(5, 'a', 2, 'a'); } {} |
| 128 do_catchsql_test 3.4.6 { |
| 129 INSERT INTO t6 VALUES(NULL, 'a', 65, 'a'); |
| 130 } {1 {FOREIGN KEY constraint failed}} |
| 131 |
| 132 do_execsql_test 3.4.7 { |
| 133 INSERT INTO t6 VALUES(100, 'one', 100, 'one'); |
| 134 DELETE FROM t6 WHERE a = 100; |
| 135 } |
| 136 do_execsql_test 3.4.8 { |
| 137 INSERT INTO t6 VALUES(100, 'one', 100, 'one'); |
| 138 UPDATE t6 SET c = 1, d = 'a' WHERE a = 100; |
| 139 DELETE FROM t6 WHERE a = 100; |
| 140 } |
| 141 |
| 142 do_execsql_test 3.5.1 { |
| 143 CREATE TABLE t7(a, b, c, d INTEGER PRIMARY KEY, |
| 144 FOREIGN KEY(c, d) REFERENCES t7(a, b) |
| 145 ); |
| 146 CREATE UNIQUE INDEX t7i ON t7(a, b); |
| 147 } |
| 148 do_execsql_test 3.5.2 { INSERT INTO t7 VALUES('x', 1, 'x', NULL) } {} |
| 149 do_execsql_test 3.5.3 { INSERT INTO t7 VALUES('x', 2, 'x', 2) } {} |
| 150 do_catchsql_test 3.5.4 { |
| 151 INSERT INTO t7 VALUES('x', 450, 'x', NULL); |
| 152 } {1 {FOREIGN KEY constraint failed}} |
| 153 do_catchsql_test 3.5.5 { |
| 154 INSERT INTO t7 VALUES('x', 450, 'x', 451); |
| 155 } {1 {FOREIGN KEY constraint failed}} |
| 156 |
| 157 |
| 158 do_execsql_test 3.6.1 { |
| 159 CREATE TABLE t8(a, b, c, d, e, FOREIGN KEY(c, d) REFERENCES t8(a, b)); |
| 160 CREATE UNIQUE INDEX t8i1 ON t8(a, b); |
| 161 CREATE UNIQUE INDEX t8i2 ON t8(c); |
| 162 INSERT INTO t8 VALUES(1, 1, 1, 1, 1); |
| 163 } |
| 164 do_catchsql_test 3.6.2 { |
| 165 UPDATE t8 SET d = 2; |
| 166 } {1 {FOREIGN KEY constraint failed}} |
| 167 do_execsql_test 3.6.3 { UPDATE t8 SET d = 1; } |
| 168 do_execsql_test 3.6.4 { UPDATE t8 SET e = 2; } |
| 169 |
| 170 do_catchsql_test 3.6.5 { |
| 171 CREATE TABLE TestTable ( |
| 172 id INTEGER PRIMARY KEY, |
| 173 name text, |
| 174 source_id integer not null, |
| 175 parent_id integer, |
| 176 |
| 177 foreign key(source_id, parent_id) references TestTable(source_id, id) |
| 178 ); |
| 179 CREATE UNIQUE INDEX testindex on TestTable(source_id, id); |
| 180 PRAGMA foreign_keys=1; |
| 181 INSERT INTO TestTable VALUES (1, 'parent', 1, null); |
| 182 INSERT INTO TestTable VALUES (2, 'child', 1, 1); |
| 183 UPDATE TestTable SET parent_id=1000 where id=2; |
| 184 } {1 {FOREIGN KEY constraint failed}} |
| 185 |
80 finish_test | 186 finish_test |
OLD | NEW |