From ba2adf3d147e0a507b1b0569c2aefbbc4f150562 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 12 Apr 2023 15:46:45 +0900 Subject: [PATCH] =?UTF-8?q?test:=20UserRepository=E3=81=AE=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 1 + .../hideout/repository/UserRepository.kt | 5 +- .../hideout/repository/UserRepositoryTest.kt | 104 ++++++++++++++++++ src/test/kotlin/utils/DBResetInterceptor.kt | 28 +++++ src/test/kotlin/utils/DatabaseTestBase.kt | 18 +++ 5 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/dev/usbharu/hideout/repository/UserRepositoryTest.kt create mode 100644 src/test/kotlin/utils/DBResetInterceptor.kt create mode 100644 src/test/kotlin/utils/DatabaseTestBase.kt diff --git a/build.gradle.kts b/build.gradle.kts index 2e5f0742..4dee9ed6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt index 2bb4abe5..f51240f0 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt @@ -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 { diff --git a/src/test/kotlin/dev/usbharu/hideout/repository/UserRepositoryTest.kt b/src/test/kotlin/dev/usbharu/hideout/repository/UserRepositoryTest.kt new file mode 100644 index 00000000..73b3e449 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/repository/UserRepositoryTest.kt @@ -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) + } + } +} diff --git a/src/test/kotlin/utils/DBResetInterceptor.kt b/src/test/kotlin/utils/DBResetInterceptor.kt new file mode 100644 index 00000000..32fc88a9 --- /dev/null +++ b/src/test/kotlin/utils/DBResetInterceptor.kt @@ -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() + } +} diff --git a/src/test/kotlin/utils/DatabaseTestBase.kt b/src/test/kotlin/utils/DatabaseTestBase.kt new file mode 100644 index 00000000..10653631 --- /dev/null +++ b/src/test/kotlin/utils/DatabaseTestBase.kt @@ -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 }) + } + } +}