xref: /sqlite-3.40.0/test/fkey_malloc.test (revision a8f0bf64)
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;
25*a8f0bf64Sdan  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;
3229c7f9caSdan}
3329c7f9caSdan
34f59c5cacSdando_malloc_test fkey_malloc-2 -sqlprep {
35f59c5cacSdan  PRAGMA foreign_keys = 1;
36f59c5cacSdan  CREATE TABLE t1(a, b, UNIQUE(a, b));
37f59c5cacSdan} -sqlbody {
38f59c5cacSdan  CREATE TABLE t2(x, y,
39f59c5cacSdan    FOREIGN KEY(x, y) REFERENCES t1(a, b) DEFERRABLE INITIALLY DEFERRED
40f59c5cacSdan  );
41f59c5cacSdan  BEGIN;
42f59c5cacSdan    INSERT INTO t2 VALUES('a', 'b');
43f59c5cacSdan    INSERT INTO t1 VALUES('a', 'b');
44f59c5cacSdan    UPDATE t1 SET a = 'c';
45f59c5cacSdan    DELETE FROM t2;
46f59c5cacSdan    INSERT INTO t2 VALUES('d', 'b');
47f59c5cacSdan    UPDATE t2 SET x = 'c';
48f59c5cacSdan  COMMIT;
49f59c5cacSdan}
50f59c5cacSdan
51*a8f0bf64Sdando_malloc_test fkey_malloc-3 -sqlprep {
52*a8f0bf64Sdan  PRAGMA foreign_keys = 1;
53*a8f0bf64Sdan  CREATE TABLE t1(x INTEGER PRIMARY KEY);
54*a8f0bf64Sdan  CREATE TABLE t2(y REFERENCES t1(rowid) ON UPDATE CASCADE);
55*a8f0bf64Sdan  CREATE TABLE t3(y DEFAULT 14 REFERENCES t1(x) ON UPDATE SET DEFAULT);
56*a8f0bf64Sdan  CREATE TABLE t4(y REFERENCES t1 ON UPDATE SET NULL);
57*a8f0bf64Sdan  INSERT INTO t1 VALUES(13);
58*a8f0bf64Sdan  INSERT INTO t2 VALUES(13);
59*a8f0bf64Sdan  INSERT INTO t3 VALUES(13);
60*a8f0bf64Sdan  INSERT INTO t4 VALUES(13);
61*a8f0bf64Sdan} -sqlbody {
62*a8f0bf64Sdan  UPDATE t1 SET x = 14;
63*a8f0bf64Sdan}
64*a8f0bf64Sdan
65*a8f0bf64Sdanproc catch_fk_error {zSql} {
66*a8f0bf64Sdan  set rc [catch {db eval $zSql} msg]
67*a8f0bf64Sdan  if {$rc==0} {
68*a8f0bf64Sdan    return $msg
69*a8f0bf64Sdan  }
70*a8f0bf64Sdan  if {[string match {*foreign key*} $msg]} {
71*a8f0bf64Sdan    return ""
72*a8f0bf64Sdan  }
73*a8f0bf64Sdan  if {$msg eq "out of memory"} {
74*a8f0bf64Sdan    error 1
75*a8f0bf64Sdan  }
76*a8f0bf64Sdan  error $msg
77*a8f0bf64Sdan}
78*a8f0bf64Sdan
79*a8f0bf64Sdando_malloc_test fkey_malloc-4 -sqlprep {
80*a8f0bf64Sdan  PRAGMA foreign_keys = 1;
81*a8f0bf64Sdan  CREATE TABLE t1(x INTEGER PRIMARY KEY, y UNIQUE);
82*a8f0bf64Sdan  CREATE TABLE t2(z REFERENCES t1(x), a REFERENCES t1(y));
83*a8f0bf64Sdan  CREATE TABLE t3(x);
84*a8f0bf64Sdan  CREATE TABLE t4(z REFERENCES t3);
85*a8f0bf64Sdan  CREATE TABLE t5(x, y);
86*a8f0bf64Sdan  CREATE TABLE t6(z REFERENCES t5(x));
87*a8f0bf64Sdan  CREATE INDEX i51 ON t5(x);
88*a8f0bf64Sdan  CREATE INDEX i52 ON t5(y, x);
89*a8f0bf64Sdan  INSERT INTO t1 VALUES(1, 2);
90*a8f0bf64Sdan} -tclbody {
91*a8f0bf64Sdan  catch_fk_error {INSERT INTO t2 VALUES(1, 3)}
92*a8f0bf64Sdan  catch_fk_error {INSERT INTO t4 VALUES(2)}
93*a8f0bf64Sdan  catch_fk_error {INSERT INTO t6 VALUES(2)}
94*a8f0bf64Sdan}
95*a8f0bf64Sdan
96*a8f0bf64Sdando_malloc_test fkey_malloc-5 -sqlprep {
97*a8f0bf64Sdan  PRAGMA foreign_keys = 1;
98*a8f0bf64Sdan  CREATE TABLE t1(x, y, PRIMARY KEY(x, y));
99*a8f0bf64Sdan  CREATE TABLE t2(a, b, FOREIGN KEY(a, b) REFERENCES t1 ON UPDATE CASCADE);
100*a8f0bf64Sdan  INSERT INTO t1 VALUES(1, 2);
101*a8f0bf64Sdan  INSERT INTO t2 VALUES(1, 2);
102*a8f0bf64Sdan} -sqlbody {
103*a8f0bf64Sdan  UPDATE t1 SET x = 5;
104*a8f0bf64Sdan}
105*a8f0bf64Sdan
10629c7f9caSdanfinish_test
10729c7f9caSdan
10829c7f9caSdan
109