Skip to content

Commit c072298

Browse files
committed
Refactor and consolidate composite foreign key tests across databases into shared base classes (MYSQL, POSTGRES and H2)
1 parent ed8453d commit c072298

8 files changed

Lines changed: 242 additions & 116 deletions

File tree

client-java/controller/src/test/java/org/evomaster/client/java/controller/internal/db/sql/h2/H2SchemaExtractorTest.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -151,42 +151,6 @@ public void testBasicForeignKey() throws Exception {
151151
assertTrue(foreignKey.targetColumns.stream().anyMatch(c -> c.equalsIgnoreCase("id")));
152152
}
153153

154-
@Test
155-
public void testCompositeForeignKey() throws Exception {
156-
157-
SqlScriptRunner.execCommand(getConnection(),
158-
" CREATE TABLE Parent(id1 bigint, id2 bigint, primary key (id1, id2));"
159-
+
160-
"CREATE TABLE Child(" +
161-
" id bigint generated by default as identity " +
162-
", pid1 bigint not null " +
163-
", pid2 bigint not null " +
164-
");"
165-
+
166-
" ALTER TABLE Child add constraint compositeKey foreign key (pid1, pid2) references Parent(id1, id2);\n"
167-
);
168-
169-
DbInfoDto schema = DbInfoExtractor.extract(getConnection());
170-
assertEquals(2, schema.tables.size());
171-
172-
TableDto parent = schema.tables.stream().filter(t -> t.id.name.equalsIgnoreCase("Parent")).findAny().get();
173-
TableDto child = schema.tables.stream().filter(t -> t.id.name.equalsIgnoreCase("Child")).findAny().get();
174-
175-
assertEquals(0, parent.foreignKeys.size());
176-
assertEquals(1, child.foreignKeys.size());
177-
178-
ForeignKeyDto foreignKey = child.foreignKeys.get(0);
179-
180-
assertEquals(2, foreignKey.sourceColumns.size());
181-
assertTrue(foreignKey.sourceColumns.stream().anyMatch(c -> c.equalsIgnoreCase("pid1")));
182-
assertTrue(foreignKey.sourceColumns.stream().anyMatch(c -> c.equalsIgnoreCase("pid2")));
183-
assertTrue(foreignKey.targetTable.equalsIgnoreCase("Parent"));
184-
185-
assertEquals(2, foreignKey.targetColumns.size());
186-
assertTrue(foreignKey.targetColumns.stream().anyMatch(c -> c.equalsIgnoreCase("id1")));
187-
assertTrue(foreignKey.targetColumns.stream().anyMatch(c -> c.equalsIgnoreCase("id2")));
188-
}
189-
190154
@Test
191155
public void testQuizGame() throws Exception {
192156

client-java/controller/src/test/java/org/evomaster/client/java/controller/internal/db/sql/mysql/MySQLSchemaExtractorTest.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -192,34 +192,4 @@ private static void deleteUserInDatabase(String url, String anotherTestUserName)
192192
SqlScriptRunner.execCommand(rootConnection, String.format("DROP USER '%s'", anotherTestUserName));
193193
}
194194

