mirror of https://github.com/usbharu/Hideout.git
test: UserRepositoryのテストを追加
This commit is contained in:
parent
5846dde42f
commit
71ad3c9c7d
|
@ -64,6 +64,7 @@ dependencies {
|
|||
|
||||
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
|
||||
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
|
||||
testImplementation ("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
|
||||
|
||||
implementation("io.ktor:ktor-client-core:$ktor_version")
|
||||
implementation("io.ktor:ktor-client-cio:$ktor_version")
|
||||
|
|
|
@ -133,7 +133,10 @@ class UserRepository(private val database: Database) : IUserRepository {
|
|||
followers.get(Users.name),
|
||||
followers.get(Users.domain),
|
||||
followers.get(Users.screenName),
|
||||
followers.get(Users.description)
|
||||
followers.get(Users.description),
|
||||
followers.get(Users.inbox),
|
||||
followers.get(Users.outbox),
|
||||
followers.get(Users.url)
|
||||
)
|
||||
.select { Users.id eq id }
|
||||
.map {
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
@file:OptIn(ExperimentalCoroutinesApi::class)
|
||||
|
||||
package dev.usbharu.hideout.repository
|
||||
|
||||
import dev.usbharu.hideout.domain.model.User
|
||||
import dev.usbharu.hideout.domain.model.Users
|
||||
import dev.usbharu.hideout.domain.model.UsersFollowers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.jetbrains.exposed.sql.SchemaUtils
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import org.junit.jupiter.api.BeforeAll
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import utils.DatabaseTestBase
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class UserRepositoryTest : DatabaseTestBase() {
|
||||
|
||||
@BeforeAll
|
||||
fun beforeAll() {
|
||||
SchemaUtils.create(Users)
|
||||
SchemaUtils.create(UsersFollowers)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `findFollowersById フォロワー一覧を取得`() = runTest {
|
||||
newSuspendedTransaction {
|
||||
val userRepository = UserRepository(db)
|
||||
val user = userRepository.create(
|
||||
User(
|
||||
"test",
|
||||
"example.com",
|
||||
"testUser",
|
||||
"This user is test user.",
|
||||
"https://example.com/inbox",
|
||||
"https://example.com/outbox",
|
||||
"https://example.com"
|
||||
)
|
||||
)
|
||||
val follower = userRepository.create(
|
||||
User(
|
||||
"follower",
|
||||
"follower.example.com",
|
||||
"followerUser",
|
||||
"This user is follower user.",
|
||||
"https://follower.example.com/inbox",
|
||||
"https://follower.example.com/outbox",
|
||||
"https://follower.example.com"
|
||||
)
|
||||
)
|
||||
val follower2 = userRepository.create(
|
||||
User(
|
||||
"follower2",
|
||||
"follower2.example.com",
|
||||
"followerUser2",
|
||||
"This user is follower user 2.",
|
||||
"https://follower2.example.com/inbox",
|
||||
"https://follower2.example.com/outbox",
|
||||
"https://follower2.example.com"
|
||||
)
|
||||
)
|
||||
userRepository.createFollower(user.id, follower.id)
|
||||
userRepository.createFollower(user.id, follower2.id)
|
||||
userRepository.findFollowersById(user.id).let {
|
||||
assertIterableEquals(listOf(follower, follower2), it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `createFollower フォロワー追加`() = runTest {
|
||||
newSuspendedTransaction {
|
||||
val userRepository = UserRepository(db)
|
||||
val user = userRepository.create(
|
||||
User(
|
||||
"test",
|
||||
"example.com",
|
||||
"testUser",
|
||||
"This user is test user.",
|
||||
"https://example.com/inbox",
|
||||
"https://example.com/outbox",
|
||||
"https://example.com"
|
||||
)
|
||||
)
|
||||
val follower = userRepository.create(
|
||||
User(
|
||||
"follower",
|
||||
"follower.example.com",
|
||||
"followerUser",
|
||||
"This user is follower user.",
|
||||
"https://follower.example.com/inbox",
|
||||
"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] }
|
||||
assertIterableEquals(listOf(follower.id), followerIds)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package utils
|
||||
|
||||
import org.jetbrains.exposed.sql.Transaction
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
import org.junit.jupiter.api.extension.*
|
||||
|
||||
class DBResetInterceptor : BeforeAllCallback,AfterAllCallback,BeforeEachCallback,AfterEachCallback {
|
||||
private lateinit var transactionAll: Transaction
|
||||
private lateinit var transactionEach: Transaction
|
||||
|
||||
override fun beforeAll(context: ExtensionContext?) {
|
||||
transactionAll = TransactionManager.manager.newTransaction()
|
||||
}
|
||||
|
||||
override fun afterAll(context: ExtensionContext?) {
|
||||
transactionAll.rollback()
|
||||
transactionAll.close()
|
||||
}
|
||||
|
||||
override fun beforeEach(context: ExtensionContext?) {
|
||||
transactionEach = TransactionManager.manager.newTransaction(outerTransaction = transactionAll)
|
||||
}
|
||||
|
||||
override fun afterEach(context: ExtensionContext?) {
|
||||
transactionEach.rollback()
|
||||
transactionEach.close()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package utils
|
||||
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.jetbrains.exposed.sql.DatabaseConfig
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
|
||||
|
||||
@ExtendWith(DBResetInterceptor::class)
|
||||
abstract class DatabaseTestBase {
|
||||
companion object {
|
||||
init {
|
||||
Database.connect(
|
||||
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1",
|
||||
driver = "org.h2.Driver",
|
||||
databaseConfig = DatabaseConfig { useNestedTransactions = true })
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue