Skip to content
Permalink
Browse files

refactoring SQL genes in own package, and fix for new tests after rebase

  • Loading branch information...
arcuri82 committed Jul 4, 2019
1 parent ae75dc4 commit 989a116d10bb0f27160c71cc3150b98aa6e4054c
Showing with 202 additions and 97 deletions.
  1. +2 −0 core/src/main/kotlin/org/evomaster/core/database/DbAction.kt
  2. +9 −10 core/src/main/kotlin/org/evomaster/core/database/DbActionGeneBuilder.kt
  3. +5 −11 core/src/main/kotlin/org/evomaster/core/database/DbActionTransformer.kt
  4. +2 −2 core/src/main/kotlin/org/evomaster/core/database/DbActionUtils.kt
  5. +1 −1 core/src/main/kotlin/org/evomaster/core/database/SqlInsertBuilder.kt
  6. +6 −14 core/src/main/kotlin/org/evomaster/core/output/TestCaseWriter.kt
  7. +2 −1 core/src/main/kotlin/org/evomaster/core/search/gene/{ → sql}/SqlAutoIncrementGene.kt
  8. +2 −1 core/src/main/kotlin/org/evomaster/core/search/gene/{ → sql}/SqlForeignKeyGene.kt
  9. +3 −1 core/src/main/kotlin/org/evomaster/core/search/gene/{ → sql}/SqlJSONGene.kt
  10. +68 −0 core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlNullable.kt
  11. +2 −1 core/src/main/kotlin/org/evomaster/core/search/gene/{ → sql}/SqlPrimaryKeyGene.kt
  12. +2 −1 core/src/main/kotlin/org/evomaster/core/search/gene/{ → sql}/SqlTimestampGene.kt
  13. +3 −1 core/src/main/kotlin/org/evomaster/core/search/gene/{ → sql}/SqlUUIDGene.kt
  14. +14 −0 core/src/main/kotlin/org/evomaster/core/search/gene/sql/SqlWrapperGene.kt
  15. +3 −1 core/src/main/kotlin/org/evomaster/core/search/gene/{ → sql}/SqlXMLGene.kt
  16. +2 −0 core/src/main/kotlin/org/evomaster/core/search/service/mutator/StandardMutator.kt
  17. +3 −0 core/src/test/kotlin/org/evomaster/core/database/DbActionUtilsTest.kt
  18. +3 −0 core/src/test/kotlin/org/evomaster/core/database/SqlInsertBuilderTest.kt
  19. +35 −35 core/src/test/kotlin/org/evomaster/core/database/TableConstraintEvaluatorTest.kt
  20. +1 −1 core/src/test/kotlin/org/evomaster/core/database/extract/h2/CatwatchSqlExtractTest.kt
  21. +2 −1 core/src/test/kotlin/org/evomaster/core/database/extract/h2/OcvnExtractTest.kt
  22. +3 −3 core/src/test/kotlin/org/evomaster/core/database/extract/h2/ProxyPrintSqlExtractTest.kt
  23. +1 −0 core/src/test/kotlin/org/evomaster/core/database/extract/postgres/Ind0ExtractTest.kt
  24. +1 −1 core/src/test/kotlin/org/evomaster/core/database/extract/postgres/SqlDateColumnTest.kt
  25. +2 −0 core/src/test/kotlin/org/evomaster/core/database/extract/postgres/SqlJSONBColumnTest.kt
  26. +5 −4 core/src/test/kotlin/org/evomaster/core/database/extract/postgres/SqlTextColumnTest.kt
  27. +6 −5 core/src/test/kotlin/org/evomaster/core/database/extract/postgres/SqlUUIDColumnTest.kt
  28. +1 −0 core/src/test/kotlin/org/evomaster/core/database/extract/postgres/SqlXMLColumnTest.kt
  29. +1 −0 core/src/test/kotlin/org/evomaster/core/output/TestCaseWriterTest.kt
  30. +2 −0 core/src/test/kotlin/org/evomaster/core/output/WriteJsonTest.kt
  31. +1 −1 core/src/test/kotlin/org/evomaster/core/output/WriteXMLTest.kt
  32. +4 −0 core/src/test/kotlin/org/evomaster/core/search/gene/FlatViewTest.kt
  33. +1 −0 core/src/test/kotlin/org/evomaster/core/search/gene/GeneUtilsTest.kt
  34. +2 −1 core/src/test/kotlin/org/evomaster/core/search/gene/{ → sql}/SqlTimestampGeneTest.kt
  35. +2 −0 experiments/src/main/kotlin/org/evomaster/experiments/objects/writer/ObjTestCaseWriter.kt
