From 7bd955ac403854958edb2e8f288c06496f0ea8f9 Mon Sep 17 00:00:00 2001 From: Martin Vysny Date: Fri, 31 Jan 2025 11:16:56 +0200 Subject: [PATCH] Migrate tests from DynaTest to JUnit5 --- .../kotlin/com/github/vokorm/Databases.kt | 2 - .../com/github/vokorm/MssqlDatabaseTest.kt | 90 +++++++++++++++++++ .../kotlin/com/github/vokorm/TestUtils.kt | 2 + 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/com/github/vokorm/MssqlDatabaseTest.kt diff --git a/src/test/kotlin/com/github/vokorm/Databases.kt b/src/test/kotlin/com/github/vokorm/Databases.kt index 4633ecf..8c05a0a 100644 --- a/src/test/kotlin/com/github/vokorm/Databases.kt +++ b/src/test/kotlin/com/github/vokorm/Databases.kt @@ -298,8 +298,6 @@ fun clearDb() { TypeMappingEntity.deleteAll() } -private val isX86_64: Boolean get() = System.getProperty("os.arch") == "amd64" - @DynaTestDsl private fun DynaNodeGroup.usingDockerizedMSSQL() { check(DockerClientFactory.instance().isDockerAvailable()) { "Docker not available" } diff --git a/src/test/kotlin/com/github/vokorm/MssqlDatabaseTest.kt b/src/test/kotlin/com/github/vokorm/MssqlDatabaseTest.kt new file mode 100644 index 0000000..8abdedd --- /dev/null +++ b/src/test/kotlin/com/github/vokorm/MssqlDatabaseTest.kt @@ -0,0 +1,90 @@ +package com.github.vokorm + +import com.gitlab.mvysny.jdbiorm.JdbiOrm +import com.gitlab.mvysny.jdbiorm.quirks.DatabaseVariant +import org.junit.jupiter.api.* +import org.testcontainers.containers.MSSQLServerContainer +import kotlin.test.expect + +class MssqlDatabaseTest { + companion object { + private lateinit var container: MSSQLServerContainer<*> + @BeforeAll @JvmStatic + fun setup() { + assumeDockerAvailable() + Assumptions.assumeTrue(isX86_64) { "MSSQL is only available on amd64: https://hub.docker.com/_/microsoft-mssql-server/ " } + + container = + MSSQLServerContainer("mcr.microsoft.com/mssql/server:${DatabaseVersions.mssql}") + container.start() + hikari { + minimumIdle = 0 + maximumPoolSize = 30 + jdbcUrl = container.jdbcUrl + username = container.username + password = container.password + } + db { + // otherwise the CREATE FULLTEXT CATALOG would fail: Cannot use full-text search in master, tempdb, or model database. + ddl("CREATE DATABASE foo") + ddl("USE foo") + ddl("CREATE FULLTEXT CATALOG AdvWksDocFTCat") + + ddl( + """create table Test ( + id bigint primary key IDENTITY(1,1) not null, + name varchar(400) not null, + age integer not null, + dateOfBirth datetime, + created datetime NULL, + modified datetime NULL, + alive bit, + maritalStatus varchar(200) + )""" + ) + // unfortunately the default Docker image doesn't support the FULLTEXT index: + // https://stackoverflow.com/questions/60489784/installing-mssql-server-express-using-docker-with-full-text-search-support + // just skip the tests for now + /* + ddl("CREATE UNIQUE INDEX ui_ukDoc ON Test(name);") + ddl("""CREATE FULLTEXT INDEX ON Test + ( + Test --Full-text index column name + TYPE COLUMN name --Name of column that contains file type information + Language 2057 --2057 is the LCID for British English + ) + KEY INDEX ui_ukDoc ON AdvWksDocFTCat --Unique index + WITH CHANGE_TRACKING AUTO --Population type; """) + */ + + ddl("""create table EntityWithAliasedId(myid bigint primary key IDENTITY(1,1) not null, name varchar(400) not null)""") + ddl("""create table NaturalPerson(id varchar(10) primary key not null, name varchar(400) not null, bytes binary(16) not null)""") + ddl("""create table LogRecord(id uniqueidentifier primary key not null, text varchar(400) not null)""") + ddl("""create table TypeMappingEntity(id bigint primary key IDENTITY(1,1) not null, enumTest varchar(10))""") + } + } + + @AfterAll @JvmStatic + fun teardown() { + JdbiOrm.destroy() + if (this::container.isInitialized) { + container.stop() + } + } + } + + @BeforeEach @AfterEach fun purgeDb() { clearDb() } + + @Test fun `expect MSSQL variant`() { + expect(DatabaseVariant.MSSQL) { + db { + DatabaseVariant.from(handle) + } + } + } + + // unfortunately the default Docker image doesn't support the FULLTEXT index: + // https://stackoverflow.com/questions/60489784/installing-mssql-server-express-using-docker-with-full-text-search-support + @Nested + inner class AllDatabaseTests : AbstractDatabaseTests(DatabaseInfo(DatabaseVariant.MSSQL, supportsFullText = false)) +} diff --git a/src/test/kotlin/com/github/vokorm/TestUtils.kt b/src/test/kotlin/com/github/vokorm/TestUtils.kt index ff3a58e..1c665ab 100644 --- a/src/test/kotlin/com/github/vokorm/TestUtils.kt +++ b/src/test/kotlin/com/github/vokorm/TestUtils.kt @@ -15,6 +15,8 @@ import java.time.Instant import java.util.Date import kotlin.test.expect +val isX86_64: Boolean get() = System.getProperty("os.arch") == "amd64" + val gson: Gson = GsonBuilder().registerJavaTimeAdapters().create() private fun GsonBuilder.registerJavaTimeAdapters(): GsonBuilder = apply {