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,98 +7,114 @@ 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() {
SchemaUtils.create(Users) lateinit var db: Database
SchemaUtils.create(UsersFollowers)
@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(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( "test",
"test", "example.com",
"example.com", "testUser",
"testUser", "This user is test user.",
"This user is test user.", "https://example.com/inbox",
"https://example.com/inbox", "https://example.com/outbox",
"https://example.com/outbox", "https://example.com"
"https://example.com"
)
) )
val follower = userRepository.create( )
User( val follower = userRepository.create(
"follower", User(
"follower.example.com", "follower",
"followerUser", "follower.example.com",
"This user is follower user.", "followerUser",
"https://follower.example.com/inbox", "This user is follower user.",
"https://follower.example.com/outbox", "https://follower.example.com/inbox",
"https://follower.example.com" "https://follower.example.com/outbox",
) "https://follower.example.com"
) )
val follower2 = userRepository.create( )
User( val follower2 = userRepository.create(
"follower2", User(
"follower2.example.com", "follower2",
"followerUser2", "follower2.example.com",
"This user is follower user 2.", "followerUser2",
"https://follower2.example.com/inbox", "This user is follower user 2.",
"https://follower2.example.com/outbox", "https://follower2.example.com/inbox",
"https://follower2.example.com" "https://follower2.example.com/outbox",
) "https://follower2.example.com"
) )
userRepository.createFollower(user.id, follower.id) )
userRepository.createFollower(user.id, follower2.id) userRepository.createFollower(user.id, follower.id)
userRepository.findFollowersById(user.id).let { userRepository.createFollower(user.id, follower2.id)
assertIterableEquals(listOf(follower, follower2), it) userRepository.findFollowersById(user.id).let {
} 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( "test",
"test", "example.com",
"example.com", "testUser",
"testUser", "This user is test user.",
"This user is test user.", "https://example.com/inbox",
"https://example.com/inbox", "https://example.com/outbox",
"https://example.com/outbox", "https://example.com"
"https://example.com"
)
) )
val follower = userRepository.create( )
User( val follower = userRepository.create(
"follower", User(
"follower.example.com", "follower",
"followerUser", "follower.example.com",
"This user is follower user.", "followerUser",
"https://follower.example.com/inbox", "This user is follower user.",
"https://follower.example.com/outbox", "https://follower.example.com/inbox",
"https://follower.example.com" "https://follower.example.com/outbox",
) "https://follower.example.com"
) )
userRepository.createFollower(user.id, follower.id) )
val followerIds = UsersFollowers.select { UsersFollowers.userId eq user.id }.map { it[UsersFollowers.followerId] } userRepository.createFollower(user.id, follower.id)
transaction {
val followerIds =
UsersFollowers.select { UsersFollowers.userId eq user.id }.map { it[UsersFollowers.followerId] }
assertIterableEquals(listOf(follower.id), followerIds) assertIterableEquals(listOf(follower.id), followerIds)
} }
} }
} }