Skip to content

Commit 0aa2918

Browse files
committed
Add tests for all raw DSL methods
1 parent 797f6f5 commit 0aa2918

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/InsertDSLExtensions.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Copyright 2016-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.mybatis.dynamic.sql.util.kotlin
217

318
import org.mybatis.dynamic.sql.insert.InsertDSL

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

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ import org.assertj.core.api.Assertions.assertThat
3636
import org.junit.jupiter.api.Test
3737
import org.mybatis.dynamic.sql.SqlBuilder.*
3838
import org.mybatis.dynamic.sql.util.kotlin.*
39-
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.deleteFrom
39+
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.*
4040
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.from
41-
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.update
4241
import java.io.InputStreamReader
4342
import java.sql.DriverManager
43+
import java.util.*
4444

4545
class GeneralKotlinTest {
4646
private fun newSession(): SqlSession {
@@ -61,6 +61,41 @@ class GeneralKotlinTest {
6161
return SqlSessionFactoryBuilder().build(config).openSession()
6262
}
6363

64+
@Test
65+
fun testRawCount() {
66+
newSession().use { session ->
67+
val mapper = session.getMapper(PersonMapper::class.java)
68+
69+
val countStatement = count(Person) {
70+
where(id, isLessThan(4))
71+
}
72+
73+
assertThat(countStatement.selectStatement).isEqualTo("select count(*) from Person" +
74+
" where id < #{parameters.p1,jdbcType=INTEGER}")
75+
76+
val rows = mapper.count(countStatement)
77+
78+
assertThat(rows).isEqualTo(3)
79+
}
80+
}
81+
82+
@Test
83+
fun testRawCountAllRows() {
84+
newSession().use { session ->
85+
val mapper = session.getMapper(PersonMapper::class.java)
86+
87+
val countStatement = count(Person) {
88+
allRows()
89+
}
90+
91+
assertThat(countStatement.selectStatement).isEqualTo("select count(*) from Person")
92+
93+
val rows = mapper.count(countStatement)
94+
95+
assertThat(rows).isEqualTo(6)
96+
}
97+
}
98+
6499
@Test
65100
fun testRawDelete1() {
66101
newSession().use { session ->
@@ -192,6 +227,80 @@ class GeneralKotlinTest {
192227
}
193228
}
194229

230+
@Test
231+
fun testInsert() {
232+
newSession().use { session ->
233+
val mapper = session.getMapper(PersonMapper::class.java)
234+
235+
val record = PersonRecord(100, "Joe", LastName("Jones"), Date(), true, "Developer", 1)
236+
237+
val insertStatement = insert(record, Person) {
238+
map(id).toProperty("id")
239+
map(firstName).toProperty("firstName")
240+
map(lastName).toProperty("lastName")
241+
map(birthDate).toProperty("birthDate")
242+
map(employed).toProperty("employed")
243+
map(occupation).toProperty("occupation")
244+
map(addressId).toProperty("addressId")
245+
}
246+
247+
val expected = "insert into Person (id, first_name, last_name, birth_date, employed, occupation, address_id)" +
248+
" values" +
249+
" (#{record.id,jdbcType=INTEGER}, #{record.firstName,jdbcType=VARCHAR}," +
250+
" #{record.lastName,jdbcType=VARCHAR,typeHandler=examples.kotlin.mybatis3.canonical.LastNameTypeHandler}," +
251+
" #{record.birthDate,jdbcType=DATE}, #{record.employed,jdbcType=VARCHAR,typeHandler=examples.kotlin.mybatis3.canonical.YesNoTypeHandler}," +
252+
" #{record.occupation,jdbcType=VARCHAR}, #{record.addressId,jdbcType=INTEGER})"
253+
254+
assertThat(insertStatement.insertStatement).isEqualTo(expected)
255+
256+
val rows = mapper.insert(insertStatement)
257+
assertThat(rows).isEqualTo(1)
258+
}
259+
}
260+
261+
@Test
262+
fun testInsertMultiple() {
263+
newSession().use { session ->
264+
val mapper = session.getMapper(PersonMapper::class.java)
265+
266+
val record1 = PersonRecord(100, "Joe", LastName("Jones"), Date(), true, "Developer", 1)
267+
val record2 = PersonRecord(101, "Sarah", LastName("Smith"), Date(), true, "Architect", 2)
268+
269+
val insertStatement = insertMultiple(listOf(record1, record2), Person) {
270+
map(id).toProperty("id")
271+
map(firstName).toProperty("firstName")
272+
map(lastName).toProperty("lastName")
273+
map(birthDate).toProperty("birthDate")
274+
map(employed).toProperty("employed")
275+
map(occupation).toProperty("occupation")
276+
map(addressId).toProperty("addressId")
277+
}
278+
279+
val expected = "insert into Person (id, first_name, last_name, birth_date, employed, occupation, address_id)" +
280+
" values" +
281+
" (#{records[0].id,jdbcType=INTEGER}," +
282+
" #{records[0].firstName,jdbcType=VARCHAR}," +
283+
" #{records[0].lastName,jdbcType=VARCHAR,typeHandler=examples.kotlin.mybatis3.canonical.LastNameTypeHandler}," +
284+
" #{records[0].birthDate,jdbcType=DATE}," +
285+
" #{records[0].employed,jdbcType=VARCHAR,typeHandler=examples.kotlin.mybatis3.canonical.YesNoTypeHandler}," +
286+
" #{records[0].occupation,jdbcType=VARCHAR}," +
287+
" #{records[0].addressId,jdbcType=INTEGER})" +
288+
", (#{records[1].id,jdbcType=INTEGER}," +
289+
" #{records[1].firstName,jdbcType=VARCHAR}," +
290+
" #{records[1].lastName,jdbcType=VARCHAR,typeHandler=examples.kotlin.mybatis3.canonical.LastNameTypeHandler}," +
291+
" #{records[1].birthDate,jdbcType=DATE}," +
292+
" #{records[1].employed,jdbcType=VARCHAR,typeHandler=examples.kotlin.mybatis3.canonical.YesNoTypeHandler}," +
293+
" #{records[1].occupation,jdbcType=VARCHAR}," +
294+
" #{records[1].addressId,jdbcType=INTEGER})"
295+
296+
assertThat(insertStatement.insertStatement).isEqualTo(expected)
297+
298+
val rows = mapper.insertMultiple(insertStatement)
299+
300+
assertThat(rows).isEqualTo(2)
301+
}
302+
}
303+
195304
@Test
196305
fun testRawSelect() {
197306
newSession().use { session ->

0 commit comments

Comments
 (0)