Skip to content

Commit b5f0ffa

Browse files
committed
Move the fix to the correct function
1 parent 0637537 commit b5f0ffa

3 files changed

Lines changed: 64 additions & 15 deletions

File tree

core/src/main/kotlin/org/evomaster/core/problem/api/service/ApiWsStructureMutator.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ abstract class ApiWsStructureMutator : StructureMutator() {
277277
val oldSqlActions = mutableListOf<EnvironmentAction>().plus(ind.seeInitializingActions())
278278

279279
val failedWhereQueries = evaluatedIndividual.fitness.getViewOfAggregatedFailedWhereQueries()
280-
.filter { it.isNotBlank() }
281280
val addedSqlInsertions = handleFailedWhereSQL(ind, fw, failedWhereQueries, mutatedGenes, sampler)
282281

283282
ind.repairInitializationActions(randomness)

core/src/main/kotlin/org/evomaster/core/search/FitnessValue.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,15 @@ class FitnessValue(
152152

153153
aggregatedFailedWhereQueries.clear()
154154
aggregatedFailedWhereQueries.addAll(
155-
databaseExecutions.values.flatMap { a -> a.executionInfo }.map{ b -> b.sqlCommand }
155+
databaseExecutions.values
156+
// Only executions that actually had failing WHERE clauses are relevant;
157+
// collecting from all executions would include unrelated INSERT/UPDATE/DELETE/etc.
158+
.filter { it.failedWhere.isNotEmpty() }
159+
.flatMap { it.executionInfo }
160+
.map { it.sqlCommand }
161+
// JSQLParser >= 4.9 may produce empty-string SQL commands (it used to throw, now
162+
// parses "" to null). Guard here at the source rather than at every call site.
163+
.filter { it.isNotBlank() }
156164
)
157165
}
158166
fun aggregateMongoDatabaseData(){

core/src/test/kotlin/org/evomaster/core/search/FitnessValueTest.kt

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.evomaster.client.java.instrumentation.shared.ObjectiveNaming
77
import org.evomaster.core.search.service.IdMapper
88
import org.evomaster.core.sql.DatabaseExecution
99
import org.evomaster.core.sql.SqlExecutionInfo
10+
import org.evomaster.core.sql.schema.TableId
1011
import org.junit.jupiter.api.Assertions.*
1112
import org.junit.jupiter.api.Test
1213

@@ -126,36 +127,77 @@ class FitnessValueTest {
126127
}
127128

128129
@Test
129-
fun testAggregatedFailedWhereQueriesExcludesBlankEntries() {
130+
fun testAggregatedFailedWhereQueriesOnlyFromExecutionsWithFailedWhere() {
130131
val fv = FitnessValue(1.0)
131132

132-
val execution = DatabaseExecution(
133+
val tableId = TableId("foo")
134+
135+
// Execution that has a failing WHERE clause — its queries should be collected
136+
val executionWithFailedWhere = DatabaseExecution(
137+
queriedData = emptyMap(),
138+
updatedData = emptyMap(),
139+
insertedData = emptyMap(),
140+
failedWhere = mapOf(tableId to setOf("id")),
141+
deletedData = emptyList(),
142+
numberOfSqlCommands = 1,
143+
sqlParseFailureCount = 0,
144+
executionInfo = listOf(
145+
SqlExecutionInfo("SELECT * FROM foo WHERE id = 1", false, 10L)
146+
)
147+
)
148+
149+
// Execution with no failing WHERE clause — its queries must NOT be collected,
150+
// even though it has executionInfo entries
151+
val executionWithoutFailedWhere = DatabaseExecution(
133152
queriedData = emptyMap(),
134153
updatedData = emptyMap(),
135154
insertedData = emptyMap(),
136155
failedWhere = emptyMap(),
137156
deletedData = emptyList(),
157+
numberOfSqlCommands = 1,
158+
sqlParseFailureCount = 0,
159+
executionInfo = listOf(
160+
SqlExecutionInfo("INSERT INTO bar VALUES (1)", false, 5L)
161+
)
162+
)
163+
164+
fv.setDatabaseExecution(0, executionWithFailedWhere)
165+
fv.setDatabaseExecution(1, executionWithoutFailedWhere)
166+
fv.aggregateDatabaseData()
167+
168+
val queries = fv.getViewOfAggregatedFailedWhereQueries()
169+
assertEquals(1, queries.size)
170+
assertTrue(queries.contains("SELECT * FROM foo WHERE id = 1"))
171+
}
172+
173+
@Test
174+
fun testAggregatedFailedWhereQueriesExcludesBlankSqlCommands() {
175+
val fv = FitnessValue(1.0)
176+
177+
val tableId = TableId("foo")
178+
179+
// JSQLParser >= 4.9 can produce empty-string SQL commands; they must be filtered out
180+
// at the source in aggregateDatabaseData() rather than at every call site
181+
val execution = DatabaseExecution(
182+
queriedData = emptyMap(),
183+
updatedData = emptyMap(),
184+
insertedData = emptyMap(),
185+
failedWhere = mapOf(tableId to setOf("id")),
186+
deletedData = emptyList(),
138187
numberOfSqlCommands = 3,
139188
sqlParseFailureCount = 0,
140189
executionInfo = listOf(
141190
SqlExecutionInfo("SELECT * FROM foo WHERE id = 1", false, 10L),
142191
SqlExecutionInfo("", false, 5L),
143-
SqlExecutionInfo(" ", false, 5L),
144-
SqlExecutionInfo("SELECT * FROM bar WHERE name = 'x'", false, 8L)
192+
SqlExecutionInfo(" ", false, 5L)
145193
)
146194
)
147195
fv.setDatabaseExecution(0, execution)
148196
fv.aggregateDatabaseData()
149197

150-
val allQueries = fv.getViewOfAggregatedFailedWhereQueries()
151-
assertEquals(4, allQueries.size)
152-
153-
// Simulate the filter applied in ApiWsStructureMutator before sending queries to the solver
154-
val nonBlankQueries = allQueries.filter { it.isNotBlank() }
155-
assertEquals(2, nonBlankQueries.size)
156-
assertTrue(nonBlankQueries.none { it.isBlank() })
157-
assertTrue(nonBlankQueries.contains("SELECT * FROM foo WHERE id = 1"))
158-
assertTrue(nonBlankQueries.contains("SELECT * FROM bar WHERE name = 'x'"))
198+
val queries = fv.getViewOfAggregatedFailedWhereQueries()
199+
assertEquals(1, queries.size)
200+
assertTrue(queries.contains("SELECT * FROM foo WHERE id = 1"))
159201
}
160202

161203
}

0 commit comments

Comments
 (0)