Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit 7df3dcf

Browse files
committed
H2を用いたマッピングテストの基盤を追加
1 parent 7bbeb53 commit 7df3dcf

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.mapk.krowmapper
2+
3+
import com.google.common.base.CaseFormat
4+
import org.h2.jdbcx.JdbcDataSource
5+
import org.junit.jupiter.api.BeforeAll
6+
import org.junit.jupiter.api.Test
7+
import org.junit.jupiter.api.TestInstance
8+
import org.springframework.jdbc.core.JdbcTemplate
9+
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource
10+
import org.springframework.jdbc.core.simple.SimpleJdbcInsert
11+
12+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
13+
class UseDBMappingTest {
14+
enum class FooStatus {
15+
active, archive, deleted
16+
}
17+
18+
data class Foo(
19+
val fooId: Int,
20+
val fooName: String,
21+
val fooStatus: FooStatus,
22+
val isBar: Boolean,
23+
val description: String?
24+
)
25+
26+
data class FooInsert(
27+
val fooId: Int,
28+
val fooName: String,
29+
private val fooStatus: FooStatus,
30+
private val isBar: Boolean,
31+
val description: String?
32+
) {
33+
fun getFooStatus(): String = fooStatus.name
34+
fun getIsBar(): String = isBar.toString()
35+
}
36+
37+
lateinit var jdbcTemplate: JdbcTemplate
38+
39+
@BeforeAll
40+
fun beforeAll() {
41+
val dataSource = JdbcDataSource()
42+
dataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS APP\\;SET SCHEMA APP;")
43+
44+
jdbcTemplate = JdbcTemplate(dataSource)
45+
46+
jdbcTemplate.execute("""
47+
CREATE TABLE IF NOT EXISTS `foo_table` (
48+
`foo_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
49+
`foo_name` VARCHAR(255) NOT NULL,
50+
`foo_status` ENUM('active', 'archive', 'deleted') NOT NULL,
51+
`is_bar` ENUM('true', 'false') NOT NULL,
52+
`description` VARCHAR(1023) NULL DEFAULT NULL,
53+
PRIMARY KEY (`foo_id`)
54+
);
55+
""".trimIndent())
56+
57+
val data = FooInsert(10, "Foo", FooStatus.archive, false, null)
58+
59+
SimpleJdbcInsert(jdbcTemplate).withTableName("foo_table").execute(BeanPropertySqlParameterSource(data))
60+
}
61+
62+
@Test
63+
fun test() {
64+
val result = jdbcTemplate.query("SELECT * FROM foo_table", KRowMapper(::Foo) {
65+
CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, it)
66+
})
67+
println(result)
68+
}
69+
}

0 commit comments

Comments
 (0)