test: 何故かユニークインデックス違反でコケるのでテストごとにDBを初期化するように

This commit is contained in:
usbharu 2023-04-12 22:38:36 +09:00
parent f4df345407
commit d6a54d290b
1 changed files with 88 additions and 72 deletions

View File

@ -7,27 +7,40 @@ import dev.usbharu.hideout.domain.model.Users
import dev.usbharu.hideout.domain.model.UsersFollowers import dev.usbharu.hideout.domain.model.UsersFollowers
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Assertions.assertIterableEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import utils.DatabaseTestBase
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class UserRepositoryTest : DatabaseTestBase() {
@BeforeAll class UserRepositoryTest {
fun beforeAll() {
lateinit var db: Database
@BeforeEach
fun beforeEach() {
db = Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver")
transaction(db) {
SchemaUtils.create(Users) SchemaUtils.create(Users)
SchemaUtils.create(UsersFollowers) SchemaUtils.create(UsersFollowers)
} }
}
@AfterEach
fun tearDown() {
transaction(db) {
SchemaUtils.drop(UsersFollowers)
SchemaUtils.drop(Users)
}
}
@Test @Test
fun `findFollowersById フォロワー一覧を取得`() = runTest { fun `findFollowersById フォロワー一覧を取得`() = runTest {
newSuspendedTransaction {
val userRepository = UserRepository(db) val userRepository = UserRepository(db)
val user = userRepository.create( val user = userRepository.create(
User( User(
@ -67,12 +80,11 @@ class UserRepositoryTest : DatabaseTestBase() {
userRepository.findFollowersById(user.id).let { userRepository.findFollowersById(user.id).let {
assertIterableEquals(listOf(follower, follower2), it) assertIterableEquals(listOf(follower, follower2), it)
} }
}
} }
@Test @Test
fun `createFollower フォロワー追加`() = runTest { fun `createFollower フォロワー追加`() = runTest {
newSuspendedTransaction {
val userRepository = UserRepository(db) val userRepository = UserRepository(db)
val user = userRepository.create( val user = userRepository.create(
User( User(
@ -97,8 +109,12 @@ class UserRepositoryTest : DatabaseTestBase() {
) )
) )
userRepository.createFollower(user.id, follower.id) userRepository.createFollower(user.id, follower.id)
val followerIds = UsersFollowers.select { UsersFollowers.userId eq user.id }.map { it[UsersFollowers.followerId] } transaction {
val followerIds =
UsersFollowers.select { UsersFollowers.userId eq user.id }.map { it[UsersFollowers.followerId] }
assertIterableEquals(listOf(follower.id), followerIds) assertIterableEquals(listOf(follower.id), followerIds)
} }
} }
} }