Skip to content

Commit 0a0dc15

Browse files
authored
Merge pull request #1204 from WebFuzzing/fix-smartdbclean
Fix smartdbclean
2 parents 968d328 + fa385d7 commit 0a0dc15

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

Diff for: client-java/controller/src/main/java/org/evomaster/client/java/controller/internal/SutController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ private int getFkDepth(String tableName, Set<String> checked){
561561
}
562562
int sum = fks.size();
563563
for (String fk: fks){
564-
if (!checked.contains(fk)){
564+
if (!checked.stream().anyMatch(c -> c.equalsIgnoreCase(fk))){
565565
sum += getFkDepth(fk, checked);
566566
}
567567
}

Diff for: client-java/controller/src/test/java/org/evomaster/client/java/controller/internal/db/sql/InitSqlScriptWithSmartDbCleanTest.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ default String getInitSqlScript() {
2222
"INSERT INTO Bar (id, valueColumn) VALUES (0, 0);",
2323
"INSERT INTO Bar (id, valueColumn) VALUES (1, 0);",
2424
"INSERT INTO Bar (id, valueColumn) VALUES (2, 0);",
25-
"INSERT INTO Foo (id, valueColumn, bar_id) VALUES (0, 0, 0);",
26-
"INSERT INTO Foo (id, valueColumn, bar_id) VALUES (1, 0, 1);",
27-
"INSERT INTO Foo (id, valueColumn, bar_id) VALUES (2, 0, 2);",
25+
"INSERT INTO Foo (id, valueColumn, refer_foo, bar_id) VALUES (0, 0, NULL, 0);",
26+
"INSERT INTO Foo (id, valueColumn, refer_foo, bar_id) VALUES (1, 0, 0, 1);",
27+
"INSERT INTO Foo (id, valueColumn, refer_foo, bar_id) VALUES (2, 0, 1, 2);",
28+
"UPDATE Foo SET valueColumn = 2 WHERE id = 2;",
2829
"INSERT INTO Abc (id, valueColumn, foo_id) VALUES (0, 0, 0);",
2930
"INSERT INTO Abc (id, valueColumn, foo_id) VALUES (1, 0, 1);",
3031
"INSERT INTO Abc (id, valueColumn, foo_id) VALUES (2, 0, 2);",
@@ -39,8 +40,9 @@ default String getInitSqlScript() {
3940
@Test
4041
default void testAccessedFkClean() throws Exception {
4142
EMSqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Bar(id INT Primary Key, valueColumn INT)", true);
42-
EMSqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(id INT Primary Key, valueColumn INT, bar_id INT, " +
43-
"CONSTRAINT fk_foo FOREIGN KEY (bar_id) REFERENCES Bar(id) )", true);
43+
EMSqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(id INT Primary Key, valueColumn INT, refer_foo INT DEFAULT NULL, bar_id INT, " +
44+
"CONSTRAINT fk_foo FOREIGN KEY (bar_id) REFERENCES Bar(id)," +
45+
"CONSTRAINT fk_self_foo FOREIGN KEY (refer_foo) REFERENCES Foo(id) )", true);
4446
EMSqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Abc(id INT Primary Key, valueColumn INT, foo_id INT, " +
4547
"CONSTRAINT fk_abc FOREIGN KEY (foo_id) REFERENCES Foo(id) )", true);
4648
EMSqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Xyz(id INT Primary Key, valueColumn INT, abc_id INT, " +
@@ -66,6 +68,10 @@ default void testAccessedFkClean() throws Exception {
6668
assertEquals(3, res.seeRows().size());
6769
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;", true);
6870
assertEquals(3, res.seeRows().size());
71+
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo WHERE valueColumn = 2;", true);
72+
assertEquals(1, res.seeRows().size());
73+
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo WHERE valueColumn = 0;", true);
74+
assertEquals(2, res.seeRows().size());
6975
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Abc;", true);
7076
assertEquals(3, res.seeRows().size());
7177
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Xyz;", true);
@@ -83,6 +89,10 @@ default void testAccessedFkClean() throws Exception {
8389
assertEquals(3, res.seeRows().size());
8490
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;", true);
8591
assertEquals(3, res.seeRows().size());
92+
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo WHERE valueColumn = 2;", true);
93+
assertEquals(1, res.seeRows().size());
94+
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo WHERE valueColumn = 0;", true);
95+
assertEquals(2, res.seeRows().size());
8696
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Abc;", true);
8797
assertEquals(3, res.seeRows().size());
8898
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Xyz;", true);
@@ -126,7 +136,7 @@ default void testAccessedFkClean() throws Exception {
126136

127137

128138
//2nd-round test: table is accessed with INSERT
129-
EMSqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (id, valueColumn, bar_id) VALUES (3, 0, 0);", true);
139+
EMSqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (id, valueColumn, refer_foo, bar_id) VALUES (3, 0, 1, 0);", true);
130140

131141
res = EMSqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;", true);
132142
assertEquals(4, res.seeRows().size());

Diff for: client-java/sql/src/main/java/org/evomaster/client/java/sql/SqlScriptRunner.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.sf.jsqlparser.schema.Table;
44
import net.sf.jsqlparser.statement.insert.Insert;
5+
import net.sf.jsqlparser.statement.update.Update;
56
import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto;
67
import org.evomaster.client.java.controller.api.dto.database.operations.InsertionEntryDto;
78
import org.evomaster.client.java.controller.api.dto.database.operations.InsertionResultsDto;
@@ -416,16 +417,21 @@ public static List<String> extractSql(String script){
416417
}
417418

418419
/**
419-
* extract a map from table name to a list of SQL INSERT commands for initializing data into the table
420+
* extract a map from table name to a list of SQL INSERT or Update commands for initializing data into the table
420421
* @param commands a list of SQL commands to be extracted
421422
* @return the map from table name (key) to a list of SQL INSERT commands (values) on the table
422423
*/
423424
public static Map<String, List<String>> extractSqlTableMap(List<String> commands){
424425
Map<String, List<String>> tableSqlMap = new HashMap<>();
425426
for (String command: commands){
426-
if (SqlParserUtils.isInsert(command)){
427-
Insert stmt = (Insert) SqlParserUtils.parseSqlCommand(command);
428-
Table table = stmt.getTable();
427+
if (SqlParserUtils.isInsert(command) || SqlParserUtils.isUpdate(command)){
428+
net.sf.jsqlparser.statement.Statement stmt = SqlParserUtils.parseSqlCommand(command);
429+
Table table = null;
430+
if (stmt instanceof Insert){
431+
table = ((Insert) stmt).getTable();
432+
}else if (stmt instanceof Update){
433+
table = ((Update) stmt).getTable();
434+
}
429435
tableSqlMap.putIfAbsent(table.getName(), new ArrayList<>());
430436
String end = "";
431437
if (!command.replaceAll(" ","").replaceAll("\r","").replaceAll("\n","").endsWith(";"))

0 commit comments

Comments
 (0)