Skip to content

Commit

Permalink
Allow for non-postgres usage in DAOTestBase
Browse files Browse the repository at this point in the history
Still default to postgres, but allow overrides of
the testcontainers container and the jooq dialect
  • Loading branch information
josephlbarnett committed Jun 15, 2023
1 parent 19c4670 commit a294b50
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
7 changes: 7 additions & 0 deletions parent-pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,12 @@
<version>${version.testcontainers}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>jdbc</artifactId>
<version>${version.testcontainers}</version>
<scope>test</scope>
</dependency>

<!-- graphQL -->
<dependency>
Expand Down Expand Up @@ -1336,6 +1342,7 @@
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>${version.maven.git}</version>
<configuration>
<offline>true</offline>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>
${project.build.outputDirectory}/${project.groupId}/${project.artifactId}.git.properties
Expand Down
5 changes: 5 additions & 0 deletions testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
<artifactId>postgresql</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>jdbc</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down
33 changes: 25 additions & 8 deletions testing/src/main/kotlin/com/trib3/testing/db/DAOTestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.flywaydb.core.api.configuration.FluentConfiguration
import org.jooq.DSLContext
import org.jooq.SQLDialect
import org.jooq.impl.DSL
import org.testcontainers.containers.JdbcDatabaseContainer
import org.testcontainers.containers.PostgreSQLContainer
import org.testng.annotations.AfterClass
import org.testng.annotations.BeforeClass
Expand All @@ -18,7 +19,7 @@ import javax.sql.DataSource
open class DAOTestBase {
lateinit var dataSource: DataSource
lateinit var ctx: DSLContext
private lateinit var postgres: PostgreSQLContainer<Nothing>
private lateinit var database: JdbcDatabaseContainer<*>
private var inited: Boolean = false

/**
Expand All @@ -30,6 +31,22 @@ open class DAOTestBase {
return Flyway.configure().dataSource(dataSource)
}

/**
* By default create a postgres container, but subclasses
* can override for other implementations
*/
open fun getDatabaseContainer(): JdbcDatabaseContainer<*> {
return PostgreSQLContainer("postgres:13.4")
}

/**
* By default configure a POSTGRES dialect, but subclasses
* can override for other dialects
*/
open fun getJooqDialect(): SQLDialect {
return SQLDialect.POSTGRES
}

/**
* By default configure autoCommit to false since
* we do so in DbConfig, and don't want DAO tests
Expand All @@ -44,22 +61,22 @@ open class DAOTestBase {
open fun setUp() {
if (!inited) {
inited = true
postgres = PostgreSQLContainer("postgres:13.4")
postgres.start()
database = getDatabaseContainer()
database.start()
dataSource = HikariDataSource().apply {
jdbcUrl = postgres.jdbcUrl
username = postgres.username
password = postgres.password
jdbcUrl = database.jdbcUrl
username = database.username
password = database.password
configureDataSource(this)
}
ctx = DSL.using(dataSource, SQLDialect.POSTGRES)
ctx = DSL.using(dataSource, getJooqDialect())
getFlywayConfiguration().load().migrate()
}
}

@AfterClass
open fun tearDown() {
postgres.stop()
database.stop()
(dataSource as HikariDataSource).close()
}
}

0 comments on commit a294b50

Please sign in to comment.