From 52a484197e142c3ba4f2888e8e68464e5fc421e8 Mon Sep 17 00:00:00 2001 From: dan Date: Mon, 12 Oct 2009 18:57:20 +0000 Subject: [PATCH] Add more test cases to e_fkey.test. FossilOrigin-Name: 5633cb2b5b91a54e677dde76e31e319cd7d51780 --- manifest | 12 +-- manifest.uuid | 2 +- test/e_fkey.test | 251 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 258 insertions(+), 7 deletions(-) diff --git a/manifest b/manifest index 38f86e5b91..da16e24d69 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\ssome\smappings\sfrom\se_fkey.test. -D 2009-10-12T16:02:10 +C Add\smore\stest\scases\sto\se_fkey.test. +D 2009-10-12T18:57:20 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 4ca3f1dd6efa2075bcb27f4dc43eef749877740d F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -319,7 +319,7 @@ F test/descidx2.test 1310ed1326cdfed4ea2c55169631579f082d174f F test/descidx3.test 3394ad4d089335cac743c36a14129d6d931c316f F test/diskfull.test 0cede7ef9d8f415d9d3944005c76be7589bb5ebb F test/distinctagg.test 1a6ef9c87a58669438fc771450d7a72577417376 -F test/e_fkey.test 372ed0d5465dc32385a32e549a0e6b339025b5c1 +F test/e_fkey.test 9f42822f9ca513cffc32a8ce00b074e53e951bf4 F test/enc.test e54531cd6bf941ee6760be041dff19a104c7acea F test/enc2.test 6d91a5286f59add0cfcbb2d0da913b76f2242398 F test/enc3.test 5c550d59ff31dccdba5d1a02ae11c7047d77c041 @@ -756,7 +756,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P a2ca9f1a7a158e7b83a642a8d17549d81caea557 -R 2bcda3d7da62068740f039a19a9b529c +P 171c67138527750ec4af403f05389fae5f7da7b5 +R 5a583fc9c05004483dd8faaeb274030f U dan -Z b8b3532918def361f30fd24f8778cda3 +Z f39171edab73c7d5cf8e2d10e425e90b diff --git a/manifest.uuid b/manifest.uuid index 14321984dc..01f773f937 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -171c67138527750ec4af403f05389fae5f7da7b5 \ No newline at end of file +5633cb2b5b91a54e677dde76e31e319cd7d51780 \ No newline at end of file diff --git a/test/e_fkey.test b/test/e_fkey.test index 5c6cb64a42..1c62303212 100644 --- a/test/e_fkey.test +++ b/test/e_fkey.test @@ -568,6 +568,257 @@ do_test e_fkey-47.4 { ### SECTION 3: Required and Suggested Database Indexes ########################################################################### +#------------------------------------------------------------------------- +# /* EV: R-13435-26311 */ +# +# A parent key must be either a PRIMARY KEY, subject to a UNIQUE +# constraint, or have a UNIQUE index created on it. +# +# /* EV: R-00376-39212 */ +# +# Also test that if a parent key is not subject to a PRIMARY KEY or UNIQUE +# constraint, but does have a UNIQUE index created on it, then the UNIQUE index +# must use the default collation sequences associated with the parent key +# columns. +# +drop_all_tables +do_test e_fkey-57.1 { + execsql { + CREATE TABLE t2(a REFERENCES t1(x)); + } +} {} +proc test_efkey_57 {tn isError sql} { + catchsql { DROP TABLE t1 } + execsql $sql + do_test e_fkey-57.$tn { + catchsql { INSERT INTO t2 VALUES(NULL) } + } [lindex {{0 {}} {1 {foreign key mismatch}}} $isError] +} +test_efkey_57 2 0 { CREATE TABLE t1(x PRIMARY KEY) } +test_efkey_57 3 0 { CREATE TABLE t1(x UNIQUE) } +test_efkey_57 4 0 { CREATE TABLE t1(x); CREATE UNIQUE INDEX t1i ON t1(x) } +test_efkey_57 5 1 { + CREATE TABLE t1(x); + CREATE UNIQUE INDEX t1i ON t1(x COLLATE nocase); +} +test_efkey_57 6 1 { CREATE TABLE t1(x) } +test_efkey_57 7 1 { CREATE TABLE t1(x, y, PRIMARY KEY(x, y)) } +test_efkey_57 8 1 { CREATE TABLE t1(x, y, UNIQUE(x, y)) } +test_efkey_57 9 1 { + CREATE TABLE t1(x, y); + CREATE UNIQUE INDEX t1i ON t1(x, y); +} + + +#------------------------------------------------------------------------- +# This block tests an example in foreignkeys.html. Several testable +# statements refer to this example, as follows +# +# /* EV: R-27484-01467 */ +# +# FK Constraints on child1, child2 and child3 are Ok. +# +# /* EV: R-51039-44840 */ +# +# Problem with FK on child4. +# +# /* EV: R-01060-48788 */ +# +# Problem with FK on child5. +# +# /* EV: R-63088-37469 */ +# +# Problem with FK on child6 and child7. +# +drop_all_tables +do_test e_fkey-56.1 { + execsql { + CREATE TABLE parent(a PRIMARY KEY, b UNIQUE, c, d, e, f); + CREATE UNIQUE INDEX i1 ON parent(c, d); + CREATE INDEX i2 ON parent(e); + CREATE UNIQUE INDEX i3 ON parent(f COLLATE nocase); + + CREATE TABLE child1(f, g REFERENCES parent(a)); -- Ok + CREATE TABLE child2(h, i REFERENCES parent(b)); -- Ok + CREATE TABLE child3(j, k, FOREIGN KEY(j, k) REFERENCES parent(c, d)); -- Ok + CREATE TABLE child4(l, m REFERENCES parent(e)); -- Err + CREATE TABLE child5(n, o REFERENCES parent(f)); -- Err + CREATE TABLE child6(p, q, FOREIGN KEY(p,q) REFERENCES parent(b, c)); -- Err + CREATE TABLE child7(r REFERENCES parent(c)); -- Err + } +} {} +do_test e_fkey-56.2 { + execsql { + INSERT INTO parent VALUES(1, 2, 3, 4, 5, 6); + INSERT INTO child1 VALUES('xxx', 1); + INSERT INTO child2 VALUES('xxx', 2); + INSERT INTO child3 VALUES(3, 4); + } +} {} +do_test e_fkey-56.2 { + catchsql { INSERT INTO child4 VALUES('xxx', 5) } +} {1 {foreign key mismatch}} +do_test e_fkey-56.3 { + catchsql { INSERT INTO child5 VALUES('xxx', 6) } +} {1 {foreign key mismatch}} +do_test e_fkey-56.4 { + catchsql { INSERT INTO child6 VALUES(2, 3) } +} {1 {foreign key mismatch}} +do_test e_fkey-56.5 { + catchsql { INSERT INTO child7 VALUES(3) } +} {1 {foreign key mismatch}} + +#------------------------------------------------------------------------- +# /* EV: R-03108-63659 */ +# /* EV: R-60781-26576 */ +# +# Test errors in the database schema that are detected while preparing +# DML statements. The error text for these messages always matches +# either "foreign key mismatch" or "no such table*" (using [string match]). +# +do_test e_fkey-57.1 { + execsql { + CREATE TABLE c1(c REFERENCES nosuchtable, d); + + CREATE TABLE p2(a, b, UNIQUE(a, b)); + CREATE TABLE c2(c, d, FOREIGN KEY(c, d) REFERENCES p2(a, x)); + + CREATE TABLE p3(a PRIMARY KEY, b); + CREATE TABLE c3(c REFERENCES p3(b), d); + + CREATE TABLE p4(a PRIMARY KEY, b); + CREATE UNIQUE INDEX p4i ON p4(b COLLATE nocase); + CREATE TABLE c4(c REFERENCES p4(b), d); + + CREATE TABLE p5(a PRIMARY KEY, b COLLATE nocase); + CREATE UNIQUE INDEX p5i ON p5(b COLLATE binary); + CREATE TABLE c5(c REFERENCES p4(b), d); + + CREATE TABLE p6(a PRIMARY KEY, b); + CREATE TABLE c6(c, d, FOREIGN KEY(c, d) REFERENCES p6); + + CREATE TABLE p7(a, b, PRIMARY KEY(a, b)); + CREATE TABLE c7(c, d REFERENCES p7); + } +} {} + +foreach {tn tbl err} { + 2 c1 "no such table: main.nosuchtable" + 3 c2 "foreign key mismatch" + 4 c3 "foreign key mismatch" + 5 c4 "foreign key mismatch" + 6 c5 "foreign key mismatch" + 7 c6 "foreign key mismatch" + 8 c7 "foreign key mismatch" +} { + do_test e_fkey-57.$tn { + catchsql "INSERT INTO $tbl VALUES('a', 'b')" + } [list 1 $err] +} + +#------------------------------------------------------------------------- +# /* EV: R-19353-43643 */ +# +# Test the example of foreign key mismatch errors caused by implicitly +# mapping a child key to the primary key of the parent table when the +# child key consists of a different number of columns to that primary key. +# +drop_all_tables +do_test e_fkey-58.1 { + execsql { + CREATE TABLE parent2(a, b, PRIMARY KEY(a,b)); + + CREATE TABLE child8(x, y, FOREIGN KEY(x,y) REFERENCES parent2); -- Ok + CREATE TABLE child9(x REFERENCES parent2); -- Err + CREATE TABLE child10(x,y,z, FOREIGN KEY(x,y,z) REFERENCES parent2); -- Err + } +} {} +do_test e_fkey-58.2 { + execsql { + INSERT INTO parent2 VALUES('I', 'II'); + INSERT INTO child8 VALUES('I', 'II'); + } +} {} +do_test e_fkey-58.3 { + catchsql { INSERT INTO child9 VALUES('I') } +} {1 {foreign key mismatch}} +do_test e_fkey-58.4 { + catchsql { INSERT INTO child9 VALUES('II') } +} {1 {foreign key mismatch}} +do_test e_fkey-58.5 { + catchsql { INSERT INTO child9 VALUES(NULL) } +} {1 {foreign key mismatch}} +do_test e_fkey-58.6 { + catchsql { INSERT INTO child10 VALUES('I', 'II', 'III') } +} {1 {foreign key mismatch}} +do_test e_fkey-58.7 { + catchsql { INSERT INTO child10 VALUES(1, 2, 3) } +} {1 {foreign key mismatch}} +do_test e_fkey-58.8 { + catchsql { INSERT INTO child10 VALUES(NULL, NULL, NULL) } +} {1 {foreign key mismatch}} + +#------------------------------------------------------------------------- +# /* EV: R-23682-59820 */ +# +# Test errors that are reported when creating the child table. +# Specifically: +# +# * different number of child and parent key columns, and +# * child columns that do not exist. +# +# /* EV: R-33883-28833 */ +# +# These errors are reported whether or not FK support is enabled. +# +drop_all_tables +foreach fk [list OFF ON] { + execsql "PRAGMA foreign_keys = $fk" + set i 0 + foreach {sql error} { + "CREATE TABLE child1(a, b, FOREIGN KEY(a, b) REFERENCES p(c))" + {number of columns in foreign key does not match the number of columns in the referenced table} + "CREATE TABLE child2(a, b, FOREIGN KEY(a, b) REFERENCES p(c, d, e))" + {number of columns in foreign key does not match the number of columns in the referenced table} + "CREATE TABLE child2(a, b, FOREIGN KEY(a, c) REFERENCES p(c, d))" + {unknown column "c" in foreign key definition} + "CREATE TABLE child2(a, b, FOREIGN KEY(c, b) REFERENCES p(c, d))" + {unknown column "c" in foreign key definition} + } { + do_test e_fkey-59.$fk.[incr i] { + catchsql $sql + } [list 1 $error] + } +} + +#------------------------------------------------------------------------- +# /* EV: R-47109-40581 */ +# +# Test that a REFERENCING clause that does not specify parent key columns +# implicitly maps to the primary key of the parent table. +# +do_test e_fkey-60.1 { + execsql { + CREATE TABLE p1(a, b, PRIMARY KEY(a, b)); + CREATE TABLE p2(a, b PRIMARY KEY); + CREATE TABLE c1(c, d, FOREIGN KEY(c, d) REFERENCES p1); + CREATE TABLE c2(a, b REFERENCES p2); + } +} {} +proc test_efkey_60 {tn isError sql} { + do_test e_fkey-60.$tn " + catchsql {$sql} + " [lindex {{0 {}} {1 {foreign key constraint failed}}} $isError] +} + +test_efkey_60 2 1 "INSERT INTO c1 VALUES(239, 231)" +test_efkey_60 3 0 "INSERT INTO p1 VALUES(239, 231)" +test_efkey_60 4 0 "INSERT INTO c1 VALUES(239, 231)" +test_efkey_60 5 1 "INSERT INTO c2 VALUES(239, 231)" +test_efkey_60 6 0 "INSERT INTO p2 VALUES(239, 231)" +test_efkey_60 7 0 "INSERT INTO c2 VALUES(239, 231)" + + ########################################################################### ### SECTION 4.1: Composite Foreign Key Constraints ###########################################################################