Skip to content

Commit

Permalink
Merge pull request #3 from viartemev/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
viartemev authored Dec 19, 2019
2 parents 327ca54 + a704f38 commit e5fc237
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {

Add the feature to the code:
```kotlin
install(Flyway) {
install(FlywayFeature) {
dataSource = database.connectionPool //required
location = "custom/dir" //optional, default value = "db/migration"
commands(Info, Migrate) //optional, default command list is: Info, Migrate
Expand Down
11 changes: 9 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
runtime "ch.qos.logback:logback-classic:$logback_version"
implementation "io.ktor:ktor-server-core:$ktor_version"
testImplementation "io.ktor:ktor-server-test-host:$ktor_version"
testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.4.2'
testCompile "io.ktor:ktor-server-test-host:$ktor_version"
testCompile "org.testcontainers:testcontainers:1.12.4"
testCompile "org.testcontainers:postgresql:1.12.4"
testCompile "org.testcontainers:junit-jupiter:1.12.4"
testCompile "com.zaxxer:HikariCP:3.4.1"
testCompile "org.postgresql:postgresql:42.2.9"
testCompile "org.junit.jupiter:junit-jupiter-api:5.5.2"
testCompile "org.junit.jupiter:junit-jupiter-params:5.5.2"
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.5.2"
}

compileKotlin {
Expand Down
51 changes: 0 additions & 51 deletions src/main/kotlin/com/viartemev/ktor/flyway/Flyway.kt

This file was deleted.

56 changes: 56 additions & 0 deletions src/main/kotlin/com/viartemev/ktor/flyway/FlywayFeature.kt
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 src/test/kotlin/com/viartemev/ktor/flyway/DatabaseAbstractTest.kt
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 src/test/kotlin/com/viartemev/ktor/flyway/FlywayFeatureTest.kt
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)
)
}
}
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);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
insert into test (id, name) values (1, 'test');
2 changes: 2 additions & 0 deletions src/test/resources/db/migration/V1__create_test_table.sql
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);

0 comments on commit e5fc237

Please sign in to comment.