-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from viartemev/add-tests
Add tests
- Loading branch information
Showing
9 changed files
with
160 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
src/main/kotlin/com/viartemev/ktor/flyway/FlywayFeature.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.viartemev.ktor.flyway | ||
|
||
import io.ktor.application.Application | ||
import io.ktor.application.ApplicationFeature | ||
import io.ktor.config.ApplicationConfigurationException | ||
import io.ktor.util.AttributeKey | ||
import io.ktor.util.KtorExperimentalAPI | ||
import org.flywaydb.core.Flyway | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import javax.sql.DataSource | ||
|
||
private val flyWayFeatureLogger: Logger = LoggerFactory.getLogger("com.viartemev.ktor.flyway.FlywayFeature") | ||
|
||
@KtorExperimentalAPI | ||
class FlywayFeature(configuration: Configuration) { | ||
private val dataSource = configuration.dataSource | ||
private val location = configuration.location | ||
private val commands: Set<FlywayCommand> = configuration.commands | ||
|
||
class Configuration { | ||
var dataSource: DataSource? = null | ||
var location: String? = null | ||
internal var commands: Set<FlywayCommand> = setOf(Info, Migrate) | ||
fun commands(vararg commandsToExecute: FlywayCommand) { | ||
commands = commandsToExecute.toSet() | ||
} | ||
} | ||
|
||
companion object Feature : ApplicationFeature<Application, Configuration, FlywayFeature> { | ||
override val key = AttributeKey<FlywayFeature>("FlywayFeature") | ||
|
||
override fun install(pipeline: Application, configure: Configuration.() -> Unit): FlywayFeature { | ||
val configuration = Configuration().apply(configure) | ||
val flywayFeature = FlywayFeature(configuration) | ||
if (configuration.dataSource == null) throw ApplicationConfigurationException("DataSource is not configured") | ||
|
||
flyWayFeatureLogger.info("Flyway migration has started") | ||
|
||
val flyway = Flyway | ||
.configure(pipeline.environment.classLoader) | ||
.dataSource(flywayFeature.dataSource) | ||
.also { config -> flywayFeature.location?.let { config.locations(it) } } | ||
.load() | ||
|
||
flywayFeature.commands.map { command -> | ||
flyWayFeatureLogger.info("Running command: ${command.javaClass.simpleName}") | ||
command.run(flyway) | ||
} | ||
|
||
flyWayFeatureLogger.info("Flyway migration has finished") | ||
return flywayFeature | ||
} | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/test/kotlin/com/viartemev/ktor/flyway/DatabaseAbstractTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.viartemev.ktor.flyway | ||
|
||
import com.zaxxer.hikari.HikariConfig | ||
import com.zaxxer.hikari.HikariDataSource | ||
import org.testcontainers.containers.JdbcDatabaseContainer | ||
import java.sql.ResultSet | ||
import javax.sql.DataSource | ||
|
||
abstract class DatabaseAbstractTest { | ||
|
||
fun getDataSource(container: JdbcDatabaseContainer<*>): DataSource { | ||
val hikariConfig = HikariConfig().apply { | ||
jdbcUrl = container.getJdbcUrl() | ||
username = container.getUsername() | ||
password = container.getPassword() | ||
} | ||
return HikariDataSource(hikariConfig) | ||
} | ||
|
||
fun performQuery(container: JdbcDatabaseContainer<*>, sql: String): ResultSet { | ||
val dataSource = getDataSource(container) | ||
val statement = dataSource.connection.createStatement() | ||
statement.execute(sql) | ||
val resultSet = statement.resultSet | ||
resultSet.next() | ||
return resultSet | ||
} | ||
|
||
} |
68 changes: 61 additions & 7 deletions
68
src/test/kotlin/com/viartemev/ktor/flyway/FlywayFeatureTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,75 @@ | ||
package com.viartemev.ktor.flyway | ||
|
||
import io.kotlintest.shouldThrow | ||
import io.kotlintest.specs.StringSpec | ||
import io.ktor.application.install | ||
import io.ktor.config.ApplicationConfigurationException | ||
import io.ktor.server.testing.withTestApplication | ||
import io.ktor.util.KtorExperimentalAPI | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import org.testcontainers.containers.PostgreSQLContainer | ||
import org.testcontainers.junit.jupiter.Container | ||
import org.testcontainers.junit.jupiter.Testcontainers | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
|
||
@Testcontainers | ||
@KtorExperimentalAPI | ||
class FlywayFeatureTest : StringSpec({ | ||
class FlywayFeatureTest : DatabaseAbstractTest() { | ||
|
||
"Flyway feature without datasource should throw exception" { | ||
shouldThrow<ApplicationConfigurationException> { | ||
@Container | ||
val postgres: PostgreSQLContainer<*> = PostgreSQLContainer<Nothing>().apply { | ||
withDatabaseName("flyway") | ||
withUsername("flyway") | ||
withPassword("12345") | ||
} | ||
|
||
@Test | ||
fun `Install Flyway feature without datasource should throw exception`() { | ||
Assertions.assertThrows(ApplicationConfigurationException::class.java) { | ||
withTestApplication { | ||
application.install(Flyway) | ||
application.install(FlywayFeature) | ||
} | ||
} | ||
} | ||
|
||
}) | ||
@Test | ||
fun `Install Flyway feature with Hikari datasource`() { | ||
withTestApplication { | ||
application.install(FlywayFeature) { | ||
dataSource = getDataSource(postgres) | ||
} | ||
} | ||
assertEquals(1, performQuery(postgres, "select count(*) from flyway_schema_history").getLong(1)) | ||
} | ||
|
||
@Test | ||
fun `Install Flyway feature with Hikari datasource and overridden location`() { | ||
withTestApplication { | ||
application.install(FlywayFeature) { | ||
dataSource = getDataSource(postgres) | ||
location = "db/migration-custom" | ||
} | ||
} | ||
assertEquals(2, performQuery(postgres, "select count(*) from flyway_schema_history").getLong(1)) | ||
} | ||
|
||
@Test | ||
fun `Install Flyway feature with Hikari datasource and overridden commands`() { | ||
withTestApplication { | ||
application.install(FlywayFeature) { | ||
dataSource = getDataSource(postgres) | ||
commands(Info) | ||
} | ||
} | ||
assertFalse( | ||
performQuery( | ||
postgres, "SELECT EXISTS (\n" + | ||
" SELECT 1\n" + | ||
" FROM information_schema.tables \n" + | ||
" WHERE table_schema = 'public'\n" + | ||
" AND table_name = 'flyway_schema_history'\n" + | ||
" );\n" | ||
).getBoolean(1) | ||
) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/test/resources/db/migration-custom/V1__create_test_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
create table test(id serial constraint table_name_pk primary key, name text not null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
insert into test (id, name) values (1, 'test'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
create table test(id serial constraint table_name_pk primary key, name text not null); | ||
|