xref: /sqlite-3.40.0/test/fkey_malloc.test (revision 1b4b334a)
129c7f9caSdan# 2009 September 22
229c7f9caSdan#
329c7f9caSdan# The author disclaims copyright to this source code.  In place of
429c7f9caSdan# a legal notice, here is a blessing:
529c7f9caSdan#
629c7f9caSdan#    May you do good and not evil.
729c7f9caSdan#    May you find forgiveness for yourself and forgive others.
829c7f9caSdan#    May you share freely, never taking more than you give.
929c7f9caSdan#
1029c7f9caSdan#***********************************************************************
1129c7f9caSdan#
1229c7f9caSdan#
1329c7f9caSdan
1429c7f9caSdanset testdir [file dirname $argv0]
1529c7f9caSdansource $testdir/tester.tcl
1629c7f9caSdan
1729c7f9caSdanifcapable !foreignkey||!trigger {
1829c7f9caSdan  finish_test
1929c7f9caSdan  return
2029c7f9caSdan}
2129c7f9caSdansource $testdir/malloc_common.tcl
2229c7f9caSdan
2329c7f9caSdando_malloc_test fkey_malloc-1 -sqlprep {
2429c7f9caSdan  PRAGMA foreign_keys = 1;
25a8f0bf64Sdan  CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
2629c7f9caSdan  CREATE TABLE t2(x REFERENCES t1 ON UPDATE CASCADE ON DELETE CASCADE);
2729c7f9caSdan} -sqlbody {
2829c7f9caSdan  INSERT INTO t1 VALUES('aaa', 1);
2929c7f9caSdan  INSERT INTO t2 VALUES('aaa');
3029c7f9caSdan  UPDATE t1 SET a = 'bbb';
3129c7f9caSdan  DELETE FROM t1;
324b4b473aSdrh  PRAGMA foreign_key_check;
3329c7f9caSdan}
3429c7f9caSdan
35f59c5cacSdando_malloc_test fkey_malloc-2 -sqlprep {
36f59c5cacSdan  PRAGMA foreign_keys = 1;
37f59c5cacSdan  CREATE TABLE t1(a, b, UNIQUE(a, b));
38f59c5cacSdan} -sqlbody {
39f59c5cacSdan  CREATE TABLE t2(x, y,
40f59c5cacSdan    FOREIGN KEY(x, y) REFERENCES t1(a, b) DEFERRABLE INITIALLY DEFERRED
41f59c5cacSdan  );
42f59c5cacSdan  BEGIN;
43f59c5cacSdan    INSERT INTO t2 VALUES('a', 'b');
44f59c5cacSdan    INSERT INTO t1 VALUES('a', 'b');
45f59c5cacSdan    UPDATE t1 SET a = 'c';
46f59c5cacSdan    DELETE FROM t2;
47f59c5cacSdan    INSERT INTO t2 VALUES('d', 'b');
48f59c5cacSdan    UPDATE t2 SET x = 'c';
49f59c5cacSdan  COMMIT;
50f59c5cacSdan}
51f59c5cacSdan
52a8f0bf64Sdando_malloc_test fkey_malloc-3 -sqlprep {
53a8f0bf64Sdan  PRAGMA foreign_keys = 1;
54a8f0bf64Sdan  CREATE TABLE t1(x INTEGER PRIMARY KEY);
55475f5719Sdan  CREATE TABLE t2(y DEFAULT 14 REFERENCES t1(x) ON UPDATE SET DEFAULT);
56475f5719Sdan  CREATE TABLE t3(y REFERENCES t1 ON UPDATE SET NULL);
57a8f0bf64Sdan  INSERT INTO t1 VALUES(13);
58a8f0bf64Sdan  INSERT INTO t2 VALUES(13);
59a8f0bf64Sdan  INSERT INTO t3 VALUES(13);
60a8f0bf64Sdan} -sqlbody {
61a8f0bf64Sdan  UPDATE t1 SET x = 14;
62a8f0bf64Sdan}
63a8f0bf64Sdan
64a8f0bf64Sdanproc catch_fk_error {zSql} {
65a8f0bf64Sdan  set rc [catch {db eval $zSql} msg]
66a8f0bf64Sdan  if {$rc==0} {
67a8f0bf64Sdan    return $msg
68a8f0bf64Sdan  }
69a8f0bf64Sdan  if {[string match {*foreign key*} $msg]} {
70a8f0bf64Sdan    return ""
71a8f0bf64Sdan  }
72*1b4b334aSdan  if {$msg eq "out of memory"
73*1b4b334aSdan   || $msg eq "FOREIGN KEY constraint failed"
74*1b4b334aSdan   || $msg eq "constraint failed"
75*1b4b334aSdan  } {
76a8f0bf64Sdan    error 1
77a8f0bf64Sdan  }
78a8f0bf64Sdan  error $msg
79a8f0bf64Sdan}
80a8f0bf64Sdan
81a8f0bf64Sdando_malloc_test fkey_malloc-4 -sqlprep {
82a8f0bf64Sdan  PRAGMA foreign_keys = 1;
83a8f0bf64Sdan  CREATE TABLE t1(x INTEGER PRIMARY KEY, y UNIQUE);
84a8f0bf64Sdan  CREATE TABLE t2(z REFERENCES t1(x), a REFERENCES t1(y));
85a8f0bf64Sdan  CREATE TABLE t3(x);
86a8f0bf64Sdan  CREATE TABLE t4(z REFERENCES t3);
87a8f0bf64Sdan  CREATE TABLE t5(x, y);
88a8f0bf64Sdan  CREATE TABLE t6(z REFERENCES t5(x));
89a8f0bf64Sdan  CREATE INDEX i51 ON t5(x);
90a8f0bf64Sdan  CREATE INDEX i52 ON t5(y, x);
91a8f0bf64Sdan  INSERT INTO t1 VALUES(1, 2);
92a8f0bf64Sdan} -tclbody {
93a8f0bf64Sdan  catch_fk_error {INSERT INTO t2 VALUES(1, 3)}
94a8f0bf64Sdan  catch_fk_error {INSERT INTO t4 VALUES(2)}
95a8f0bf64Sdan  catch_fk_error {INSERT INTO t6 VALUES(2)}
96a8f0bf64Sdan}
97a8f0bf64Sdan
98a8f0bf64Sdando_malloc_test fkey_malloc-5 -sqlprep {
99a8f0bf64Sdan  PRAGMA foreign_keys = 1;
100a8f0bf64Sdan  CREATE TABLE t1(x, y, PRIMARY KEY(x, y));
101a8f0bf64Sdan  CREATE TABLE t2(a, b, FOREIGN KEY(a, b) REFERENCES t1 ON UPDATE CASCADE);
102a8f0bf64Sdan  INSERT INTO t1 VALUES(1, 2);
103a8f0bf64Sdan  INSERT INTO t2 VALUES(1, 2);
104a8f0bf64Sdan} -sqlbody {
105a8f0bf64Sdan  UPDATE t1 SET x = 5;
106a8f0bf64Sdan}
107a8f0bf64Sdan
108f7a94543Sdando_malloc_test fkey_malloc-6 -sqlprep {
109f7a94543Sdan  PRAGMA foreign_keys = 1;
110f7a94543Sdan  CREATE TABLE t1(
111f7a94543Sdan    x PRIMARY KEY,
112f7a94543Sdan    y REFERENCES t1 ON DELETE RESTRICT ON UPDATE SET DEFAULT
113f7a94543Sdan  );
114f7a94543Sdan  INSERT INTO t1 VALUES('abc', 'abc');
115f7a94543Sdan  INSERT INTO t1 VALUES('def', 'def');
116f7a94543Sdan} -sqlbody {
117f7a94543Sdan  INSERT INTO t1 VALUES('ghi', 'ghi');
118f7a94543Sdan  DELETE FROM t1 WHERE rowid>1;
119f7a94543Sdan  UPDATE t1 SET x='jkl', y='jkl';
120f7a94543Sdan}
121f7a94543Sdan
122f7a94543Sdando_malloc_test fkey_malloc-7 -sqlprep {
123f7a94543Sdan  PRAGMA foreign_keys = 1;
124f7a94543Sdan  CREATE TABLE x(a, b, PRIMARY KEY(a, b));
125f7a94543Sdan  CREATE TABLE y(c, d,
126f7a94543Sdan    FOREIGN KEY(d, c) REFERENCES x DEFERRABLE INITIALLY DEFERRED
127f7a94543Sdan  );
128f7a94543Sdan  CREATE TABLE z(e, f, FOREIGN KEY(e, f) REFERENCES x);
129f7a94543Sdan} -sqlbody {
130f7a94543Sdan  DROP TABLE y;
131f7a94543Sdan  DROP TABLE x;
132f7a94543Sdan}
133f7a94543Sdan
13429c7f9caSdanfinish_test
135