Skip to content
Snippets Groups Projects
Commit b58f1f77 authored by npedot's avatar npedot
Browse files

adds test to schema cmd parser

parent 58accf31
No related branches found
No related tags found
No related merge requests found
......@@ -182,8 +182,9 @@ class Schema () {
}
fun addFunctional(tableName:String, setExpression: String): Schema {
constraints().addAll(SchemaCmdParser
.parseFunctionals(tableName, setExpression))
val constraints = constraints()
constraints.addAll(SchemaCmdParser
.parseFunctionals(constraints.size, tableName, setExpression))
return this
}
......@@ -229,6 +230,7 @@ class Schema () {
return this
}
// FIXME Use SchemaCmdParser
fun addForeignKey(commandArgs:String):Schema {
val source:String = commandArgs.split("-->")[0]
val target:String = commandArgs.split("-->")[1]
......@@ -251,6 +253,7 @@ class Schema () {
return this
}
// FIXME Use SchemaCmdParser
fun addDoubleInc(commandArgs:String):Schema {
val source:String = commandArgs.split("-->")[0]
val target:String = commandArgs.split("-->")[1]
......
......@@ -2,6 +2,7 @@ package unibz.cs.semint.kprime.domain.ddl
object SchemaCmdParser {
// table:a,b,c
fun parseTable(commandArgs: String): Table {
val tableName: String = commandArgs.split(":")[0]
val attributes = commandArgs.split(":")[1].split(",")
......@@ -11,32 +12,62 @@ object SchemaCmdParser {
return table
}
fun parseFunctionals(tableName: String,setExpression: String): Set<Constraint> {
return parseConstraint(setExpression, tableName, Constraint.TYPE.FUNCTIONAL.name)
// table:a-->b,c
fun parseFunctionals(index: Int, tableName: String,setExpression: String): Set<Constraint> {
return parseConstraint(index, setExpression, tableName, Constraint.TYPE.FUNCTIONAL.name)
}
fun parseMultivalued(tableName: String,setExpression: String): Set<Constraint> {
return parseConstraint(setExpression, tableName, Constraint.TYPE.MULTIVALUED.name)
// table:a->>b,c
fun parseMultivalued(index: Int, tableName: String, arrowExpressions: String): Set<Constraint> {
return parseConstraint(index, arrowExpressions, tableName, Constraint.TYPE.MULTIVALUED.name)
}
fun parseInclusion(tableName: String,setExpression: String): Set<Constraint> {
return parseConstraint(setExpression, tableName, Constraint.TYPE.INCLUSION.name)
}
fun parseDoubleInclusion(tableName: String,setExpression: String): Set<Constraint> {
return parseConstraint(setExpression, tableName, Constraint.TYPE.DOUBLE_INCLUSION.name)
}
private fun parseConstraint(setExpression: String, tableName: String, type:String): Set<Constraint> {
val constraintsToAdd = Constraint.set(setExpression)
// arrowWxpressions = "a,b-->c,d;x-->y"
private fun parseConstraint(index: Int, arrowExpressions: String, tableName: String, type:String): Set<Constraint> {
val constraintsToAdd = Constraint.set(arrowExpressions)
var indexToAdd = index
for (constraint in constraintsToAdd) {
constraint.name = "$tableName.$type"
constraint.name = "$tableName.$type$indexToAdd"
constraint.type = type
constraint.source.table = tableName
constraint.target.table = tableName
indexToAdd++
}
return constraintsToAdd
}
// table1:a-->table2:b
fun parseInclusion(index: Int, constraintExpression: String): Constraint {
val split = constraintExpression.split("-->")
val tableSourceString = split[0]
val tableTargetString = split[1]
val tableSource = parseTable(tableSourceString)
val tableTarget = parseTable(tableTargetString)
val type = Constraint.TYPE.INCLUSION.name
return buildConstraint(tableSource, tableTarget, type, index)
}
// table1:a<->table2:b
fun parseDoubleInclusion(index: Int, constraintExpression: String): Constraint {
val split = constraintExpression.split("<->")
val tableSourceString = split[0]
val tableTargetString = split[1]
val tableSource = parseTable(tableSourceString)
val tableTarget = parseTable(tableTargetString)
val type = Constraint.TYPE.DOUBLE_INCLUSION.name
return buildConstraint(tableSource, tableTarget, type, index)
}
private fun buildConstraint(tableSource: Table, tableTarget: Table, type: String, index: Int): Constraint {
var constraint = Constraint()
constraint.name = "${tableSource.name}_${tableTarget.name}.$type$index"
constraint.type = type
constraint.source.table = tableSource.name
constraint.source.columns = tableSource.columns
constraint.target.table = tableTarget.name
constraint.target.columns = tableTarget.columns
return constraint
}
}
\ No newline at end of file
package unibz.cs.semint.kprime.domain.ddl
import org.junit.Test
import kotlin.test.assertEquals
class SchemaCmdParserTest {
@Test
fun test_parse_functionals() {
// Given
// When
val constraints = SchemaCmdParser.parseFunctionals(1,"table1","A,B-->C; C-->B")
// Then
assertEquals(2,constraints.size)
val functionals = constraints.toList()
assertEquals("table1.FUNCTIONAL1", functionals[0].name)
assertEquals("table1", functionals[0].source.table)
assertEquals("[C]", functionals[0].source.columns.toString())
assertEquals("table1", functionals[0].target.table)
assertEquals("[B]", functionals[0].target.columns.toString())
assertEquals("table1.FUNCTIONAL2", functionals[1].name)
assertEquals("table1", functionals[1].source.table)
assertEquals("[A, B]", functionals[1].source.columns.toString())
assertEquals("table1", functionals[1].target.table)
assertEquals("[C]", functionals[1].target.columns.toString())
}
@Test
fun test_parse_multivalueds() {
// Given
// When
val constraints = SchemaCmdParser.parseMultivalued(1,"table1", "A,B-->C; C-->B")
// Then
assertEquals(2, constraints.size)
val functionals = constraints.toList()
assertEquals("table1.MULTIVALUED1", functionals[0].name)
assertEquals("table1", functionals[0].source.table)
assertEquals("[C]", functionals[0].source.columns.toString())
assertEquals("table1", functionals[0].target.table)
assertEquals("[B]", functionals[0].target.columns.toString())
assertEquals("table1.MULTIVALUED2", functionals[1].name)
assertEquals("table1", functionals[1].source.table)
assertEquals("[A, B]", functionals[1].source.columns.toString())
assertEquals("table1", functionals[1].target.table)
assertEquals("[C]", functionals[1].target.columns.toString())
}
@Test
fun test_parse_inclusion() {
// Given
// When
val constraint = SchemaCmdParser.parseInclusion(1,"table0:A,B-->table1:C")
// Then
assertEquals("table0_table1.INCLUSION1", constraint.name)
assertEquals("table0", constraint.source.table)
assertEquals("[A, B]", constraint.source.columns.toString())
assertEquals("table1", constraint.target.table)
assertEquals("[C]", constraint.target.columns.toString())
}
@Test
fun test_parse_double_inclusion() {
// Given
// When
val constraint = SchemaCmdParser.parseDoubleInclusion(1,"table0:A,B<->table1:C")
// Then
assertEquals("table0_table1.DOUBLE_INCLUSION1", constraint.name)
assertEquals("table0", constraint.source.table)
assertEquals("[A, B]", constraint.source.columns.toString())
assertEquals("table1", constraint.target.table)
assertEquals("[C]", constraint.target.columns.toString())
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment