From 10556cf452ebe8bdd65755ed4b08ae89a007299b Mon Sep 17 00:00:00 2001 From: Dhruv Sringari Date: Mon, 18 Jul 2022 12:10:49 -0700 Subject: [PATCH] Add foreign key tests where the parent table has inverted primary keys (#1117) * Add foreign key tests where the parent table has inverted primary keys * named inserts, and selects --- enginetest/queries/foreign_key_queries.go | 150 ++++++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/enginetest/queries/foreign_key_queries.go b/enginetest/queries/foreign_key_queries.go index 443fbda343..288f8158c5 100644 --- a/enginetest/queries/foreign_key_queries.go +++ b/enginetest/queries/foreign_key_queries.go @@ -1334,4 +1334,154 @@ var ForeignKeyTests = []ScriptTest{ }, }, }, + { + Name: "Table with inverted primary key referencing another table can insert rows", + SetUpScript: []string{ + "create table a (x int, y int, primary key (x,y), INDEX `a_y_idx` (y));", + "create table b (x int, y int, primary key (y,x), foreign key (y) references a(y) on update cascade on delete cascade);", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "INSERT into a (x, y) VALUES (1, 3);", + Expected: []sql.Row{{sql.NewOkResult(1)}}, + }, + { + Query: "INSERT into b (x, y) VALUES (2, 3);", + Expected: []sql.Row{{sql.NewOkResult(1)}}, + }, + { + Query: "SELECT x, y from a;", + Expected: []sql.Row{{1, 3}}, + }, + { + Query: "SELECT x, y from b;", + Expected: []sql.Row{{2, 3}}, + }, + { + Query: "INSERT into b (x, y) VALUES (3, 5);", + ExpectedErr: sql.ErrForeignKeyChildViolation, + }, + }, + }, + { + Name: "Table with inverted primary key referencing another table with inverted primary keys can be inserted", + SetUpScript: []string{ + "create table a (x int, y int, primary key (y,x));", + "create table b (x int, y int, primary key (y,x), foreign key (y) references a(y) on update cascade on delete cascade);", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "INSERT into a (x, y) VALUES (1, 3);", + Expected: []sql.Row{{sql.NewOkResult(1)}}, + }, + { + Query: "INSERT into b (x, y) VALUES (2, 3);", + Expected: []sql.Row{{sql.NewOkResult(1)}}, + }, + { + Query: "SELECT x, y from a;", + Expected: []sql.Row{{1, 3}}, + }, + { + Query: "SELECT x, y from b;", + Expected: []sql.Row{{2, 3}}, + }, + { + Query: "INSERT into b (x, y) VALUES (3, 5);", + ExpectedErr: sql.ErrForeignKeyChildViolation, + }, + }, + }, + { + Name: "Table with inverted primary key referencing another table can be updated", + SetUpScript: []string{ + "create table a (x int, y int, primary key (x,y), INDEX `a_y_idx` (y));", + "create table b (x int, y int, primary key (y,x), foreign key (y) references a(y) on update cascade on delete cascade);", + "INSERT into a VALUES (1, 3);", + "INSERT into b VALUES (2, 3);", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "UPDATE a SET y = 4 where y = 3;", + Expected: []sql.Row{{sql.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}}}, + }, + { + Query: "SELECT x, y from a;", + Expected: []sql.Row{{1, 4}}, + }, + { + Query: "SELECT x, y from b;", + Expected: []sql.Row{{2, 4}}, + }, + }, + }, + { + Name: "Table with inverted primary key referencing another table with inverted primary keys can be updated", + SetUpScript: []string{ + "create table a (x int, y int, primary key (y,x));", + "create table b (x int, y int, primary key (y,x), foreign key (y) references a(y) on update cascade on delete cascade);", + "INSERT into a VALUES (1, 3)", + "INSERT into b VALUES (2, 3)", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "UPDATE a SET y = 4 where y = 3;", + Expected: []sql.Row{{sql.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}}}, + }, + { + Query: "SELECT x, y from a;", + Expected: []sql.Row{{1, 4}}, + }, + { + Query: "SELECT x, y from b;", + Expected: []sql.Row{{2, 4}}, + }, + }, + }, + { + Name: "Table with inverted primary key referencing another table can be deleted", + SetUpScript: []string{ + "create table a (x int, y int, primary key (x,y), INDEX `a_y_idx` (y));", + "create table b (x int, y int, primary key (y,x), foreign key (y) references a(y) on update cascade on delete cascade);", + "INSERT into a VALUES (1, 3);", + "INSERT into b VALUES (2, 3);", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "DELETE from a where x = 1 AND y = 3;", + Expected: []sql.Row{{sql.NewOkResult(1)}}, + }, + { + Query: "SELECT * from a;", + Expected: []sql.Row{}, + }, + { + Query: "SELECT * from b;", + Expected: []sql.Row{}, + }, + }, + }, + { + Name: "Table with inverted primary key referencing another table with inverted primary keys can be deleted", + SetUpScript: []string{ + "create table a (x int, y int, primary key (y,x));", + "create table b (x int, y int, primary key (y,x), foreign key (y) references a(y) on update cascade on delete cascade);", + "INSERT into a VALUES (1, 3)", + "INSERT into b VALUES (2, 3)", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "DELETE from a where x = 1 AND y = 3;", + Expected: []sql.Row{{sql.NewOkResult(1)}}, + }, + { + Query: "SELECT * from a;", + Expected: []sql.Row{}, + }, + { + Query: "SELECT * from b;", + Expected: []sql.Row{}, + }, + }, + }, }