-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #615 from LeeJejune/feat/round
Support Arithmetic function ROUND
- Loading branch information
Showing
10 changed files
with
257 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/RoundDslTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.linecorp.kotlinjdsl.dsl.jpql.expression | ||
|
||
import com.linecorp.kotlinjdsl.dsl.jpql.entity.book.Book | ||
import com.linecorp.kotlinjdsl.dsl.jpql.queryPart | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths | ||
import org.assertj.core.api.WithAssertions | ||
import org.junit.jupiter.api.Test | ||
import java.math.BigDecimal | ||
|
||
class RoundDslTest : WithAssertions { | ||
private val expression1 = Paths.path(Book::salePrice) | ||
private val intExpression1: Expression<Int> = Expressions.value(3) | ||
private val doubleExpression1: Expression<Double> = Expressions.value(3.0) | ||
private val int1 = 3 | ||
private val double1 = 3.0 | ||
|
||
@Test | ||
fun `round() with a BigDecimalExpression`() { | ||
// when | ||
val expression = queryPart { | ||
round(expression1, intExpression1) | ||
}.toExpression() | ||
|
||
val actual: Expression<BigDecimal> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.round( | ||
value = expression1, | ||
scale = intExpression1, | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `round() with a BigDecimalProperty`() { | ||
// when | ||
val expression = queryPart { | ||
round(Book::price, Expressions.value(int1)) | ||
}.toExpression() | ||
|
||
val actual: Expression<BigDecimal> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.round( | ||
value = Paths.path(Book::price), | ||
scale = Expressions.value(int1), | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `round() with a DoubleExpression`() { | ||
// when | ||
val expression = queryPart { | ||
round(doubleExpression1, intExpression1) | ||
}.toExpression() | ||
|
||
val actual: Expression<Double> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.round( | ||
value = doubleExpression1, | ||
scale = intExpression1, | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `round() with a DoubleProperty`() { | ||
// when | ||
val expression = queryPart { | ||
round(Expressions.value(double1), Expressions.value(int1)) | ||
}.toExpression() | ||
|
||
val actual: Expression<Double> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.round( | ||
value = Expressions.value(double1), | ||
scale = Expressions.value(int1), | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlRound.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression | ||
|
||
/** | ||
* Expression that calculates the rounding of a numeric [value] to a specified [scale]. | ||
*/ | ||
@Internal | ||
data class JpqlRound<T: Number> internal constructor( | ||
val value: Expression<T>, | ||
val scale: Expression<Int>, | ||
) : Expression<T> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...rc/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlRoundSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlRound | ||
import com.linecorp.kotlinjdsl.render.RenderContext | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter | ||
import kotlin.reflect.KClass | ||
|
||
@Internal | ||
class JpqlRoundSerializer : JpqlSerializer<JpqlRound<*>>{ | ||
override fun handledType(): KClass<JpqlRound<*>> { | ||
return JpqlRound::class | ||
} | ||
|
||
override fun serialize(part: JpqlRound<*>, writer: JpqlWriter, context: RenderContext) { | ||
val delegate = context.getValue(JpqlRenderSerializer) | ||
|
||
writer.write("ROUND") | ||
|
||
writer.writeParentheses { | ||
delegate.serialize(part.value, writer, context) | ||
|
||
writer.write(", ") | ||
delegate.serialize(part.scale, writer, context) | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...est/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlRoundSerializerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlRound | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths | ||
import com.linecorp.kotlinjdsl.render.TestRenderContext | ||
import com.linecorp.kotlinjdsl.render.jpql.entity.book.Book | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializerTest | ||
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.verifySequence | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
@JpqlSerializerTest | ||
class JpqlRoundSerializerTest { | ||
private val sut = JpqlRoundSerializer() | ||
|
||
@MockK | ||
private lateinit var writer: JpqlWriter | ||
|
||
@MockK | ||
private lateinit var serializer: JpqlRenderSerializer | ||
|
||
private val expression1 = Paths.path(Book::price) | ||
private val expression2 = Expressions.value(3) | ||
|
||
@Test | ||
fun handledType() { | ||
// when | ||
val actual = sut.handledType() | ||
|
||
// then | ||
assertThat(actual).isEqualTo(JpqlRound::class) | ||
} | ||
@Test | ||
fun serialize() { | ||
// given | ||
val part = Expressions.round( | ||
value = expression1, | ||
scale = expression2 | ||
) | ||
val context = TestRenderContext(serializer) | ||
|
||
// when | ||
sut.serialize(part as JpqlRound<*>, writer, context) | ||
|
||
// then | ||
verifySequence { | ||
writer.write("ROUND") | ||
writer.writeParentheses(any()) | ||
serializer.serialize(expression1, writer, context) | ||
writer.write(", ") | ||
serializer.serialize(expression2, writer, context) | ||
} | ||
} | ||
} |