test: InstanceRepositoryImplのテストを追加

This commit is contained in:
usbharu 2024-09-10 18:55:55 +09:00
parent 27ccca02c0
commit 37c8335165
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
4 changed files with 158 additions and 17 deletions

View File

@ -133,6 +133,7 @@ dependencies {
testImplementation(libs.h2db) testImplementation(libs.h2db)
testImplementation(libs.mockito.kotlin) testImplementation(libs.mockito.kotlin)
testImplementation("org.assertj:assertj-db:2.0.2") testImplementation("org.assertj:assertj-db:2.0.2")
testImplementation("com.ninja-squad:DbSetup-kotlin:2.1.0")
} }
detekt { detekt {

View File

@ -52,7 +52,7 @@ class InstanceRepositoryImpl : InstanceRepository,
} }
override suspend fun findById(id: InstanceId): InstanceEntity? = query { override suspend fun findById(id: InstanceId): InstanceEntity? = query {
return@query Instance.selectAll().where { Instance.id eq id.instanceId } return@query Instance.selectAll().where { Instance.id eq id.instanceId }.limit(1)
.singleOrNull()?.toInstance() .singleOrNull()?.toInstance()
} }
@ -61,7 +61,7 @@ class InstanceRepositoryImpl : InstanceRepository,
} }
override suspend fun findByUrl(url: URI): dev.usbharu.hideout.core.domain.model.instance.Instance? = query { override suspend fun findByUrl(url: URI): dev.usbharu.hideout.core.domain.model.instance.Instance? = query {
return@query Instance.selectAll().where { Instance.url eq url.toString() }.singleOrNull()?.toInstance() return@query Instance.selectAll().where { Instance.url eq url.toString() }.limit(1).singleOrNull()?.toInstance()
} }
companion object { companion object {

View File

@ -1,5 +1,6 @@
package dev.usbharu.hideout.core.infrastructure.exposedrepository package dev.usbharu.hideout.core.infrastructure.exposedrepository
import com.ninja_squad.dbsetup_kotlin.dbSetup
import dev.usbharu.hideout.core.domain.model.instance.* import dev.usbharu.hideout.core.domain.model.instance.*
import dev.usbharu.hideout.core.domain.model.instance.Instance import dev.usbharu.hideout.core.domain.model.instance.Instance
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
@ -12,6 +13,9 @@ import utils.value
import java.net.URI import java.net.URI
import java.sql.Timestamp import java.sql.Timestamp
import java.time.Instant import java.time.Instant
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Instance as InstanceTable import dev.usbharu.hideout.core.infrastructure.exposedrepository.Instance as InstanceTable
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ -19,7 +23,7 @@ class InstanceRepositoryImplTest : AbstractRepositoryTest(InstanceTable) {
@Test @Test
fun save() = runTest { fun save_idが同じレコードがない場合はinsertされる() = runTest {
InstanceRepositoryImpl().save( InstanceRepositoryImpl().save(
Instance( Instance(
id = InstanceId(1), id = InstanceId(1),
@ -38,19 +42,152 @@ class InstanceRepositoryImplTest : AbstractRepositoryTest(InstanceTable) {
) )
val table = assertTable val table = assertTable
assertThat(table) assertThat(table).row(1).isEqualTo(InstanceTable.id, 1).isEqualTo(InstanceTable.name, "test")
.row(1)
.isEqualTo(InstanceTable.id, 1)
.isEqualTo(InstanceTable.name, "test")
.isEqualTo(InstanceTable.url, "https://www.example.com") .isEqualTo(InstanceTable.url, "https://www.example.com")
.isEqualTo(InstanceTable.iconUrl, "https://www.example.com") .isEqualTo(InstanceTable.iconUrl, "https://www.example.com").isEqualTo(InstanceTable.sharedInbox, null)
.isEqualTo(InstanceTable.sharedInbox, null) .isEqualTo(InstanceTable.software, "").isEqualTo(InstanceTable.version, "")
.isEqualTo(InstanceTable.software, "") .isEqualTo(InstanceTable.isBlocked, false).isEqualTo(InstanceTable.isMuted, false)
.isEqualTo(InstanceTable.version, "") .isEqualTo(InstanceTable.moderationNote, "").value(InstanceTable.createdAt)
.isEqualTo(InstanceTable.isBlocked, false) .isEqualTo(Timestamp.from(Instant.parse("2020-01-01T00:00:00Z")))
.isEqualTo(InstanceTable.isMuted, false) }
.isEqualTo(InstanceTable.moderationNote, "")
.value(InstanceTable.createdAt).isEqualTo(Timestamp.from(Instant.parse("2020-01-01T00:00:00Z"))) @Test
fun save_idが同じレコードがある場合はupdateされる() = runTest {
dbSetup(to = dataSource) {
insertInto(InstanceTable.tableName) {
columns(
"ID",
"name",
"DESCRIPTION",
"URL",
"ICON_URL",
"SHARED_INBOX",
"SOFTWARE",
"VERSION",
"IS_BLOCKED",
"IS_MUTED",
"MODERATION_NOTE",
"CREATED_AT"
)
values(
1,
"system",
"",
"https://example.com",
"",
null,
"",
"",
false,
false,
"",
"2024-09-10 16:59:50.160202"
)
}
}.launch()
InstanceRepositoryImpl().save(
Instance(
id = InstanceId(1),
name = InstanceName("test"),
description = InstanceDescription("id"),
url = URI.create("https://www.example.com"),
iconUrl = URI.create("https://www.example.com"),
sharedInbox = null,
software = InstanceSoftware(""),
version = InstanceVersion(""),
isBlocked = false,
isMuted = false,
moderationNote = InstanceModerationNote(""),
createdAt = Instant.parse("2020-01-01T00:00:00Z"),
)
)
val table = assertTable
assertThat(table).row(1).isEqualTo(InstanceTable.id, 1).isEqualTo(InstanceTable.name, "test")
.isEqualTo(InstanceTable.url, "https://www.example.com")
.isEqualTo(InstanceTable.iconUrl, "https://www.example.com").isEqualTo(InstanceTable.sharedInbox, null)
.isEqualTo(InstanceTable.software, "").isEqualTo(InstanceTable.version, "")
.isEqualTo(InstanceTable.isBlocked, false).isEqualTo(InstanceTable.isMuted, false)
.isEqualTo(InstanceTable.moderationNote, "").value(InstanceTable.createdAt)
.isEqualTo(Timestamp.from(Instant.parse("2020-01-01T00:00:00Z")))
}
@Test
fun findById_指定したidで存在したら返す() = runTest {
dbSetup(to = dataSource) {
insertInto(InstanceTable.tableName) {
columns(
"ID",
"name",
"DESCRIPTION",
"URL",
"ICON_URL",
"SHARED_INBOX",
"SOFTWARE",
"VERSION",
"IS_BLOCKED",
"IS_MUTED",
"MODERATION_NOTE",
"CREATED_AT"
)
values(
1,
"test",
"description",
"https://www.example.com",
"https://www.example.com",
null,
"",
"",
false,
false,
"",
Timestamp.from(Instant.parse("2020-01-01T00:00:00Z"))
)
}
}.launch()
val actual = InstanceRepositoryImpl().findById(InstanceId(1))
val expected = Instance(
id = InstanceId(1),
name = InstanceName("test"),
description = InstanceDescription("description"),
url = URI.create("https://www.example.com"),
iconUrl = URI.create("https://www.example.com"),
sharedInbox = null,
software = InstanceSoftware(""),
version = InstanceVersion(""),
isBlocked = false,
isMuted = false,
moderationNote = InstanceModerationNote(""),
createdAt = Instant.parse("2020-01-01T00:00:00Z"),
)
assertEquals(expected, actual)
}
@Test
fun findById_指定したIDで存在しないとnull() = runTest {
assertNull(InstanceRepositoryImpl().findById(InstanceId(1)))
}
companion object {
fun assertEquals(expected: Instance, actual: Instance?) {
assertNotNull(actual)
kotlin.test.assertEquals(expected, actual)
assertEquals(expected.name, actual.name)
assertEquals(expected.description, actual.description)
assertEquals(expected.url, actual.url)
assertEquals(expected.iconUrl, actual.iconUrl)
assertEquals(expected.sharedInbox, actual.sharedInbox)
assertEquals(expected.software, actual.software)
assertEquals(expected.version, actual.version)
assertEquals(expected.isBlocked, actual.isBlocked)
assertEquals(expected.moderationNote, actual.moderationNote)
assertEquals(expected.createdAt, actual.createdAt)
}
} }
} }

View File

@ -33,6 +33,8 @@ abstract class AbstractRepositoryTest(private val exposedTable: org.jetbrains.ex
@BeforeEach @BeforeEach
fun setUp() { fun setUp() {
flyway.clean()
flyway.migrate()
transaction = TransactionManager.currentOrNew(Connection.TRANSACTION_READ_UNCOMMITTED) transaction = TransactionManager.currentOrNew(Connection.TRANSACTION_READ_UNCOMMITTED)
} }
@ -58,8 +60,9 @@ abstract class AbstractRepositoryTest(private val exposedTable: org.jetbrains.ex
flyway = Flyway.configure().cleanDisabled(false).dataSource(dataSource).load() flyway = Flyway.configure().cleanDisabled(false).dataSource(dataSource).load()
val db = Database.connect(dataSource, databaseConfig = DatabaseConfig { Database.connect(dataSource, databaseConfig = DatabaseConfig {
this.defaultMaxAttempts = 1 defaultMaxAttempts = 1
}) })
flyway.clean() flyway.clean()
flyway.migrate() flyway.migrate()