From d07b1d3ee89b33880acd2a4a5399f2fa322b9ee3 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 12 Apr 2023 22:38:36 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E4=BD=95=E6=95=85=E3=81=8B=E3=83=A6?= =?UTF-8?q?=E3=83=8B=E3=83=BC=E3=82=AF=E3=82=A4=E3=83=B3=E3=83=87=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=82=B9=E9=81=95=E5=8F=8D=E3=81=A7=E3=82=B3=E3=82=B1?= =?UTF-8?q?=E3=82=8B=E3=81=AE=E3=81=A7=E3=83=86=E3=82=B9=E3=83=88=E3=81=94?= =?UTF-8?q?=E3=81=A8=E3=81=ABDB=E3=82=92=E5=88=9D=E6=9C=9F=E5=8C=96?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/repository/UserRepositoryTest.kt | 160 ++++++++++-------- 1 file changed, 88 insertions(+), 72 deletions(-) diff --git a/src/test/kotlin/dev/usbharu/hideout/repository/UserRepositoryTest.kt b/src/test/kotlin/dev/usbharu/hideout/repository/UserRepositoryTest.kt index 73b3e449..740bc90e 100644 --- a/src/test/kotlin/dev/usbharu/hideout/repository/UserRepositoryTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/repository/UserRepositoryTest.kt @@ -7,98 +7,114 @@ 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.Database 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.jetbrains.exposed.sql.transactions.transaction +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions.assertIterableEquals +import org.junit.jupiter.api.BeforeEach 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) +class UserRepositoryTest { + + 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(UsersFollowers) + } + } + + @AfterEach + fun tearDown() { + transaction(db) { + + SchemaUtils.drop(UsersFollowers) + SchemaUtils.drop(Users) + } } @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 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 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" - ) + ) + 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) - } + ) + 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 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 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] } + ) + 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) } + } }