Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ class SqlBinaryStringGene(

private val binaryArrayGene: ArrayGene<IntegerGene> = ArrayGene(name, template = IntegerGene(name, min = 0, max = 255), minSize = minSize, maxSize = maxSize),

val databaseType: DatabaseType = DatabaseType.POSTGRES
databaseType: DatabaseType = DatabaseType.POSTGRES

) : CompositeFixedGene(name, mutableListOf( binaryArrayGene)) {

var databaseType: DatabaseType = databaseType
private set

companion object {
val log: Logger = LoggerFactory.getLogger(SqlBinaryStringGene::class.java)

Expand Down Expand Up @@ -80,7 +83,9 @@ class SqlBinaryStringGene(
if (other !is SqlBinaryStringGene) {
return false
}
return binaryArrayGene.unsafeCopyValueFrom(other.binaryArrayGene)
this.databaseType = other.databaseType
val ok = binaryArrayGene.unsafeCopyValueFrom(other.binaryArrayGene)
return ok
}


Expand All @@ -99,4 +104,4 @@ class SqlBinaryStringGene(
return false
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SqlMultidimensionalArrayGene<T>(
* The database type of the column where the gene is inserted.
* By default, the database type is POSTGRES
*/
val databaseType: DatabaseType = DatabaseType.POSTGRES,
databaseType: DatabaseType = DatabaseType.POSTGRES,
/**
* The type for this array. Every time we create a new element to add, it has to be based
* on this template
Expand All @@ -46,6 +46,9 @@ class SqlMultidimensionalArrayGene<T>(
private val maxDimensionSize: Int = ArrayGene.MAX_SIZE
) : CompositeGene(name, mutableListOf()) where T : Gene {

var databaseType: DatabaseType = databaseType
private set

/**
* Stores what is the size of each dimension.
* It is used to check that the multidimensional array
Expand Down Expand Up @@ -335,6 +338,7 @@ class SqlMultidimensionalArrayGene<T>(
val elements = gene.getViewOfChildren().mapNotNull { it.copy() as? T }.toMutableList()
addChildren(elements)
this.dimensionSizes = gene.dimensionSizes
this.databaseType = gene.databaseType
return true
}

Expand Down Expand Up @@ -412,4 +416,4 @@ class SqlMultidimensionalArrayGene<T>(
return false
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SqlGeometryCollectionGene(
/**
* The database type of the source column for this gene
*/
val databaseType: DatabaseType = DatabaseType.H2,
var databaseType: DatabaseType = DatabaseType.H2,
template: Gene,
val elements: ArrayGene<Gene> = ArrayGene(
name = "points",
Expand Down Expand Up @@ -85,6 +85,7 @@ class SqlGeometryCollectionGene(
if (other !is SqlGeometryCollectionGene) {
return false
}
this.databaseType = other.databaseType
return this.elements.unsafeCopyValueFrom(other.elements)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ class SqlMultiPathGene(
/**
* The database type of the source column for this gene
*/
val databaseType: DatabaseType = DatabaseType.MYSQL,
databaseType: DatabaseType = DatabaseType.MYSQL,
val paths: ArrayGene<SqlPathGene> = ArrayGene(
name = "points",
minSize = 1,
template = SqlPathGene("p", databaseType = databaseType)
)
) : CompositeFixedGene(name, mutableListOf(paths)) {

var databaseType: DatabaseType = databaseType
private set

companion object {
val log: Logger = LoggerFactory.getLogger(SqlMultiPathGene::class.java)
}
Expand Down Expand Up @@ -92,6 +95,7 @@ class SqlMultiPathGene(
if (other !is SqlMultiPathGene) {
return false
}
this.databaseType = other.databaseType
return this.paths.unsafeCopyValueFrom(other.paths)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ class SqlMultiPointGene(
/**
* The database type of the source column for this gene
*/
val databaseType: DatabaseType = DatabaseType.H2,
databaseType: DatabaseType = DatabaseType.H2,
val points: ArrayGene<SqlPointGene> = ArrayGene(
name = "points",
minSize = 1, //looks like insertion fails if 0, due to "bad" syntax
template = SqlPointGene("p", databaseType = databaseType)
)
) : CompositeFixedGene(name, mutableListOf(points)) {

var databaseType: DatabaseType = databaseType
private set

init {
if (databaseType!=DatabaseType.H2 && databaseType!=DatabaseType.MYSQL) {
IllegalArgumentException("Cannot create a SqlMultiPointGene with database type ${databaseType}")
Expand Down Expand Up @@ -96,14 +99,15 @@ class SqlMultiPointGene(
if (other !is SqlMultiPointGene) {
return false
}
this.databaseType = other.databaseType
return this.points.unsafeCopyValueFrom(other.points)
}

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

override fun customShouldApplyShallowMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SqlMultiPolygonGene(
/**
* The database type of the source column for this gene
*/
val databaseType: DatabaseType = DatabaseType.H2,
databaseType: DatabaseType = DatabaseType.H2,
val polygons: ArrayGene<SqlPolygonGene> = ArrayGene(
name = "polygons",
minSize = 1,
Expand All @@ -32,6 +32,9 @@ class SqlMultiPolygonGene(
databaseType = databaseType))
) : CompositeFixedGene(name, mutableListOf(polygons)) {

var databaseType: DatabaseType = databaseType
private set

companion object {
val log: Logger = LoggerFactory.getLogger(SqlMultiPolygonGene::class.java)
}
Expand Down Expand Up @@ -97,14 +100,15 @@ class SqlMultiPolygonGene(
if (other !is SqlMultiPolygonGene) {
return false
}
this.databaseType = other.databaseType
return this.polygons.unsafeCopyValueFrom(other.polygons)
}

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

override fun customShouldApplyShallowMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ import org.slf4j.LoggerFactory
*/
class SqlPathGene(
name: String,
val databaseType: DatabaseType = DatabaseType.POSTGRES,
databaseType: DatabaseType = DatabaseType.POSTGRES,
val points: ArrayGene<SqlPointGene> = ArrayGene(
name = "points",
// paths are lists of at least 2 points
minSize = 2,
template = SqlPointGene("p", databaseType = databaseType))
) : CompositeFixedGene(name, mutableListOf(points)) {

var databaseType: DatabaseType = databaseType
private set

companion object {
val log: Logger = LoggerFactory.getLogger(SqlPathGene::class.java)
}
Expand Down Expand Up @@ -93,7 +96,9 @@ class SqlPathGene(
if (other !is SqlPathGene) {
return false
}
return this.points.unsafeCopyValueFrom(other.points)
this.databaseType = other.databaseType
val ok = this.points.unsafeCopyValueFrom(other.points)
return ok
}

override fun containsSameValueAs(other: Gene): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ class SqlPointGene(
name: String,
val x: FloatGene = FloatGene(name = "x"),
val y: FloatGene = FloatGene(name = "y"),
val databaseType: DatabaseType = DatabaseType.POSTGRES
databaseType: DatabaseType = DatabaseType.POSTGRES
) : CompositeFixedGene(name, mutableListOf(x, y)) {

var databaseType: DatabaseType = databaseType
private set

companion object {
val log: Logger = LoggerFactory.getLogger(SqlPointGene::class.java)
}
Expand Down Expand Up @@ -71,8 +74,10 @@ class SqlPointGene(
if (other !is SqlPointGene) {
return false
}
return this.x.unsafeCopyValueFrom(other.x)
this.databaseType = other.databaseType
val ok = this.x.unsafeCopyValueFrom(other.x)
&& this.y.unsafeCopyValueFrom(other.y)
return ok
}

override fun containsSameValueAs(other: Gene): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import java.awt.geom.Line2D
*/
class SqlPolygonGene(
name: String,
val databaseType: DatabaseType = DatabaseType.POSTGRES,
databaseType: DatabaseType = DatabaseType.POSTGRES,
val minLengthOfPolygonRing: Int = 2,
val onlyNonIntersectingPolygons: Boolean = false,
val points: ArrayGene<SqlPointGene> = ArrayGene(
Expand All @@ -30,6 +30,9 @@ class SqlPolygonGene(
template = SqlPointGene("p", databaseType = databaseType))
) : CompositeFixedGene(name, mutableListOf(points)) {

var databaseType: DatabaseType = databaseType
private set

companion object {
val log: Logger = LoggerFactory.getLogger(SqlPolygonGene::class.java)
}
Expand Down Expand Up @@ -180,7 +183,9 @@ class SqlPolygonGene(
if (other !is SqlPolygonGene) {
return false
}
return this.points.unsafeCopyValueFrom(other.points)
this.databaseType = other.databaseType
val ok = this.points.unsafeCopyValueFrom(other.points)
return ok
}

override fun containsSameValueAs(other: Gene): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.evomaster.core.search.gene.sql

import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType
import org.evomaster.core.search.gene.numeric.IntegerGene
import org.evomaster.core.search.gene.collection.ArrayGene
import org.evomaster.core.search.gene.utils.GeneUtils.SINGLE_APOSTROPHE_PLACEHOLDER
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class SqlBinaryStringGeneTest {

@Test
fun testPrintEmptyBinaryString() {
val gene = SqlBinaryStringGene("bin", databaseType = DatabaseType.POSTGRES)
assertEquals("\"\\x\"", gene.getValueAsPrintableString())
}

@Test
fun testPrintNonEmptyBinaryStringPostgres() {
val gene = SqlBinaryStringGene("bin", databaseType = DatabaseType.POSTGRES)
val arrayGene = gene.getViewOfChildren()[0] as ArrayGene<IntegerGene>
arrayGene.addElement(IntegerGene("g0", value = 10)) // 0a
arrayGene.addElement(IntegerGene("g1", value = 255)) // ff
assertEquals("\"\\x0aff\"", gene.getValueAsPrintableString())
}

@Test
fun testPrintNonEmptyBinaryStringH2() {
val gene = SqlBinaryStringGene("bin", databaseType = DatabaseType.H2)
val arrayGene = gene.getViewOfChildren()[0] as ArrayGene<IntegerGene>
arrayGene.addElement(IntegerGene("g0", value = 10)) // 0a
arrayGene.addElement(IntegerGene("g1", value = 255)) // ff
assertEquals("X${SINGLE_APOSTROPHE_PLACEHOLDER}0aff${SINGLE_APOSTROPHE_PLACEHOLDER}", gene.getValueAsPrintableString())
}

@Test
fun testUnsafeCopyValueFromModifiesDatabaseType() {
val gene1 = SqlBinaryStringGene(
name = "bin1",
databaseType = DatabaseType.POSTGRES
)
val gene2 = SqlBinaryStringGene(
name = "bin2",
databaseType = DatabaseType.MYSQL
)

val ok = gene1.unsafeCopyValueFrom(gene2)

assertTrue(ok)
assertEquals(DatabaseType.MYSQL, gene1.databaseType)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,17 @@ class SqlMultidimensionalArrayGeneTest {
assertTrue(gene.isLocallyValid())
}

}
@Test
fun testCopyValuesFromWithDifferentDatabaseTypes() {
val sourceArray = sampleTwoDimensionalArrayOfIntegerGenes(2, 3, databaseType = DatabaseType.H2)
val targetArray = sampleTwoDimensionalArrayOfIntegerGenes(2, 3, databaseType = DatabaseType.POSTGRES)

assertEquals(DatabaseType.H2, sourceArray.databaseType)
assertEquals(DatabaseType.POSTGRES, targetArray.databaseType)

assertTrue(targetArray.unsafeCopyValueFrom(sourceArray))

assertEquals(DatabaseType.H2, targetArray.databaseType)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,15 @@ class SqlMultiPathGeneTest {
assertEquals("MULTILINESTRING(LINESTRING(POINT(0.0, 1.0), POINT(1.0, 1.0), POINT(0.0, 0.0)), LINESTRING(POINT(1.0, 1.0), POINT(0.0, 1.0), POINT(1.0, 0.0)))",gene.getValueAsPrintableString())
}

@Test
fun testUnsafeCopyValueFrom() {
val gene1 = SqlMultiPathGene(name = "multilinestring", databaseType = DatabaseType.H2)
val gene2 = SqlMultiPathGene(name = "multilinestring", databaseType = DatabaseType.MYSQL)

gene1.unsafeCopyValueFrom(gene2)

assertEquals(DatabaseType.MYSQL, gene1.databaseType)
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,27 @@ class SqlMultiPointGeneTest {
assertEquals("MULTIPOINT(POINT(0.0, 0.0), POINT(1.0, 1.0))", gene.getValueAsPrintableString())
}

@Test
fun testUnsafeCopyValueFromCopiesDatabaseType() {
val geneH2 = SqlMultiPointGene("geneH2", databaseType = DatabaseType.H2)
val geneMySQL = SqlMultiPointGene("geneMySQL", databaseType = DatabaseType.MYSQL)

geneMySQL.unsafeCopyValueFrom(geneH2)

assertEquals(DatabaseType.H2, geneMySQL.databaseType)
}

@Test
fun testContainsSameValueAsChecksDatabaseType() {
val geneH2 = SqlMultiPointGene("geneH2", databaseType = DatabaseType.H2)
val geneMySQL = SqlMultiPointGene("geneMySQL", databaseType = DatabaseType.MYSQL)

// They have same points (default empty) but different databaseType
assertEquals(false, geneH2.containsSameValueAs(geneMySQL))

geneMySQL.unsafeCopyValueFrom(geneH2)
assertEquals(true, geneH2.containsSameValueAs(geneMySQL))
}


}
}
Loading
Loading