Skip to content

Commit

Permalink
DOPE-224: added atomic extensions, removed inheritance from SetAssign…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
martinagallati-ergon committed Aug 9, 2024
1 parent 3a7dca6 commit 2fb367c
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ interface IUpdateSetClause : IUpdateUnsetClause {

interface IUpdateUseKeysClause : IUpdateSetClause {
fun <T : ValidType> set(field: Field<T>, value: TypeExpression<T>) = SetClause(field.to(value), parentClause = this)
fun set(field: Field<NumberType>, value: Number) = SetClause(field.to(value.toDopeType()), parentClause = this)
fun set(field: Field<StringType>, value: String) = SetClause(field.to(value.toDopeType()), parentClause = this)
fun set(field: Field<BooleanType>, value: Boolean) = SetClause(field.to(value.toDopeType()), parentClause = this)
}

interface IUpdateClause : IUpdateUseKeysClause {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import ch.ergon.dope.DopeQuery
import ch.ergon.dope.resolvable.clause.IUpdateSetClause
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.resolvable.expression.unaliased.type.toDopeType
import ch.ergon.dope.resolvable.formatToQueryString
import ch.ergon.dope.validtype.BooleanType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.StringType
import ch.ergon.dope.validtype.ValidType

class SetClause(
Expand All @@ -31,13 +35,37 @@ class SetClause(

fun <T : ValidType> set(field: Field<T>, value: TypeExpression<T>) =
SetClause(this.fieldAssignment, *this.fieldAssignments, field.to(value), parentClause = this.parentClause)

fun set(field: Field<NumberType>, value: Number) =
SetClause(
this.fieldAssignment,
*this.fieldAssignments,
field.to(value.toDopeType()),
parentClause = this.parentClause,
)

fun set(field: Field<StringType>, value: String) =
SetClause(
this.fieldAssignment,
*this.fieldAssignments,
field.to(value.toDopeType()),
parentClause = this.parentClause,
)

fun set(field: Field<BooleanType>, value: Boolean) =
SetClause(
this.fieldAssignment,
*this.fieldAssignments,
field.to(value.toDopeType()),
parentClause = this.parentClause,
)
}

class SetAssignment<T : ValidType>(
private val field: Field<T>,
private val value: TypeExpression<T>,
) : TypeExpression<T> {
override fun toDopeQuery(): DopeQuery {
) {
fun toDopeQuery(): DopeQuery {
val fieldDopeQuery = field.toDopeQuery()
val valueDopeQuery = value.toDopeQuery()
return DopeQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,42 @@ class SetClauseTest : ParameterDependentTest {
assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support set clause extension with number`() {
val numberField = someNumberField()
val numberValue = someNumber()
val parentClause = someUpdateClause()
val expected = SetClause(numberField.to(numberValue.toDopeType()), parentClause = parentClause)

val actual = parentClause.set(numberField, numberValue)

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support set clause extension with string`() {
val stringField = someStringField()
val stringValue = someString()
val parentClause = someUpdateClause()
val expected = SetClause(stringField.to(stringValue.toDopeType()), parentClause = parentClause)

val actual = parentClause.set(stringField, stringValue)

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support set clause extension with boolean`() {
val booleanField = someBooleanField()
val booleanValue = someBoolean()
val parentClause = someUpdateClause()
val expected = SetClause(booleanField.to(booleanValue.toDopeType()), parentClause = parentClause)

val actual = parentClause.set(booleanField, booleanValue)

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support set clause extension with multiple fields`() {
val stringField = someStringField()
Expand All @@ -116,4 +152,31 @@ class SetClauseTest : ParameterDependentTest {

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support set clause extension with multiple fields with atomic types`() {
val field = someStringField()
val value = someStringField()
val stringField = someStringField()
val stringValue = someString()
val numberField = someNumberField()
val numberValue = someNumber()
val booleanField = someBooleanField()
val booleanValue = someBoolean()
val parentClause = someUpdateClause()
val expected = SetClause(
field.to(value),
stringField.to(stringValue.toDopeType()),
numberField.to(numberValue.toDopeType()),
booleanField.to(booleanValue.toDopeType()),
parentClause = parentClause,
)

val actual = parentClause.set(field, value)
.set(stringField, stringValue)
.set(numberField, numberValue)
.set(booleanField, booleanValue)

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,41 @@ fun SetClause.set(
value: TypeExpression<ArrayType<BooleanType>>,
) = this.set(field.toDopeType(), value)

@JvmName("setCMNumberFieldToNumber")
fun IUpdateUseKeysClause.set(
field: CMField<out Number>,
value: Number,
) = set(field.toDopeType(), value)

@JvmName("setCMNumberFieldToNumber")
fun SetClause.set(
field: CMField<out Number>,
value: Number,
) = this.set(field.toDopeType(), value)

@JvmName("setCMStringFieldToString")
fun IUpdateUseKeysClause.set(
field: CMField<String>,
value: String,
) = set(field.toDopeType(), value)

@JvmName("setCMStringFieldToString")
fun SetClause.set(
field: CMField<String>,
value: String,
) = this.set(field.toDopeType(), value)

@JvmName("setCMBooleanFieldToBoolean")
fun IUpdateUseKeysClause.set(
field: CMField<Boolean>,
value: Boolean,
) = set(field.toDopeType(), value)

@JvmName("setCMBooleanFieldToBoolean")
fun SetClause.set(
field: CMField<Boolean>,
value: Boolean,
) = this.set(field.toDopeType(), value)

fun IUpdateClause.useKeys(useKeys: CMField<String>) = useKeys(useKeys.toDopeType())
fun IUpdateClause.useKeys(useKeys: CMList<String>) = useKeys(useKeys.toDopeType())
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,96 @@ class UpdateClauseTest {
assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support update set CMField number to number`() {
val field = someCMNumberField()
val value = someNumber()
val parentClause = someUpdate()
val expected = SetClause(field.toDopeType().to(value.toDopeType()), parentClause = parentClause)

val actual = parentClause.set(field, value)

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support update additional set CMField number to number`() {
val numberField = someCMNumberField()
val numberValue = someNumber()
val stringField = someCMStringField()
val stringValue = someCMStringField()
val parentClause = someUpdate()
val expected = SetClause(
stringField.toDopeType().to(stringValue.toDopeType()),
numberField.toDopeType().to(numberValue.toDopeType()),
parentClause = parentClause,
)

val actual = parentClause.set(stringField, stringValue).set(numberField, numberValue)

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support update set CMField string to string`() {
val field = someCMStringField()
val value = someString()
val parentClause = someUpdate()
val expected = SetClause(field.toDopeType().to(value.toDopeType()), parentClause = parentClause)

val actual = parentClause.set(field, value)

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support update additional set CMField string to string`() {
val numberField = someCMNumberField()
val numberValue = someCMNumberField()
val stringField = someCMStringField()
val stringValue = someString()
val parentClause = someUpdate()
val expected = SetClause(
numberField.toDopeType().to(numberValue.toDopeType()),
stringField.toDopeType().to(stringValue.toDopeType()),
parentClause = parentClause,
)

val actual = parentClause.set(numberField, numberValue).set(stringField, stringValue)

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support update set CMField boolean to boolean`() {
val field = someCMBooleanField()
val value = someBoolean()
val parentClause = someUpdate()
val expected = SetClause(field.toDopeType().to(value.toDopeType()), parentClause = parentClause)

val actual = parentClause.set(field, value)

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support update additional set CMField boolean to boolean`() {
val booleanField = someCMBooleanField()
val booleanValue = someBoolean()
val stringField = someCMStringField()
val stringValue = someCMStringField()
val parentClause = someUpdate()
val expected = SetClause(
stringField.toDopeType().to(stringValue.toDopeType()),
booleanField.toDopeType().to(booleanValue.toDopeType()),
parentClause = parentClause,
)

val actual = parentClause.set(stringField, stringValue).set(booleanField, booleanValue)

assertEquals(expected.toDopeQuery(), actual.toDopeQuery())
}

@Test
fun `should support update unset with CM`() {
val stringField = someCMStringField()
Expand Down

0 comments on commit 2fb367c

Please sign in to comment.