@@ -20,10 +20,13 @@ import examples.kotlin.mybatis3.general.FooDynamicSqlSupport.B
20
20
import examples.kotlin.mybatis3.general.FooDynamicSqlSupport.C
21
21
import examples.kotlin.mybatis3.general.FooDynamicSqlSupport.foo
22
22
import org.assertj.core.api.Assertions.assertThat
23
+ import org.assertj.core.api.Assertions.assertThatExceptionOfType
23
24
import org.junit.jupiter.api.Test
24
25
import org.mybatis.dynamic.sql.SqlTable
26
+ import org.mybatis.dynamic.sql.exception.InvalidSqlException
25
27
import org.mybatis.dynamic.sql.util.kotlin.elements.add
26
28
import org.mybatis.dynamic.sql.util.kotlin.elements.column
29
+ import org.mybatis.dynamic.sql.util.kotlin.elements.count
27
30
import org.mybatis.dynamic.sql.util.kotlin.elements.isBetween
28
31
import org.mybatis.dynamic.sql.util.kotlin.elements.isLessThanOrEqualTo
29
32
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
@@ -339,4 +342,71 @@ class KGroupingTest {
339
342
assertThat(selectStatement.parameters).containsEntry(" p1" , 4 )
340
343
assertThat(selectStatement.parameters).containsEntry(" p2" , 5 )
341
344
}
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
+ }
342
412
}
0 commit comments