-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement floor function expression
- Loading branch information
1 parent
347096d
commit 4e58c3f
Showing
8 changed files
with
182 additions
and
0 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
48 changes: 48 additions & 0 deletions
48
dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/FloorDslTest.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,48 @@ | ||
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 FloorDslTest : WithAssertions { | ||
private val expression1 = Paths.path(Book::salePrice) | ||
|
||
@Test | ||
fun `floor() with a property`() { | ||
// when | ||
val expression = queryPart { | ||
floor(Book::price) | ||
}.toExpression() | ||
|
||
val actual: Expression<BigDecimal> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.floor( | ||
value = Paths.path(Book::price), | ||
) | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `floor() with a expression`() { | ||
// when | ||
val expression = queryPart { | ||
floor(expression1) | ||
}.toExpression() | ||
|
||
val actual: Expression<BigDecimal> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.floor( | ||
value = expression1, | ||
) | ||
|
||
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
12 changes: 12 additions & 0 deletions
12
...jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlFloor.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,12 @@ | ||
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression | ||
|
||
/** | ||
* Expression that applies the floor function to [value]. | ||
*/ | ||
@Internal | ||
data class JpqlFloor<T : Number> internal constructor( | ||
val value: Expression<T>, | ||
) : 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
26 changes: 26 additions & 0 deletions
26
...rc/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlFloorSerializer.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,26 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFloor | ||
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 JpqlFloorSerializer : JpqlSerializer<JpqlFloor<*>> { | ||
override fun handledType(): KClass<JpqlFloor<*>> { | ||
return JpqlFloor::class | ||
} | ||
|
||
override fun serialize(part: JpqlFloor<*>, writer: JpqlWriter, context: RenderContext) { | ||
val delegate = context.getValue(JpqlRenderSerializer) | ||
|
||
writer.write("FLOOR") | ||
|
||
writer.writeParentheses { | ||
delegate.serialize(part.value, writer, context) | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...est/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlFloorSerializerTest.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,55 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFloor | ||
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.WithAssertions | ||
import org.junit.jupiter.api.Test | ||
|
||
@JpqlSerializerTest | ||
class JpqlFloorSerializerTest : WithAssertions { | ||
private val sut = JpqlFloorSerializer() | ||
|
||
@MockK | ||
private lateinit var writer: JpqlWriter | ||
|
||
@MockK | ||
private lateinit var serializer: JpqlRenderSerializer | ||
|
||
private val expression1 = Paths.path(Book::price) | ||
|
||
@Test | ||
fun handledType() { | ||
// when | ||
val actual = sut.handledType() | ||
|
||
// then | ||
assertThat(actual).isEqualTo(JpqlFloor::class) | ||
} | ||
|
||
@Test | ||
fun serialize() { | ||
// given | ||
val part = Expressions.floor( | ||
value = expression1, | ||
) | ||
val context = TestRenderContext(serializer) | ||
|
||
// when | ||
sut.serialize(part as JpqlFloor<*>, writer, context) | ||
|
||
// then | ||
verifySequence { | ||
writer.write("FLOOR") | ||
writer.writeParentheses(any()) | ||
serializer.serialize(expression1, writer, context) | ||
} | ||
} | ||
} |