Skip to content

Commit 7ea6907

Browse files
committed
fixed merge of sql actions
1 parent 52e7c01 commit 7ea6907

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

core/src/main/kotlin/org/evomaster/core/problem/enterprise/EnterpriseIndividual.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,24 @@ abstract class EnterpriseIndividual(
409409
* if [relativePosition] = -1, append the [actions] at the end
410410
*/
411411
fun addInitializingDbActions(relativePosition: Int=-1, actions: List<Action>){
412-
if (relativePosition < 0) {
413-
addChildrenToGroup(getLastIndexOfDbActionToAdd(), actions, GroupsOfChildren.INITIALIZATION_SQL)
414-
} else{
415-
addChildrenToGroup(getFirstIndexOfDbActionToAdd()+relativePosition, actions, GroupsOfChildren.INITIALIZATION_SQL)
412+
/*
413+
SQL actions representing existing data must ALWAYS be at the beginning.
414+
Recall those are only used for FKs.
415+
*/
416+
val (existing, others) = actions.partition { it is SqlAction && it.representExistingData }
417+
418+
if(existing.isNotEmpty()){
419+
addChildrenToGroup(getFirstIndexOfDbActionToAdd(), existing, GroupsOfChildren.INITIALIZATION_SQL)
420+
//TODO shall we check for duplications???
421+
}
422+
423+
if(others.isNotEmpty()) {
424+
val pos = if (relativePosition < 0) {
425+
getLastIndexOfDbActionToAdd()
426+
} else {
427+
getFirstIndexOfDbActionToAdd() + relativePosition
428+
}
429+
addChildrenToGroup(pos, others, GroupsOfChildren.INITIALIZATION_SQL)
416430
}
417431
}
418432

core/src/test/kotlin/org/evomaster/core/problem/rest/service/RestIndividualBuilderTest.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import org.evomaster.core.sql.schema.ColumnDataType
1414
import org.evomaster.core.sql.schema.Table
1515
import org.evomaster.core.sql.schema.TableId
1616
import org.junit.jupiter.api.Assertions.assertEquals
17+
import org.junit.jupiter.api.Assertions.assertFalse
18+
import org.junit.jupiter.api.Assertions.assertTrue
1719
import org.junit.jupiter.api.Test
1820

1921
class RestIndividualBuilderTest {
@@ -168,9 +170,16 @@ class RestIndividualBuilderTest {
168170
// initializing SQL actions should contain both sql1 and sql2
169171
val inits = merged.seeInitializingActions().filterIsInstance<SqlAction>()
170172
assertEquals(4, inits.size)
171-
// check their table names preserved and order: first's sql then second's sql
172-
assertEquals("T1", inits[0].table.name)
173-
assertEquals("T2", inits[1].table.name)
173+
/*
174+
check their table names preserved and order: first's sql then second's sql.
175+
however, existing data is always at the beginning
176+
*/
177+
assertTrue(inits[0].representExistingData)
178+
assertTrue(inits[1].representExistingData)
179+
assertFalse(inits[2].representExistingData)
180+
assertFalse(inits[3].representExistingData)
181+
assertEquals("T1", inits[2].table.name)
182+
assertEquals("T2", inits[3].table.name)
174183

175184
// merged total actions should be sum of both initial individuals
176185
val before = first.seeAllActions().size + second.seeAllActions().size

0 commit comments

Comments
 (0)