Skip to content

Commit

Permalink
add more flexibility of jdbc module
Browse files Browse the repository at this point in the history
  • Loading branch information
ayato-p committed Nov 12, 2024
1 parent da67914 commit 68a0d29
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
7 changes: 7 additions & 0 deletions examples/database/src/test/resources/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
create table users (
id int primary key,
name varchar(255)
);

insert into users (id, name)
values (1, 'Alice'), (2, 'Bob');
4 changes: 2 additions & 2 deletions playtest-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.20.2</version>
<version>1.20.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.20.2</version>
<version>1.20.3</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class FocusSQLSteps {

@Step("DB<databaseName>にSQL<sql>を実行した結果が")
fun executeSQL(databaseName: String, sql: String) {
DriverManager.getConnection(jdbcConfig(databaseName).jdbcUrl).use { conn ->
jdbcConfig(databaseName).connector.connect().use { conn ->
conn.prepareStatement(sql).executeQuery().use(ResultSetProxy::of)
.let { ScenarioDataStore.put(K.AssertionTarget, it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,32 @@ package com.uzabase.playtest2.jdbc.config
import com.uzabase.playtest2.core.config.ConfigurationEntry
import com.uzabase.playtest2.core.config.ModuleConfiguration
import com.uzabase.playtest2.core.config.ModuleKey
import java.sql.Connection
import java.sql.DriverManager

internal sealed interface Connector {
fun connect(): Connection

data class JdbcUrl(val url: String): Connector {
override fun connect(): Connection = DriverManager.getConnection(url)
}

data class JdbcUrlWithCredentials(val url: String, val username: String, val password: String): Connector {
override fun connect(): Connection = DriverManager.getConnection(url, username, password)
}
}

internal data class JdbcModuleKey(val databaseName: String) : ModuleKey
internal data class JdbcModuleConfiguration(val jdbcUrl: String) : ModuleConfiguration
internal data class JdbcModuleConfiguration(val connector: Connector) : ModuleConfiguration

fun jdbc(databaseName: String, jdbcUrl: String): ConfigurationEntry =
ConfigurationEntry(
JdbcModuleKey(databaseName),
JdbcModuleConfiguration(jdbcUrl)
JdbcModuleConfiguration(Connector.JdbcUrl(jdbcUrl))
)

fun jdbc(databaseName: String, jdbcUrl: String, username: String, password: String): ConfigurationEntry =
ConfigurationEntry(
JdbcModuleKey(databaseName),
JdbcModuleConfiguration(Connector.JdbcUrlWithCredentials(jdbcUrl, username, password))
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,30 @@ import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.types.shouldBeInstanceOf

class JdbcConfigurationTest : FunSpec({

test("jdbc configuration") {
jdbc("test", "jdbc:h2:mem:test")
context("jdbc configuration with jdbc url") {
val entry = jdbc("test", "jdbc:h2:mem:test")
.shouldBeInstanceOf<ConfigurationEntry>()

test("should be instance of ConfigurationEntry") {
entry.shouldBeInstanceOf<ConfigurationEntry>()
}

test("should be instance of Connector") {
val conf = entry.config as JdbcModuleConfiguration
conf.connector.shouldBeInstanceOf<Connector>()
}
}

context("jdbc configuration with username/password") {
val entry = jdbc("test", "jdbc:h2:mem:test", "alice", "password")

test("should be instance of ConfigurationEntry") {
entry.shouldBeInstanceOf<ConfigurationEntry>()
}

test("should be instance of Connector") {
val conf = entry.config as JdbcModuleConfiguration
conf.connector.shouldBeInstanceOf<Connector>()
}
}
})

0 comments on commit 68a0d29

Please sign in to comment.