Skip to content

Commit 7483daf

Browse files
committed
Add test for implicit composite foreign keys and for multiple foreign keys in the same table.
1 parent 0b21414 commit 7483daf

1 file changed

Lines changed: 91 additions & 4 deletions

File tree

client-java/sql/src/test/java/org/evomaster/client/java/sql/DbInfoExtractorTestBase.java

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,100 @@ public void testCompositeForeignKey() throws Exception {
5050
ForeignKeyDto foreignKey = child.foreignKeys.get(0);
5151

5252
assertEquals(2, foreignKey.sourceColumns.size());
53-
assertTrue(foreignKey.sourceColumns.stream().anyMatch(c -> c.equalsIgnoreCase("pid1")));
54-
assertTrue(foreignKey.sourceColumns.stream().anyMatch(c -> c.equalsIgnoreCase("pid2")));
53+
assertTrue(foreignKey.sourceColumns.get(0).equalsIgnoreCase("pid1"));
54+
assertTrue(foreignKey.sourceColumns.get(1).equalsIgnoreCase("pid2"));
5555
assertTrue(foreignKey.targetTable.equalsIgnoreCase("Parent"));
5656

5757
assertEquals(2, foreignKey.targetColumns.size());
58-
assertTrue(foreignKey.targetColumns.stream().anyMatch(c -> c.equalsIgnoreCase("id1")));
59-
assertTrue(foreignKey.targetColumns.stream().anyMatch(c -> c.equalsIgnoreCase("id2")));
58+
assertTrue(foreignKey.targetColumns.get(0).equalsIgnoreCase("id1"));
59+
assertTrue(foreignKey.targetColumns.get(1).equalsIgnoreCase("id2"));
60+
}
61+
62+
@Test
63+
public void testOneImplicitCompositeForeignKey() throws Exception {
64+
/**
65+
* Implicity foreign keys are not supported by MySQL, so we skip this test for MySQL databases.
66+
*/
67+
Assumptions.assumeTrue(this.getDbType() != DatabaseType.MYSQL);
68+
69+
SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Parent(" +
70+
"id1 bigint, " +
71+
"id2 bigint, " +
72+
"primary key (id1, id2)" +
73+
")");
74+
SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Child(" +
75+
"id bigint primary key, " +
76+
"pid1 bigint not null, " +
77+
"pid2 bigint not null, " +
78+
"foreign key (pid1, pid2) references Parent" +
79+
")");
80+
81+
DbInfoDto schema = DbInfoExtractor.extract(getConnection());
82+
TableDto child = schema.tables.stream().filter(t -> t.id.name.equalsIgnoreCase("Child")).findAny().get();
83+
84+
assertEquals(1, child.foreignKeys.size());
85+
86+
ForeignKeyDto foreignKey = child.foreignKeys.get(0);
87+
88+
assertEquals(2, foreignKey.sourceColumns.size());
89+
assertTrue(foreignKey.sourceColumns.get(0).equalsIgnoreCase("pid1"));
90+
assertTrue(foreignKey.sourceColumns.get(1).equalsIgnoreCase("pid2"));
91+
assertTrue(foreignKey.targetTable.equalsIgnoreCase("Parent"));
92+
93+
assertEquals(2, foreignKey.targetColumns.size());
94+
assertTrue(foreignKey.targetColumns.get(0).equalsIgnoreCase("id1"));
95+
assertTrue(foreignKey.targetColumns.get(1).equalsIgnoreCase("id2"));
96+
}
97+
98+
@Test
99+
public void testTwoCompositeExplicitForeignKeys() throws Exception {
100+
101+
SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Parent1(" +
102+
"id1 bigint, " +
103+
"id2 bigint, " +
104+
"primary key (id1, id2)" +
105+
")");
106+
SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Parent2(" +
107+
"id1 bigint, " +
108+
"id2 bigint, " +
109+
"primary key (id1, id2)" +
110+
")");
111+
SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE ChildTwoImplicit(" +
112+
"id bigint primary key, " +
113+
"p1_id1 bigint not null, " +
114+
"p1_id2 bigint not null, " +
115+
"p2_id1 bigint not null, " +
116+
"p2_id2 bigint not null, " +
117+
"foreign key (p1_id1, p1_id2) references Parent1(id1, id2), " +
118+
"foreign key (p2_id1, p2_id2) references Parent2(id1, id2) " +
119+
")");
120+
121+
DbInfoDto schema = DbInfoExtractor.extract(getConnection());
122+
TableDto child = schema.tables.stream().filter(t -> t.id.name.equalsIgnoreCase("ChildTwoImplicit")).findAny().get();
123+
124+
assertEquals(2, child.foreignKeys.size());
125+
126+
ForeignKeyDto fk1 = child.foreignKeys.stream()
127+
.filter(fk -> fk.targetTable.equalsIgnoreCase("Parent1"))
128+
.findFirst().get();
129+
130+
assertEquals(2, fk1.sourceColumns.size());
131+
assertTrue(fk1.sourceColumns.get(0).equalsIgnoreCase("p1_id1"));
132+
assertTrue(fk1.sourceColumns.get(1).equalsIgnoreCase("p1_id2"));
133+
assertEquals(2, fk1.targetColumns.size());
134+
assertTrue(fk1.targetColumns.get(0).equalsIgnoreCase("id1"));
135+
assertTrue(fk1.targetColumns.get(1).equalsIgnoreCase("id2"));
136+
137+
ForeignKeyDto fk2 = child.foreignKeys.stream()
138+
.filter(fk -> fk.targetTable.equalsIgnoreCase("Parent2"))
139+
.findFirst().get();
140+
141+
assertEquals(2, fk2.sourceColumns.size());
142+
assertTrue(fk2.sourceColumns.get(0).equalsIgnoreCase("p2_id1"));
143+
assertTrue(fk2.sourceColumns.get(1).equalsIgnoreCase("p2_id2"));
144+
assertEquals(2, fk2.targetColumns.size());
145+
assertTrue(fk2.targetColumns.get(0).equalsIgnoreCase("id1"));
146+
assertTrue(fk2.targetColumns.get(1).equalsIgnoreCase("id2"));
60147
}
61148

62149
}

0 commit comments

Comments
 (0)