-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a "real" database implementation
- Loading branch information
Showing
10 changed files
with
167 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/me/relph/domain/adapter/DatabaseUserStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package me.relph.domain.adapter | ||
|
||
import dev.forkhandles.result4k.Failure | ||
import dev.forkhandles.result4k.Result4k | ||
import dev.forkhandles.result4k.Success | ||
import me.relph.domain.port.User | ||
import me.relph.domain.port.UserId | ||
import me.relph.domain.port.UserStorage | ||
import me.relph.domain.port.UserStorageFailure | ||
import org.jetbrains.exposed.sql.Column | ||
import org.jetbrains.exposed.sql.Database | ||
import org.jetbrains.exposed.sql.ResultRow | ||
import org.jetbrains.exposed.sql.Table | ||
import org.jetbrains.exposed.sql.batchInsert | ||
import org.jetbrains.exposed.sql.insert | ||
import org.jetbrains.exposed.sql.select | ||
import org.jetbrains.exposed.sql.selectAll | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
|
||
object Users : Table() { | ||
val id: Column<String> = varchar("id", 10) | ||
val name: Column<String> = varchar("name", length = 50) | ||
override val primaryKey = PrimaryKey(id, name = "PK_User_ID") // name is optional here | ||
} | ||
|
||
private fun ResultRow.toUser() = User(UserId.of(this[Users.id]), this[Users.name]) | ||
class DatabaseUserStorage(private val database: Database) : UserStorage { | ||
|
||
override fun insert(vararg users: User): Result4k<Unit, UserStorageFailure> { | ||
return try { | ||
Users.batchInsert(users.toList()) { user -> | ||
this[Users.name] = user.name | ||
this[Users.id] = user.id.value | ||
} | ||
return Success(Unit) | ||
}catch (e: Exception) { | ||
Failure(UserStorageFailure.Unclassified(e)) | ||
} | ||
} | ||
|
||
override fun byId(id: UserId): Result4k<User, UserStorageFailure> = try { | ||
Success(Users.select { | ||
Users.id.eq(id.value) | ||
}.limit(1).single().toUser()) | ||
} catch (e: Exception) { | ||
when (e) { | ||
is NoSuchElementException -> Failure(UserStorageFailure.NotFound(id)) | ||
else -> Failure(UserStorageFailure.Unclassified(e)) | ||
} | ||
} | ||
|
||
override fun all(): Result4k<List<User>, UserStorageFailure> = try { | ||
Success(Users.selectAll().map { it.toUser() }) | ||
} catch (e: Exception) { | ||
Failure(UserStorageFailure.Unclassified(e)) | ||
} | ||
|
||
override fun <T> transaction(block: () -> T): T = transaction(database) { | ||
block() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package me.relph.domain.port | ||
|
||
interface AppStorage { | ||
fun <T>transaction(block: () -> T): T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/kotlin/me/relph/domain/adapter/DatabaseUserStorageTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package me.relph.domain.adapter | ||
|
||
import me.relph.domain.port.UserStorage | ||
import org.jetbrains.exposed.sql.Database | ||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.BeforeEach | ||
|
||
class DatabaseUserStorageTest : UserStorageContract { | ||
|
||
override val storage: UserStorage = DatabaseUserStorage( | ||
Database.connect( | ||
"jdbc:h2:file:/tmp/boom-test;AUTO_SERVER=TRUE", | ||
driver = "org.h2.Driver", | ||
user = "root", | ||
password = "" | ||
) | ||
) | ||
override fun createSchema() { | ||
SchemaUtils.create(Users) | ||
} | ||
|
||
} |
29 changes: 1 addition & 28 deletions
29
src/test/kotlin/me/relph/domain/adapter/InMemoryUserStorageTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,5 @@ | ||
package me.relph.domain.adapter | ||
|
||
import dev.mrbergin.kotest.result4k.shouldBeFailure | ||
import dev.mrbergin.kotest.result4k.shouldBeSuccess | ||
import me.relph.domain.port.User | ||
import me.relph.domain.port.UserId | ||
import me.relph.domain.port.UserStorage | ||
import me.relph.domain.port.UserStorageFailure.NotFound | ||
import org.junit.jupiter.api.Test | ||
|
||
private val roger = User("Roger") | ||
|
||
interface UserStorageContract { | ||
val storage: UserStorage | ||
@Test | ||
fun `can get a user by id`() { | ||
storage.byId(UserId.of("1")) shouldBeSuccess roger | ||
} | ||
|
||
@Test | ||
fun `can not get a user by id`() { | ||
storage.byId(UserId.of("2")) shouldBeFailure NotFound(UserId.of("2")) | ||
} | ||
@Test | ||
fun `can get all users`() { | ||
storage.all() shouldBeSuccess listOf(roger) | ||
} | ||
} | ||
|
||
internal class InMemoryUserStorageTest : UserStorageContract { | ||
override val storage = InMemoryUserStorage(mapOf(UserId.of("1") to roger)) | ||
override val storage = InMemoryUserStorage() | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/kotlin/me/relph/domain/adapter/UserStorageContract.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package me.relph.domain.adapter | ||
|
||
import dev.mrbergin.kotest.result4k.shouldBeFailure | ||
import dev.mrbergin.kotest.result4k.shouldBeSuccess | ||
import me.relph.domain.port.User | ||
import me.relph.domain.port.UserId | ||
import me.relph.domain.port.UserStorage | ||
import me.relph.domain.port.UserStorageFailure | ||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
|
||
val roger = User(UserId.of("1"), "Roger") | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
interface UserStorageContract { | ||
val storage: UserStorage | ||
|
||
fun createSchema() {} | ||
|
||
|
||
@BeforeAll | ||
fun beforeAll() { | ||
storage.transaction { | ||
createSchema() | ||
storage.insert(roger) | ||
} | ||
} | ||
|
||
@Test | ||
fun `can get a user by id`() { | ||
storage.transaction { | ||
storage.byId(UserId.of("1")) shouldBeSuccess roger | ||
} | ||
} | ||
|
||
@Test | ||
fun `can not get a user by id`() { | ||
storage.transaction { | ||
storage.byId(UserId.of("2")) shouldBeFailure UserStorageFailure.NotFound(UserId.of("2")) | ||
} | ||
} | ||
|
||
@Test | ||
fun `can get all users`() { | ||
storage.transaction { | ||
storage.all() shouldBeSuccess listOf(roger) | ||
} | ||
} | ||
} |