From 2efae6f6daf281c2106a221cf014c0c71495db16 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 30 Jan 2024 22:26:13 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E9=96=93=E9=81=95=E3=81=A3=E3=81=A6?= =?UTF-8?q?=E3=81=84=E3=82=8B=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ExposedPaginationExtensionKtTest.kt | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/application/infrastructure/exposed/ExposedPaginationExtensionKtTest.kt diff --git a/src/test/kotlin/dev/usbharu/hideout/application/infrastructure/exposed/ExposedPaginationExtensionKtTest.kt b/src/test/kotlin/dev/usbharu/hideout/application/infrastructure/exposed/ExposedPaginationExtensionKtTest.kt new file mode 100644 index 00000000..e258978f --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/application/infrastructure/exposed/ExposedPaginationExtensionKtTest.kt @@ -0,0 +1,115 @@ +package dev.usbharu.hideout.application.infrastructure.exposed + +import org.assertj.core.api.Assertions.assertThat +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.transactions.transaction +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test + +class ExposedPaginationExtensionKtTest { + + @BeforeEach + fun setUp(): Unit = transaction { + val map = (1..100).map { it to it.toString() } + + ExposePaginationTestTable.batchInsert(map){ + this[ExposePaginationTestTable.id] = it.first.toLong() + this[ExposePaginationTestTable.name] = it.second + } + } + + @AfterEach + fun tearDown():Unit = transaction { + ExposePaginationTestTable.deleteAll() + } + + @Test + fun パラメーター無しでの取得(): Unit = transaction { + val pagination: List = ExposePaginationTestTable.selectAll().pagination(Page.of(), ExposePaginationTestTable.id).limit(20).toList() + + assertThat(pagination.firstOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(100) + assertThat(pagination.lastOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(81) + assertThat(pagination).size().isEqualTo(20) + } + + @Test + fun maxIdを指定して取得(): Unit = transaction { + val pagination: List = ExposePaginationTestTable.selectAll().pagination(Page.of(maxId = 100), ExposePaginationTestTable.id).limit(20).toList() + + assertThat(pagination.firstOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(99) + assertThat(pagination.lastOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(80) + assertThat(pagination).size().isEqualTo(20) + } + + @Test + fun sinceIdを指定して取得(): Unit = transaction { + val pagination: List = ExposePaginationTestTable.selectAll().pagination(Page.of(sinceId = 15), ExposePaginationTestTable.id).limit(20).toList() + + assertThat(pagination.firstOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(100) + assertThat(pagination.lastOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(81) + assertThat(pagination).size().isEqualTo(20) + } + + @Test + fun minIdを指定して取得():Unit = transaction { + val pagination: List = ExposePaginationTestTable.selectAll().pagination(Page.of(minId = 45), ExposePaginationTestTable.id).limit(20).toList() + + assertThat(pagination.firstOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(46) + assertThat(pagination.lastOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(65) + assertThat(pagination).size().isEqualTo(20) + } + + @Test + fun maxIdとsinceIdを指定して取得(): Unit = transaction { + val pagination: List = ExposePaginationTestTable.selectAll().pagination(Page.of(maxId = 45, sinceId = 34), ExposePaginationTestTable.id).limit(20).toList() + + assertThat(pagination.firstOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(44) + assertThat(pagination.lastOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(35) + assertThat(pagination).size().isEqualTo(10) + } + + @Test + fun maxIdとminIdを指定して取得():Unit = transaction { + val pagination: List = ExposePaginationTestTable.selectAll().pagination(Page.of(maxId = 54, minId = 45), ExposePaginationTestTable.id).limit(20).toList() + + assertThat(pagination.firstOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(46) + assertThat(pagination.lastOrNull()?.getOrNull(ExposePaginationTestTable.id)).isEqualTo(53) + assertThat(pagination).size().isEqualTo(8) + } + + @Test + fun limitを指定して取得():Unit = transaction { + val pagination: List = ExposePaginationTestTable.selectAll().pagination(Page.of(limit = 30), ExposePaginationTestTable.id).toList() + assertThat(pagination).size().isEqualTo(30) + } + + object ExposePaginationTestTable : Table(){ + val id = long("id") + val name = varchar("name",100) + + override val primaryKey: PrimaryKey? + get() = PrimaryKey(id) + } + + companion object { + private lateinit var database: Database + + @JvmStatic + @BeforeAll + fun beforeAll(): Unit { + database = Database.connect( + url = "jdbc:h2:mem:test;MODE=POSTGRESQL;DB_CLOSE_DELAY=-1;CASE_INSENSITIVE_IDENTIFIERS=true;TRACE_LEVEL_FILE=4;", + driver = "org.h2.Driver", + user = "sa", + password = "" + ) + + transaction(database) { + SchemaUtils.create(ExposePaginationTestTable) + SchemaUtils.createMissingTablesAndColumns(ExposePaginationTestTable) + } + } + } +} \ No newline at end of file