Skip to content

Commit d50059e

Browse files
committed
additional test for merging behavior with foreign key violations
1 parent f8e3acf commit d50059e

1 file changed

Lines changed: 63 additions & 15 deletions

File tree

testing/go/foreign_keys_test.go

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
package _go
1616

1717
import (
18+
"math/big"
1819
"testing"
1920

2021
"github.com/dolthub/go-mysql-server/sql"
22+
"github.com/jackc/pgx/v5/pgtype"
2123
)
2224

2325
func TestForeignKeys(t *testing.T) {
@@ -989,8 +991,7 @@ func TestForeignKeys(t *testing.T) {
989991
},
990992
},
991993
{
992-
Name: "merging",
993-
Focus: true,
994+
Name: "merging",
994995
SetUpScript: []string{
995996
`CREATE TABLE "evaluation_job_config" (
996997
"tenant_id" varchar(256) NOT NULL,
@@ -1026,13 +1027,25 @@ func TestForeignKeys(t *testing.T) {
10261027
"created_at" timestamp DEFAULT now() NOT NULL,
10271028
"updated_at" timestamp DEFAULT now() NOT NULL,
10281029
CONSTRAINT "agent_tenant_id_project_id_id_pk" PRIMARY KEY("tenant_id","project_id","id")
1030+
);`,
1031+
`CREATE TABLE "projects" (
1032+
"tenant_id" varchar(256) NOT NULL,
1033+
"id" varchar(256) NOT NULL,
1034+
"name" varchar(256) NOT NULL,
1035+
"description" text,
1036+
"models" jsonb,
1037+
"stop_when" jsonb,
1038+
"created_at" timestamp DEFAULT now() NOT NULL,
1039+
"updated_at" timestamp DEFAULT now() NOT NULL,
1040+
CONSTRAINT "projects_tenant_id_id_pk" PRIMARY KEY("tenant_id","id")
10291041
);`,
10301042
`ALTER TABLE "evaluation_job_config" ADD CONSTRAINT "evaluation_job_config_project_fk" FOREIGN KEY ("tenant_id","project_id") REFERENCES "public"."projects"("tenant_id","id") ON DELETE cascade ON UPDATE no action;`,
10311043
`ALTER TABLE "evaluation_job_config_evaluator_relations" ADD CONSTRAINT "eval_job_cfg_evaluator_rel_job_cfg_fk" FOREIGN KEY ("tenant_id","project_id","evaluation_job_config_id") REFERENCES "public"."evaluation_job_config"("tenant_id","project_id","id") ON DELETE cascade ON UPDATE no action;`,
1044+
`INSERT INTO projects VALUES ('tenant1', 'project1', 'Project One', 'First project', '{"model": "gpt-4"}', '{"condition": "complete"}', now(), now());`,
10321045
`INSERT INTO evaluation_job_config VALUES ('tenant1', 'jobconfig1', 'project1', '{"filter": "all"}', now(), now());`,
10331046
`INSERT INTO evaluation_job_config_evaluator_relations VALUES ('tenant1', 'rel1', 'project1', 'jobconfig1', 'evaluator1', now(), now());`,
1034-
`INSERT INTO agent VALUES ('tenant1', 'agent1', 'project1', 'Agent One', 'First agent', null, null, '{"model": "gpt-4"}', null, null, now(), now());`,
1035-
`SELECT DOLT_COMMIT('-am', 'initial tables')`,
1047+
`INSERT INTO agent VALUES ('tenant1', 'agent1', 'project1', 'Agent One', 'First agent', null, null, '{"model": "gpt-4"}', '{}', 'You are an agent.', '{}', now(), now());`,
1048+
`SELECT DOLT_COMMIT('-Am', 'initial tables')`,
10361049
`SELECT DOLT_BRANCH('feature')`,
10371050
`CREATE TABLE "triggers" (
10381051
"tenant_id" varchar(256) NOT NULL,
@@ -1052,30 +1065,65 @@ func TestForeignKeys(t *testing.T) {
10521065
CONSTRAINT "triggers_tenant_id_project_id_agent_id_id_pk" PRIMARY KEY("tenant_id","project_id","agent_id","id")
10531066
);`,
10541067
`ALTER TABLE "triggers" ADD CONSTRAINT "triggers_agent_fk" FOREIGN KEY ("tenant_id","project_id","agent_id") REFERENCES "public"."agent"("tenant_id","project_id","id") ON DELETE cascade ON UPDATE no action;`,
1068+
`select DOLT_COMMIT('-Am', 'add triggers table')`,
10551069
},
10561070
Assertions: []ScriptTestAssertion{
10571071
{
1058-
Query: `ALTER TABLE ONLY public.hn_stories
1059-
ADD CONSTRAINT hn_stories_website_url_fkey FOREIGN KEY (website_url) REFERENCES public.websites(url) ON UPDATE SET DEFAULT;`,
1060-
Expected: []sql.Row{},
1072+
Query: "select dolt_checkout('feature')",
1073+
SkipResultsCheck: true,
10611074
},
10621075
{
1063-
Query: "UPDATE public.websites SET url = 'http://fake.com' WHERE title = 'foo1';",
1076+
Query: "insert into agent VALUES ('tenant1', 'agent2', 'project1', 'Agent Two', 'Second agent', null, null, '{\"model\": \"gpt-4\"}', '{}', 'You are another agent.', '{}', now(), now());",
10641077
},
10651078
{
1066-
Query: "SELECT * FROM public.hn_stories where title = 'test1';",
1067-
Expected: []sql.Row{{"test1", nil}},
1079+
Query: "select dolt_commit('-Am', 'add second agent')",
1080+
SkipResultsCheck: true,
10681081
},
10691082
{
1070-
Query: "ALTER TABLE hn_stories ALTER COLUMN website_url SET DEFAULT (title);",
1071-
Expected: []sql.Row{},
1083+
Query: "select dolt_merge('main')",
1084+
SkipResultsCheck: true,
10721085
},
1086+
},
1087+
},
1088+
{
1089+
Name: "merge with constraint violations",
1090+
SetUpScript: []string{
1091+
"CREATE TABLE parent (a INT PRIMARY KEY, b INT UNIQUE);",
1092+
"CREATE TABLE child (c INT PRIMARY KEY, d INT);",
1093+
"alter table child add constraint fk foreign key (d) references parent(b);",
1094+
"INSERT INTO parent VALUES (1, 1), (2, 2), (3, 3);",
1095+
"INSERT INTO child VALUES (1, 1), (2, 2);",
1096+
"SELECT DOLT_COMMIT('-Am', 'initial commit')",
1097+
"SELECT DOLT_BRANCH('feature')",
1098+
"insert into child VALUES (3, 3);",
1099+
"SELECT DOLT_COMMIT('-Am', 'new child')",
1100+
"select dolt_checkout('feature')",
1101+
"delete from parent where b = 3;",
1102+
"SELECT DOLT_COMMIT('-Am', 'delete from parent')",
1103+
},
1104+
Assertions: []ScriptTestAssertion{
10731105
{
1074-
Query: "UPDATE public.websites SET url = 'http://doltdb.com' WHERE title = 'foo2';",
1106+
Query: "select dolt_merge('main')",
1107+
ExpectedErr: "constraint violations",
10751108
},
10761109
{
1077-
Query: "SELECT * FROM public.hn_stories where title = 'test2';",
1078-
Expected: []sql.Row{{"test2", "test2"}},
1110+
Query: "set dolt_force_transaction_commit = 1;",
1111+
},
1112+
{
1113+
Query: "select dolt_merge('main')",
1114+
SkipResultsCheck: true,
1115+
},
1116+
{
1117+
Query: "select * from dolt_constraint_violations order by 1",
1118+
Expected: []sql.Row{
1119+
{"child", pgtype.Numeric{Int: big.NewInt(1), Valid: true}},
1120+
},
1121+
},
1122+
{
1123+
Query: "select violation_type, c, d, violation_info from dolt_constraint_violations_child order by 1",
1124+
Expected: []sql.Row{
1125+
{"foreign key", 3, 3, "{\"Columns\":[\"d\"],\"ForeignKey\":\"fk\",\"Index\":\"fk\",\"OnDelete\":\"RESTRICT\",\"OnUpdate\":\"RESTRICT\",\"ReferencedColumns\":[\"b\"],\"ReferencedIndex\":\"b\",\"ReferencedTable\":\"parent\",\"Table\":\"child\"}"},
1126+
},
10791127
},
10801128
},
10811129
},

0 commit comments

Comments
 (0)