Skip to content

Commit 39a59df

Browse files
committed
Kotlin Having Tests
1 parent a1d92ef commit 39a59df

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/test/kotlin/examples/kotlin/mybatis3/general/KGroupingTest.kt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ import examples.kotlin.mybatis3.general.FooDynamicSqlSupport.B
2020
import examples.kotlin.mybatis3.general.FooDynamicSqlSupport.C
2121
import examples.kotlin.mybatis3.general.FooDynamicSqlSupport.foo
2222
import org.assertj.core.api.Assertions.assertThat
23+
import org.assertj.core.api.Assertions.assertThatExceptionOfType
2324
import org.junit.jupiter.api.Test
2425
import org.mybatis.dynamic.sql.SqlTable
26+
import org.mybatis.dynamic.sql.exception.InvalidSqlException
2527
import org.mybatis.dynamic.sql.util.kotlin.elements.add
2628
import org.mybatis.dynamic.sql.util.kotlin.elements.column
29+
import org.mybatis.dynamic.sql.util.kotlin.elements.count
2730
import org.mybatis.dynamic.sql.util.kotlin.elements.isBetween
2831
import org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanOrEqualTo
2932
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
@@ -339,4 +342,71 @@ class KGroupingTest {
339342
assertThat(selectStatement.parameters).containsEntry("p1", 4)
340343
assertThat(selectStatement.parameters).containsEntry("p2", 5)
341344
}
345+
346+
@Test
347+
fun testHaving() {
348+
val selectStatement = select(A, count()) {
349+
from(foo)
350+
groupBy(A)
351+
having { count() isGreaterThan 6 }
352+
}
353+
354+
val expected = "select A, count(*)" +
355+
" from Foo" +
356+
" group by A" +
357+
" having count(*) > #{parameters.p1}"
358+
359+
assertThat(selectStatement.selectStatement).isEqualTo(expected)
360+
assertThat(selectStatement.parameters).containsEntry("p1", 6L)
361+
}
362+
363+
@Test
364+
fun testHavingMultipleConditions() {
365+
val selectStatement = select(A, count()) {
366+
from(foo)
367+
groupBy(A)
368+
having {
369+
count() isGreaterThan 6
370+
and { A isEqualTo 5 }
371+
}
372+
}
373+
374+
val expected = "select A, count(*)" +
375+
" from Foo" +
376+
" group by A" +
377+
" having count(*) > #{parameters.p1}" +
378+
" and A = #{parameters.p2}"
379+
380+
assertThat(selectStatement.selectStatement).isEqualTo(expected)
381+
assertThat(selectStatement.parameters).containsEntry("p1", 6L)
382+
assertThat(selectStatement.parameters).containsEntry("p2", 5)
383+
}
384+
385+
@Test
386+
fun testHavingWithOptionalCondition() {
387+
val selectStatement = select(A, count()) {
388+
from(foo)
389+
groupBy(A)
390+
having { count() isGreaterThanWhenPresent null }
391+
}
392+
393+
val expected = "select A, count(*)" +
394+
" from Foo" +
395+
" group by A"
396+
397+
assertThat(selectStatement.selectStatement).isEqualTo(expected)
398+
assertThat(selectStatement.parameters).isEmpty()
399+
}
400+
401+
@Test
402+
fun testThatMultipleHavingClausesThrowsException() {
403+
assertThatExceptionOfType(InvalidSqlException::class.java).isThrownBy {
404+
select(A, count()) {
405+
from(foo)
406+
groupBy(A)
407+
having { count() isGreaterThanWhenPresent null }
408+
having { count() isGreaterThanWhenPresent null }
409+
}
410+
}
411+
}
342412
}

0 commit comments

Comments
 (0)