195-
@Test
196-
public void testCompositeForeignKey() throws Exception {
197-
198-
SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Parent(id1 bigint, id2 bigint, primary key (id1, id2))");
199-
SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Child(" +
200-
" id bigint primary key auto_increment " +
201-
", pid1 bigint not null " +
202-
", pid2 bigint not null " +
203-
")");
204-
SqlScriptRunner.execCommand(getConnection(), "ALTER TABLE Child add constraint compositeKey foreign key (pid1, pid2) references Parent(id1, id2)");
205-
206-
DbInfoDto schema = DbInfoExtractor.extract(getConnection());
207-
TableDto parent = schema.tables.stream().filter(t -> t.id.name.equalsIgnoreCase("Parent")).findAny().get();
208-
TableDto child = schema.tables.stream().filter(t -> t.id.name.equalsIgnoreCase("Child")).findAny().get();
209-
210-
assertEquals(0, parent.foreignKeys.size());
211-
assertEquals(1, child.foreignKeys.size());
212-
213-
ForeignKeyDto foreignKey = child.foreignKeys.get(0);
214-
215-
assertEquals(2, foreignKey.sourceColumns.size());
216-
assertTrue(foreignKey.sourceColumns.stream().anyMatch(c -> c.equalsIgnoreCase("pid1")));
217-
assertTrue(foreignKey.sourceColumns.stream().anyMatch(c -> c.equalsIgnoreCase("pid2")));
218-
assertTrue(foreignKey.targetTable.equalsIgnoreCase("Parent"));
219-
220-
assertEquals(2, foreignKey.targetColumns.size());
221-
assertTrue(foreignKey.targetColumns.stream().anyMatch(c -> c.equalsIgnoreCase("id1")));
222-
assertTrue(foreignKey.targetColumns.stream().anyMatch(c -> c.equalsIgnoreCase("id2")));
223-
}
224-
225195
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.evomaster.client.java.sql;
2+
3+
import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;
4+
import org.junit.jupiter.api.AfterAll;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.BeforeEach;
7+
8+
import java.sql.Connection;
9+
import java.sql.DriverManager;
10+
11+
12+
public class DbInfoExtractorH2Test extends DbInfoExtractorTestBase {
13+
14+
private static Connection connection;
15+
16+
@BeforeAll
17+
public static void initClass() throws Exception {
18+
connection = DriverManager.getConnection("jdbc:h2:mem:db_test", "sa", "");
19+
}
20+
21+
@AfterAll
22+
public static void afterClass() throws Exception {
23+
connection.close();
24+
}
25+
26+
@BeforeEach
27+
public void initTest() throws Exception {
28+
//custom H2 command
29+
SqlScriptRunner.execCommand(connection, "DROP ALL OBJECTS;");
30+
}
31+
32+
33+
@Override
34+
protected DatabaseType getDbType() {
35+
return DatabaseType.H2;
36+
}
37+
38+
@Override
39+
protected Connection getConnection() {
40+
return connection;
41+
}
42+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.evomaster.client.java.sql;
2+
3+
import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;
4+
import org.junit.jupiter.api.AfterAll;
5+
import org.junit.jupiter.api.AfterEach;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
import org.testcontainers.containers.GenericContainer;
9+
10+
import java.sql.Connection;
11+
import java.sql.DriverManager;
12+
import java.sql.SQLException;
13+
import java.text.SimpleDateFormat;
14+
import java.util.Date;
15+
import java.util.HashMap;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
20+
public class DbInfoExtractorMySQLTest extends DbInfoExtractorTestBase {
21+
22+
private static final String DB_NAME = "test";
23+
24+
private static final int PORT = 3306;
25+
26+
private static final String MYSQL_VERSION = "8.0.27";
27+
28+
public static final GenericContainer mysql = new GenericContainer("mysql:" + MYSQL_VERSION)
29+
.withEnv(new HashMap<String, String>(){{
30+
put("MYSQL_ROOT_PASSWORD", "root");
31+
put("MYSQL_DATABASE", DB_NAME);
32+
put("MYSQL_USER", "test");
33+
put("MYSQL_PASSWORD", "test");
34+
}})
35+
.withExposedPorts(PORT);
36+
37+
private static Connection connection;
38+
39+
@BeforeAll
40+
public static void initClass() throws Exception {
41+
42+
mysql.start();
43+
44+
String host = mysql.getContainerIpAddress();
45+
int port = mysql.getMappedPort(PORT);
46+
String url = "jdbc:mysql://"+host+":"+port+"/"+DB_NAME;
47+
48+
connection = DriverManager.getConnection(url, "test", "test");
49+
50+
}
51+
52+
@AfterAll
53+
public static void afterClass() throws Exception {
54+
connection.close();
55+
mysql.stop();
56+
}
57+
58+
@AfterEach
59+
public void afterTest() throws SQLException {
60+
SqlScriptRunner.execCommand(connection, "DROP TABLE IF EXISTS example_table;");
61+
}
62+
63+
64+
@Override
65+
protected DatabaseType getDbType() {
66+
return DatabaseType.MYSQL;
67+
}
68+
69+
@Override
70+
protected Connection getConnection() {
71+
return connection;
72+
}
73+
74+
75+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.evomaster.client.java.sql;
2+
3+
import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;
4+
import org.junit.jupiter.api.AfterAll;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.testcontainers.containers.GenericContainer;
8+
9+
import java.sql.Connection;
10+
import java.sql.DriverManager;
11+
import java.util.Collections;
12+
13+
public class DbInfoExtractorPostgresTest extends DbInfoExtractorTestBase {
14+
15+
private static final String POSTGRES_VERSION = "14";
16+
17+
private static final GenericContainer<?> postgres = new GenericContainer("postgres:" + POSTGRES_VERSION)
18+
.withExposedPorts(5432)
19+
.withTmpFs(Collections.singletonMap("/var/lib/postgresql/data", "rw"))
20+
.withEnv("POSTGRES_HOST_AUTH_METHOD","trust");
21+
22+
private static Connection connection;
23+
24+
@BeforeAll
25+
public static void initClass() throws Exception{
26+
postgres.start();
27+
String host = postgres.getHost();
28+
int port = postgres.getMappedPort(5432);
29+
String url = "jdbc:postgresql://"+host+":"+port+"/postgres";
30+
31+
connection = DriverManager.getConnection(url, "postgres", "");
32+
}
33+
34+
@AfterAll
35+
public static void afterClass() throws Exception{
36+
connection.close();
37+
postgres.stop();
38+
}
39+
40+
@BeforeEach
41+
public void initTest() throws Exception {
42+
/*
43+
see:
44+
https://stackoverflow.com/questions/3327312/how-can-i-drop-all-the-tables-in-a-postgresql-database
45+
*/
46+
SqlScriptRunner.execCommand(connection, "DROP SCHEMA public CASCADE;");
47+
SqlScriptRunner.execCommand(connection, "CREATE SCHEMA public;");
48+
SqlScriptRunner.execCommand(connection, "GRANT ALL ON SCHEMA public TO postgres;");
49+
SqlScriptRunner.execCommand(connection, "GRANT ALL ON SCHEMA public TO public;");
50+
}
51+
52+
@Override
53+
protected Connection getConnection(){
54+
return connection;
55+
}
56+
57+
@Override
58+
protected DatabaseType getDbType() {
59+
return DatabaseType.POSTGRES;
60+
}
61+
62+
63+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.evomaster.client.java.sql;
2+
3+
import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;
4+
import org.evomaster.client.java.controller.api.dto.database.schema.DbInfoDto;
5+
import org.evomaster.client.java.controller.api.dto.database.schema.ForeignKeyDto;
6+
import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;
7+
import org.junit.jupiter.api.Assumptions;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.sql.Connection;
11+
import java.sql.Time;
12+
import java.sql.Timestamp;
13+
import java.text.SimpleDateFormat;
14+
import java.time.*;
15+
import java.time.format.DateTimeFormatter;
16+
import java.util.Date;
17+
import java.util.UUID;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
import static org.junit.jupiter.api.Assumptions.assumeFalse;
22+
23+
public abstract class DbInfoExtractorTestBase {
24+
25+
protected abstract DatabaseType getDbType();
26+
protected abstract Connection getConnection();
27+
28+
@Test
29+
public void testCompositeForeignKey() throws Exception {
30+
31+
SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Parent(" +
32+
"id1 bigint, " +
33+
"id2 bigint, " +
34+
"primary key (id1, id2)" +
35+
")");
36+
SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Child(" +
37+
"id bigint primary key, " +
38+
"pid1 bigint not null, " +
39+
"pid2 bigint not null " +
40+
")");
41+
SqlScriptRunner.execCommand(getConnection(), "ALTER TABLE Child add constraint compositeKey foreign key (pid1, pid2) references Parent(id1, id2)");
42+
43+
DbInfoDto schema = DbInfoExtractor.extract(getConnection());
44+
TableDto parent = schema.tables.stream().filter(t -> t.id.name.equalsIgnoreCase("Parent")).findAny().get();
45+
TableDto child = schema.tables.stream().filter(t -> t.id.name.equalsIgnoreCase("Child")).findAny().get();
46+
47+
assertEquals(0, parent.foreignKeys.size());
48+
assertEquals(1, child.foreignKeys.size());
49+
50+
ForeignKeyDto foreignKey = child.foreignKeys.get(0);
51+
52+
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")));
55+
assertTrue(foreignKey.targetTable.equalsIgnoreCase("Parent"));
56+
57+
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")));
60+
}
61+
62+
}

core/src/test/kotlin/org/evomaster/core/sql/extract/postgres/CompositeForeignKeyTest.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

core/src/test/resources/sql_schema/postgres_composite_fk.sql

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)