fkey_malloc.test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # 2009 September 22
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  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.
  9. #
  10. #***********************************************************************
  11. #
  12. #
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. ifcapable !foreignkey||!trigger {
  16. finish_test
  17. return
  18. }
  19. source $testdir/malloc_common.tcl
  20. do_malloc_test fkey_malloc-1 -sqlprep {
  21. PRAGMA foreign_keys = 1;
  22. CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
  23. CREATE TABLE t2(x REFERENCES t1 ON UPDATE CASCADE ON DELETE CASCADE);
  24. } -sqlbody {
  25. INSERT INTO t1 VALUES('aaa', 1);
  26. INSERT INTO t2 VALUES('aaa');
  27. UPDATE t1 SET a = 'bbb';
  28. DELETE FROM t1;
  29. PRAGMA foreign_key_check;
  30. }
  31. do_malloc_test fkey_malloc-2 -sqlprep {
  32. PRAGMA foreign_keys = 1;
  33. CREATE TABLE t1(a, b, UNIQUE(a, b));
  34. } -sqlbody {
  35. CREATE TABLE t2(x, y,
  36. FOREIGN KEY(x, y) REFERENCES t1(a, b) DEFERRABLE INITIALLY DEFERRED
  37. );
  38. BEGIN;
  39. INSERT INTO t2 VALUES('a', 'b');
  40. INSERT INTO t1 VALUES('a', 'b');
  41. UPDATE t1 SET a = 'c';
  42. DELETE FROM t2;
  43. INSERT INTO t2 VALUES('d', 'b');
  44. UPDATE t2 SET x = 'c';
  45. COMMIT;
  46. }
  47. do_malloc_test fkey_malloc-3 -sqlprep {
  48. PRAGMA foreign_keys = 1;
  49. CREATE TABLE t1(x INTEGER PRIMARY KEY);
  50. CREATE TABLE t2(y DEFAULT 14 REFERENCES t1(x) ON UPDATE SET DEFAULT);
  51. CREATE TABLE t3(y REFERENCES t1 ON UPDATE SET NULL);
  52. INSERT INTO t1 VALUES(13);
  53. INSERT INTO t2 VALUES(13);
  54. INSERT INTO t3 VALUES(13);
  55. } -sqlbody {
  56. UPDATE t1 SET x = 14;
  57. }
  58. proc catch_fk_error {zSql} {
  59. set rc [catch {db eval $zSql} msg]
  60. if {$rc==0} {
  61. return $msg
  62. }
  63. if {[string match {*foreign key*} $msg]} {
  64. return ""
  65. }
  66. if {$msg eq "out of memory" || $msg eq "constraint failed"} {
  67. error 1
  68. }
  69. error $msg
  70. }
  71. do_malloc_test fkey_malloc-4 -sqlprep {
  72. PRAGMA foreign_keys = 1;
  73. CREATE TABLE t1(x INTEGER PRIMARY KEY, y UNIQUE);
  74. CREATE TABLE t2(z REFERENCES t1(x), a REFERENCES t1(y));
  75. CREATE TABLE t3(x);
  76. CREATE TABLE t4(z REFERENCES t3);
  77. CREATE TABLE t5(x, y);
  78. CREATE TABLE t6(z REFERENCES t5(x));
  79. CREATE INDEX i51 ON t5(x);
  80. CREATE INDEX i52 ON t5(y, x);
  81. INSERT INTO t1 VALUES(1, 2);
  82. } -tclbody {
  83. catch_fk_error {INSERT INTO t2 VALUES(1, 3)}
  84. catch_fk_error {INSERT INTO t4 VALUES(2)}
  85. catch_fk_error {INSERT INTO t6 VALUES(2)}
  86. }
  87. do_malloc_test fkey_malloc-5 -sqlprep {
  88. PRAGMA foreign_keys = 1;
  89. CREATE TABLE t1(x, y, PRIMARY KEY(x, y));
  90. CREATE TABLE t2(a, b, FOREIGN KEY(a, b) REFERENCES t1 ON UPDATE CASCADE);
  91. INSERT INTO t1 VALUES(1, 2);
  92. INSERT INTO t2 VALUES(1, 2);
  93. } -sqlbody {
  94. UPDATE t1 SET x = 5;
  95. }
  96. do_malloc_test fkey_malloc-6 -sqlprep {
  97. PRAGMA foreign_keys = 1;
  98. CREATE TABLE t1(
  99. x PRIMARY KEY,
  100. y REFERENCES t1 ON DELETE RESTRICT ON UPDATE SET DEFAULT
  101. );
  102. INSERT INTO t1 VALUES('abc', 'abc');
  103. INSERT INTO t1 VALUES('def', 'def');
  104. } -sqlbody {
  105. INSERT INTO t1 VALUES('ghi', 'ghi');
  106. DELETE FROM t1 WHERE rowid>1;
  107. UPDATE t1 SET x='jkl', y='jkl';
  108. }
  109. do_malloc_test fkey_malloc-7 -sqlprep {
  110. PRAGMA foreign_keys = 1;
  111. CREATE TABLE x(a, b, PRIMARY KEY(a, b));
  112. CREATE TABLE y(c, d,
  113. FOREIGN KEY(d, c) REFERENCES x DEFERRABLE INITIALLY DEFERRED
  114. );
  115. CREATE TABLE z(e, f, FOREIGN KEY(e, f) REFERENCES x);
  116. } -sqlbody {
  117. DROP TABLE y;
  118. DROP TABLE x;
  119. }
  120. finish_test