@@ -4,6 +4,8 @@ import org.evomaster.core.database.schema.Column
import org.evomaster.core.database.schema.Table
import org.evomaster.core.search.Action
import org.evomaster.core.search.gene.*
import org.evomaster.core.search.gene.sql.SqlPrimaryKeyGene
import org.evomaster.core.search.gene.sql.SqlTimestampGene

/**
* An action executed on the database.
@@ -10,6 +10,7 @@ import org.evomaster.core.parser.RegexHandler.createGeneForPostgresSimilarTo
import org.evomaster.core.search.gene.*
import org.evomaster.core.search.gene.regex.DisjunctionListRxGene
import org.evomaster.core.search.gene.regex.RegexGene
import org.evomaster.core.search.gene.sql.*

class DbActionGeneBuilder {

@@ -25,13 +26,7 @@ class DbActionGeneBuilder {

val fk = getForeignKey(table, column)

/*
TODO should nullable columns be wrapped in a OptionalGene?
Maybe not, as need special gene to represent NULL even for
numeric values
*/

val gene = when {
var gene = when {
//TODO handle all constraints and cases
column.autoIncrement ->
SqlAutoIncrementGene(column.name)
@@ -152,11 +147,15 @@ class DbActionGeneBuilder {
}

if (column.primaryKey) {
return SqlPrimaryKeyGene(column.name, table.name, gene, id)
} else {
return gene
gene = SqlPrimaryKeyGene(column.name, table.name, gene, id)
}

if(column.nullable && fk == null){
//FKs handle nullability in their own custom way
gene = SqlNullable(column.name, gene)
}

return gene
}

private fun handleBigIntColumn(column: Column): Gene {
@@ -4,8 +4,9 @@ import org.evomaster.client.java.controller.api.dto.database.operations.Database
import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto
import org.evomaster.client.java.controller.api.dto.database.operations.InsertionEntryDto
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.SqlForeignKeyGene
import org.evomaster.core.search.gene.SqlPrimaryKeyGene
import org.evomaster.core.search.gene.sql.SqlForeignKeyGene
import org.evomaster.core.search.gene.sql.SqlPrimaryKeyGene
import org.evomaster.core.search.gene.sql.SqlWrapperGene


object DbActionTransformer {
@@ -57,15 +58,8 @@ object DbActionTransformer {

val entry = InsertionEntryDto()

if (g is SqlForeignKeyGene) {
handleSqlForeignKey(g, previous, entry, sqlIdMap)
} else if (g is SqlPrimaryKeyGene) {
val k = g.gene
if (k is SqlForeignKeyGene) {
handleSqlForeignKey(k, previous, entry, sqlIdMap)
} else {
entry.printableValue = g.getValueAsPrintableString(targetFormat = null)
}
if(g is SqlWrapperGene && g.getForeignKey() != null){
handleSqlForeignKey(g.getForeignKey()!!, previous, entry, sqlIdMap)
} else {
entry.printableValue = g.getValueAsPrintableString(targetFormat = null)
}
@@ -3,8 +3,8 @@ package org.evomaster.core.database
import org.evomaster.core.Lazy
import org.evomaster.core.search.Action
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.SqlForeignKeyGene
import org.evomaster.core.search.gene.SqlPrimaryKeyGene
import org.evomaster.core.search.gene.sql.SqlForeignKeyGene
import org.evomaster.core.search.gene.sql.SqlPrimaryKeyGene
import org.evomaster.core.search.service.Randomness

object DbActionUtils {
@@ -11,7 +11,7 @@ import org.evomaster.core.database.schema.ForeignKey
import org.evomaster.core.database.schema.Table
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.ImmutableDataHolderGene
import org.evomaster.core.search.gene.SqlPrimaryKeyGene
import org.evomaster.core.search.gene.sql.SqlPrimaryKeyGene
import org.evomaster.dbconstraint.*


@@ -14,10 +14,10 @@ import org.evomaster.core.problem.rest.UsedObjects
import org.evomaster.core.problem.rest.param.BodyParam
import org.evomaster.core.problem.rest.param.HeaderParam
import org.evomaster.core.search.EvaluatedAction
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.ObjectGene
import org.evomaster.core.search.gene.SqlForeignKeyGene
import org.evomaster.core.search.gene.SqlPrimaryKeyGene
import org.evomaster.core.search.gene.*
import org.evomaster.core.search.gene.sql.SqlForeignKeyGene
import org.evomaster.core.search.gene.sql.SqlPrimaryKeyGene
import org.evomaster.core.search.gene.sql.SqlWrapperGene
import javax.ws.rs.core.MediaType


@@ -127,16 +127,8 @@ class TestCaseWriter {
.filter { it.isPrintable() }
.forEach { g ->
when {
g is SqlForeignKeyGene -> {
val line = handleFK(g, dbAction, dbInitialization)
lines.add(line)
}
g is SqlPrimaryKeyGene && g.gene is SqlForeignKeyGene -> {
/*
TODO: this will need to be refactored when Gene system
will have "previousGenes"-based methods on all genes
*/
val line = handleFK(g.gene, dbAction, dbInitialization)
g is SqlWrapperGene && g.getForeignKey() != null -> {
val line = handleFK(g.getForeignKey()!!, dbAction, dbInitialization)
lines.add(line)
}
g is ObjectGene -> {
@@ -1,6 +1,7 @@
package org.evomaster.core.search.gene
package org.evomaster.core.search.gene.sql

import org.evomaster.core.output.OutputFormat
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.service.Randomness


@@ -1,6 +1,7 @@
package org.evomaster.core.search.gene
package org.evomaster.core.search.gene.sql

import org.evomaster.core.output.OutputFormat
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.service.Randomness

/**
@@ -1,6 +1,8 @@
package org.evomaster.core.search.gene
package org.evomaster.core.search.gene.sql

import org.evomaster.core.output.OutputFormat
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.ObjectGene
import org.evomaster.core.search.service.Randomness

class SqlJSONGene(name: String, val objectGene: ObjectGene = ObjectGene(name, fields = listOf())) : Gene(name) {
@@ -0,0 +1,68 @@
package org.evomaster.core.search.gene.sql

import org.evomaster.core.output.OutputFormat
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.service.Randomness
import java.lang.IllegalStateException


class SqlNullable(name: String,
val gene: Gene,
var isPresent: Boolean = true
) : SqlWrapperGene(name) {

init{
if(gene is SqlWrapperGene && gene.getForeignKey() != null){
throw IllegalStateException("SqlNullable should not contain a FK, " +
"as its nullability is handled directly in SqlForeignKeyGene")
}
}

override fun getForeignKey(): SqlForeignKeyGene? {
return null
}

override fun copy(): Gene {
return SqlNullable(name, gene.copy(), isPresent)
}

override fun randomize(randomness: Randomness, forceNewValue: Boolean, allGenes: List<Gene>) {

isPresent = if (!isPresent && forceNewValue)
true
else
randomness.nextBoolean(0.1)

gene.randomize(randomness, forceNewValue, allGenes)
}

override fun getValueAsPrintableString(previousGenes: List<Gene>, mode: String?, targetFormat: OutputFormat?): String {

if (!isPresent) {
return "NULL"
}

return gene.getValueAsPrintableString(previousGenes, mode, targetFormat)
}

override fun copyValueFrom(other: Gene) {
if (other !is SqlNullable) {
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
}
this.isPresent = other.isPresent
this.gene.copyValueFrom(other.gene)
}

override fun containsSameValueAs(other: Gene): Boolean {
if (other !is SqlNullable) {
throw IllegalArgumentException("Invalid gene type ${other.javaClass}")
}
return this.isPresent == other.isPresent &&
this.gene.containsSameValueAs(other.gene)
}

override fun flatView(excludePredicate: (Gene) -> Boolean): List<Gene>{
return if (excludePredicate(this)) listOf(this) else
listOf(this).plus(gene.flatView(excludePredicate))
}
}
@@ -1,6 +1,7 @@
package org.evomaster.core.search.gene
package org.evomaster.core.search.gene.sql

import org.evomaster.core.output.OutputFormat
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.service.Randomness

/**
@@ -1,6 +1,7 @@
package org.evomaster.core.search.gene
package org.evomaster.core.search.gene.sql

import org.evomaster.core.output.OutputFormat
import org.evomaster.core.search.gene.*


class SqlTimestampGene(
@@ -1,6 +1,8 @@
package org.evomaster.core.search.gene
package org.evomaster.core.search.gene.sql

import org.evomaster.core.output.OutputFormat
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.LongGene
import org.evomaster.core.search.service.Randomness
import java.util.*

@@ -0,0 +1,14 @@
package org.evomaster.core.search.gene.sql

import org.evomaster.core.search.gene.Gene

/**
* A gene that either might contain other SQL special genes inside,
* or being one of those genes
*/
abstract class SqlWrapperGene(name: String) : Gene(name){


abstract fun getForeignKey() : SqlForeignKeyGene?

}
@@ -1,6 +1,8 @@
package org.evomaster.core.search.gene
package org.evomaster.core.search.gene.sql

import org.evomaster.core.output.OutputFormat
import org.evomaster.core.search.gene.Gene
import org.evomaster.core.search.gene.ObjectGene
import org.evomaster.core.search.service.Randomness

class SqlXMLGene(name: String, val objectGene: ObjectGene = ObjectGene(name, fields = listOf())) : Gene(name) {
@@ -11,6 +11,8 @@ import org.evomaster.core.search.Individual.GeneFilter.ALL
import org.evomaster.core.search.Individual.GeneFilter.NO_SQL
import org.evomaster.core.search.gene.*
import org.evomaster.core.search.gene.regex.*
import org.evomaster.core.search.gene.sql.SqlForeignKeyGene
import org.evomaster.core.search.gene.sql.SqlNullable
import java.math.BigDecimal
import java.math.RoundingMode

@@ -6,6 +6,9 @@ import org.evomaster.core.database.schema.ColumnDataType
import org.evomaster.core.database.schema.ForeignKey
import org.evomaster.core.database.schema.Table
import org.evomaster.core.search.gene.*
import org.evomaster.core.search.gene.sql.SqlAutoIncrementGene
import org.evomaster.core.search.gene.sql.SqlForeignKeyGene
import org.evomaster.core.search.gene.sql.SqlPrimaryKeyGene
import org.evomaster.core.search.service.Randomness
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
@@ -5,6 +5,9 @@ import org.evomaster.client.java.controller.api.dto.database.operations.QueryRes
import org.evomaster.client.java.controller.db.SqlScriptRunner
import org.evomaster.client.java.controller.internal.db.SchemaExtractor
import org.evomaster.core.search.gene.*
import org.evomaster.core.search.gene.sql.SqlAutoIncrementGene
import org.evomaster.core.search.gene.sql.SqlPrimaryKeyGene
import org.evomaster.core.search.gene.sql.SqlTimestampGene
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach

0 comments on commit 989a116

Please sign in to comment.
You can’t perform that action at this time.