Add ShadowJar compatibility and Query file can be define name with comment
Add Migration and Query definition class Add Docker DB for tests
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import java.io.File
|
||||
import fr.postgresjson.utils.searchSqlFiles
|
||||
import java.net.URI
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
import fr.postgresjson.definition.Function as FunctionDefinition
|
||||
import fr.postgresjson.definition.Query as QueryDefinition
|
||||
|
||||
class Requester(
|
||||
private val connection: Connection,
|
||||
@@ -13,17 +16,19 @@ class Requester(
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(query: QueryDefinition): Requester = addQuery(query.name, query.script)
|
||||
|
||||
fun addQuery(name: String, sql: String): Requester {
|
||||
addQuery(Query(name, sql, connection))
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(queriesDirectory: File): Requester {
|
||||
queriesDirectory.walk()
|
||||
.filter { it.isFile && it.extension == "sql" }
|
||||
fun addQuery(queriesDirectory: URI): Requester {
|
||||
queriesDirectory.searchSqlFiles()
|
||||
.forEach {
|
||||
val path = it.parentFile.nameWithoutExtension
|
||||
addQuery("$path/${it.nameWithoutExtension}", it.readText())
|
||||
if (it is QueryDefinition) {
|
||||
addQuery(it)
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
||||
@@ -44,11 +49,12 @@ class Requester(
|
||||
return this
|
||||
}
|
||||
|
||||
fun addFunction(functionsDirectory: File): Requester {
|
||||
functionsDirectory.walk()
|
||||
.filter { it.isFile && it.extension == "sql" }
|
||||
fun addFunction(functionsDirectory: URI): Requester {
|
||||
functionsDirectory.searchSqlFiles()
|
||||
.forEach {
|
||||
addFunction(it.readText())
|
||||
if (it is FunctionDefinition) {
|
||||
addFunction(it)
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
||||
@@ -69,8 +75,8 @@ class Requester(
|
||||
|
||||
class RequesterFactory(
|
||||
private val connection: Connection,
|
||||
private val queriesDirectory: File? = null,
|
||||
private val functionsDirectory: File? = null
|
||||
private val queriesDirectory: URI? = null,
|
||||
private val functionsDirectory: URI? = null
|
||||
) {
|
||||
constructor(
|
||||
host: String = "localhost",
|
||||
@@ -78,8 +84,8 @@ class Requester(
|
||||
database: String = "dc-project",
|
||||
username: String = "dc-project",
|
||||
password: String = "dc-project",
|
||||
queriesDirectory: File? = null,
|
||||
functionsDirectory: File? = null
|
||||
queriesDirectory: URI? = null,
|
||||
functionsDirectory: URI? = null
|
||||
) : this(
|
||||
Connection(host = host, port = port, database = database, username = username, password = password),
|
||||
queriesDirectory,
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
|
||||
open class Function(
|
||||
override val script: String
|
||||
class Function(
|
||||
override val script: String,
|
||||
override var source: Path? = null
|
||||
) : Resource, ParametersInterface {
|
||||
val returns: String
|
||||
override val name: String
|
||||
override val parameters: List<Parameter>
|
||||
override var source: File? = null
|
||||
|
||||
init {
|
||||
val functionRegex =
|
||||
@@ -46,8 +47,7 @@ open class Function(
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ParseException(message: String, cause: Throwable? = null) : Exception(message, cause)
|
||||
class FunctionNotFound(cause: Throwable? = null) : ParseException("Function not found in script", cause)
|
||||
class FunctionNotFound(cause: Throwable? = null) : Resource.ParseException("Function not found in script", cause)
|
||||
|
||||
fun getDefinition(): String {
|
||||
return parameters
|
||||
|
||||
35
src/main/kotlin/fr/postgresjson/definition/Migration.kt
Normal file
35
src/main/kotlin/fr/postgresjson/definition/Migration.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.nio.file.Path
|
||||
|
||||
class Migration(
|
||||
override val script: String,
|
||||
source: Path
|
||||
) : Resource {
|
||||
override val name: String
|
||||
val direction: Direction
|
||||
override var source: Path? = null
|
||||
|
||||
init {
|
||||
this.source = source
|
||||
this.direction = source.fileName.toString()
|
||||
.let {
|
||||
when {
|
||||
it.endsWith(".down.sql") -> Direction.DOWN
|
||||
it.endsWith(".up.sql") -> Direction.UP
|
||||
else -> throw MigrationNotFound()
|
||||
}
|
||||
}
|
||||
this.name = source.fileName.toString()
|
||||
.substringAfterLast("/")
|
||||
.let {
|
||||
when (direction) {
|
||||
Direction.DOWN -> it.substringBefore(".down.sql")
|
||||
Direction.UP -> it.substringBefore(".up.sql")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MigrationNotFound(cause: Throwable? = null) : Resource.ParseException("Migration not found in script", cause)
|
||||
enum class Direction { UP, DOWN }
|
||||
}
|
||||
25
src/main/kotlin/fr/postgresjson/definition/Query.kt
Normal file
25
src/main/kotlin/fr/postgresjson/definition/Query.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.nio.file.Path
|
||||
|
||||
class Query(
|
||||
override val script: String,
|
||||
source: Path
|
||||
) : Resource {
|
||||
override var source: Path? = source
|
||||
override val name: String = getNameFromComment(script) ?: getNameFromFile(source)
|
||||
|
||||
/** Try to get name from comment in file */
|
||||
private fun getNameFromComment(script: String): String? =
|
||||
"""-- *name ?: ?(?<name>[^ \n]+)"""
|
||||
.toRegex(setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
|
||||
.find(script)?.let {
|
||||
it.groups["name"]?.value?.trim()
|
||||
}
|
||||
|
||||
/** Try to get name from the filename */
|
||||
private fun getNameFromFile(source: Path): String = source
|
||||
.fileName.toString()
|
||||
.substringAfterLast("/")
|
||||
.substringBeforeLast(".sql")
|
||||
}
|
||||
@@ -1,11 +1,38 @@
|
||||
package fr.postgresjson.definition
|
||||
|
||||
import java.io.File
|
||||
import java.net.URL
|
||||
import java.nio.file.Path
|
||||
|
||||
interface Resource {
|
||||
val name: String
|
||||
val script: String
|
||||
var source: File?
|
||||
var source: Path?
|
||||
|
||||
open class ParseException(message: String, cause: Throwable? = null) : Exception(message, cause)
|
||||
|
||||
companion object {
|
||||
fun build(file: File): Resource =
|
||||
build(file.readText(), Path.of(file.toURI()))
|
||||
|
||||
fun build(url: URL): Resource =
|
||||
build(url.readText(), Path.of(url.toURI()))
|
||||
|
||||
fun build(resource: String, path: Path): Resource =
|
||||
try {
|
||||
Function(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
try {
|
||||
Migration(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
try {
|
||||
Query(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
throw ParseException("No SQL resource found")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface ResourceCollection {
|
||||
|
||||
@@ -2,14 +2,15 @@ package fr.postgresjson.migration
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.definition.Function.FunctionNotFound
|
||||
import fr.postgresjson.definition.Migration as DefinitionMigration
|
||||
import fr.postgresjson.entity.mutable.Entity
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import fr.postgresjson.migration.Migration.Status
|
||||
import fr.postgresjson.utils.LoggerDelegate
|
||||
import fr.postgresjson.utils.searchSqlFiles
|
||||
import org.slf4j.Logger
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.net.URI
|
||||
import java.util.*
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
|
||||
@@ -35,27 +36,27 @@ interface Migration {
|
||||
|
||||
data class Migrations private constructor(
|
||||
private val connection: Connection,
|
||||
private val queries: MutableMap<String, Query> = mutableMapOf(),
|
||||
private val migrationsScripts: MutableMap<String, Query> = mutableMapOf(),
|
||||
private val functions: MutableMap<String, Function> = mutableMapOf()
|
||||
) {
|
||||
private var directories: List<File> = emptyList()
|
||||
private var directories: List<URI> = emptyList()
|
||||
private val logger: Logger? by LoggerDelegate()
|
||||
constructor(directory: File, connection: Connection) : this(listOf(directory), connection)
|
||||
constructor(directory: URI, connection: Connection) : this(listOf(directory), connection)
|
||||
|
||||
constructor(directories: List<File>, connection: Connection) : this(connection) {
|
||||
constructor(directories: List<URI>, connection: Connection) : this(connection) {
|
||||
initDB()
|
||||
this.directories = directories
|
||||
reset()
|
||||
}
|
||||
|
||||
fun reset() {
|
||||
queries.clear()
|
||||
migrationsScripts.clear()
|
||||
functions.clear()
|
||||
|
||||
getMigrationFromDB()
|
||||
getMigrationFromDirectory(directories)
|
||||
|
||||
queries.forEach { (_, query) ->
|
||||
migrationsScripts.forEach { (_, query) ->
|
||||
if (query.doExecute === null) {
|
||||
query.doExecute = Action.DOWN
|
||||
}
|
||||
@@ -84,7 +85,7 @@ data class Migrations private constructor(
|
||||
this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let {
|
||||
connection.select<MigrationEntity>(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||
.map { query ->
|
||||
queries[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
|
||||
migrationsScripts[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,7 +93,7 @@ data class Migrations private constructor(
|
||||
/**
|
||||
* Get all migration from multiples Directories
|
||||
*/
|
||||
private fun getMigrationFromDirectory(directory: List<File>) {
|
||||
private fun getMigrationFromDirectory(directory: List<URI>) {
|
||||
directory.forEach {
|
||||
getMigrationFromDirectory(it)
|
||||
}
|
||||
@@ -101,29 +102,26 @@ data class Migrations private constructor(
|
||||
/**
|
||||
* Get all migration from Directory
|
||||
*/
|
||||
private fun getMigrationFromDirectory(directory: File) {
|
||||
directory.walk().filter {
|
||||
it.isFile
|
||||
}.forEach { file ->
|
||||
if (file.name.endsWith(".up.sql")) {
|
||||
file.path.substring(0, file.path.length - 7).let {
|
||||
try {
|
||||
val down = File("$it.down.sql").readText()
|
||||
val up = file.readText()
|
||||
val name = file.name.substring(0, file.name.length - 7)
|
||||
addQuery(name, up, down)
|
||||
} catch (e: FileNotFoundException) {
|
||||
throw DownMigrationNotDefined("$it.down.sql", e)
|
||||
}
|
||||
private fun getMigrationFromDirectory(directory: URI) {
|
||||
val downs: MutableMap<String, DefinitionMigration> = mutableMapOf()
|
||||
|
||||
/* Set Down Migration */
|
||||
directory.searchSqlFiles().apply {
|
||||
forEach { migration ->
|
||||
if (migration is DefinitionMigration && migration.direction == DefinitionMigration.Direction.DOWN) {
|
||||
downs += migration.name to migration
|
||||
}
|
||||
} else if (file.name.endsWith(".down.sql")) {
|
||||
// Nothing
|
||||
} else {
|
||||
val fileContent = file.readText()
|
||||
try {
|
||||
addFunction(fileContent)
|
||||
} catch (e: FunctionNotFound) {
|
||||
// Nothing
|
||||
}
|
||||
|
||||
/* Set up migrations and functions */
|
||||
forEach { migration ->
|
||||
if (migration is DefinitionMigration && migration.direction == DefinitionMigration.Direction.UP) {
|
||||
val down = downs[migration.name] ?: throw DownMigrationNotDefined(migration.name + ".down.sql")
|
||||
downs -= migration.name
|
||||
|
||||
addQuery(migration, down)
|
||||
} else if (migration is DefinitionFunction) {
|
||||
addFunction(migration)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,7 +129,7 @@ data class Migrations private constructor(
|
||||
|
||||
enum class Direction { UP, DOWN }
|
||||
|
||||
internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException) :
|
||||
internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException? = null) :
|
||||
Throwable("The file $path whas not found", cause)
|
||||
|
||||
fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations {
|
||||
@@ -155,18 +153,21 @@ data class Migrations private constructor(
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(up: DefinitionMigration, down: DefinitionMigration, callback: (Query) -> Unit = {}): Migrations =
|
||||
addQuery(up.name, up.script, down.script, callback)
|
||||
|
||||
fun addQuery(name: String, up: String, down: String, callback: (Query) -> Unit = {}): Migrations {
|
||||
if (queries[name] === null) {
|
||||
queries[name] = Query(name, up, down, connection).apply {
|
||||
if (migrationsScripts[name] === null) {
|
||||
migrationsScripts[name] = Query(name, up, down, connection).apply {
|
||||
doExecute = Action.UP
|
||||
}
|
||||
} else {
|
||||
queries[name]!!.apply {
|
||||
migrationsScripts[name]!!.apply {
|
||||
doExecute = Action.OK
|
||||
}
|
||||
}
|
||||
|
||||
callback(queries[name]!!)
|
||||
callback(migrationsScripts[name]!!)
|
||||
|
||||
return this
|
||||
}
|
||||
@@ -191,7 +192,7 @@ data class Migrations private constructor(
|
||||
|
||||
internal fun up(): Map<String, Status> {
|
||||
val list: MutableMap<String, Status> = mutableMapOf()
|
||||
queries.forEach {
|
||||
migrationsScripts.forEach {
|
||||
it.value.let { query ->
|
||||
if (query.doExecute == Action.UP) {
|
||||
query.up().let { status ->
|
||||
@@ -216,7 +217,7 @@ data class Migrations private constructor(
|
||||
|
||||
internal fun down(force: Boolean = false): Map<String, Status> {
|
||||
val list: MutableMap<String, Status> = mutableMapOf()
|
||||
queries.forEach {
|
||||
migrationsScripts.forEach {
|
||||
it.value.let { query ->
|
||||
if (query.doExecute == Action.DOWN || force) {
|
||||
query.down().let { status ->
|
||||
@@ -297,7 +298,7 @@ data class Migrations private constructor(
|
||||
}
|
||||
|
||||
fun copy(): Migrations {
|
||||
val queriesCopy = queries.map {
|
||||
val queriesCopy = migrationsScripts.map {
|
||||
it.key to it.value.copy()
|
||||
}.toMap().toMutableMap()
|
||||
|
||||
|
||||
46
src/main/kotlin/fr/postgresjson/utils/searchSqlFiles.kt
Normal file
46
src/main/kotlin/fr/postgresjson/utils/searchSqlFiles.kt
Normal file
@@ -0,0 +1,46 @@
|
||||
package fr.postgresjson.utils
|
||||
|
||||
import fr.postgresjson.definition.Resource
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.net.URI
|
||||
import java.net.URL
|
||||
import java.nio.file.FileSystems
|
||||
import java.nio.file.FileVisitOption
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import kotlin.streams.asSequence
|
||||
|
||||
fun URL.searchSqlFiles() = this.toURI().searchSqlFiles()
|
||||
|
||||
fun URI.searchSqlFiles() = sequence<Resource> {
|
||||
val logger: Logger = LoggerFactory.getLogger("sqlFilesSearch")
|
||||
val uri: URI = this@searchSqlFiles
|
||||
if (uri.scheme == "jar") {
|
||||
val relativePath = uri.toString().substringAfter('!')
|
||||
FileSystems
|
||||
.newFileSystem(uri, emptyMap<String, Any>())
|
||||
.getPath(relativePath)
|
||||
.walk(5)
|
||||
.asSequence()
|
||||
.filter { it.fileName.toString().endsWith(".sql") }
|
||||
.map { it.toUri().toURL() }
|
||||
.forEach {
|
||||
logger.debug(it.toString())
|
||||
yield(Resource.build(it))
|
||||
}
|
||||
} else {
|
||||
uri
|
||||
.walk(5)
|
||||
.asSequence()
|
||||
.map { it.toFile() }
|
||||
.filter { it.isFile && it.extension == "sql" }
|
||||
.forEach {
|
||||
logger.debug(it.toString())
|
||||
yield(Resource.build(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun Path.walk(maxDepth: Int = 2147483647, vararg options: FileVisitOption) = Files.walk(this, maxDepth, *options)
|
||||
private fun URI.walk(maxDepth: Int = 2147483647, vararg options: FileVisitOption) = Files.walk(Path.of(this), maxDepth, *options)
|
||||
@@ -10,13 +10,12 @@ import org.amshove.kluent.shouldThrow
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import java.io.File
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class MigrationTest() : TestAbstract() {
|
||||
@Test
|
||||
fun `run up query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||
val resources = this::class.java.getResource("/sql/migrations").toURI()
|
||||
val m = Migrations(resources, connection)
|
||||
m.up().apply {
|
||||
this `should contain` Pair("1", Migration.Status.OK)
|
||||
@@ -28,7 +27,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `migration up Query should throw error if no down`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migration_without_down").toURI())
|
||||
val resources = this::class.java.getResource("/sql/migration_without_down").toURI()
|
||||
invoking {
|
||||
Migrations(resources, connection)
|
||||
} shouldThrow Migrations.DownMigrationNotDefined::class
|
||||
@@ -36,7 +35,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run forced down query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||
val resources = this::class.java.getResource("/sql/migrations").toURI()
|
||||
val m = Migrations(resources, connection)
|
||||
repeat(3) {
|
||||
m.down(true).apply {
|
||||
@@ -48,7 +47,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run dry migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
val resources = this::class.java.getResource("/sql/real_migrations").toURI()
|
||||
Migrations(resources, connection).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
@@ -59,7 +58,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run dry migrations launch twice`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
val resources = this::class.java.getResource("/sql/real_migrations").toURI()
|
||||
Migrations(resources, connection).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
runDry().size `should be equal to` 2
|
||||
@@ -68,7 +67,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
val resources = this::class.java.getResource("/sql/real_migrations").toURI()
|
||||
Migrations(resources, connection).apply {
|
||||
run().apply {
|
||||
size `should be equal to` 1
|
||||
@@ -78,8 +77,8 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run migrations force down`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
val resourcesFunctions = File(this::class.java.getResource("/sql/function/Test").toURI())
|
||||
val resources = this::class.java.getResource("/sql/real_migrations").toURI()
|
||||
val resourcesFunctions = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
Migrations(listOf(resources, resourcesFunctions), connection).apply {
|
||||
up().apply {
|
||||
size `should be equal to` 6
|
||||
@@ -94,7 +93,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run functions migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function/Test").toURI())
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
Migrations(resources, connection).apply {
|
||||
run().size `should be equal to` 5
|
||||
}
|
||||
@@ -110,7 +109,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run functions migrations and drop if exist`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function/Test1").toURI())
|
||||
val resources = this::class.java.getResource("/sql/function/Test1").toURI()
|
||||
Migrations(resources, connection).apply {
|
||||
run().size `should be equal to` 1
|
||||
}
|
||||
@@ -123,7 +122,7 @@ class MigrationTest() : TestAbstract() {
|
||||
Assertions.assertEquals(objTest!!.id, 3)
|
||||
Assertions.assertEquals(objTest.name, "test")
|
||||
|
||||
val resources2 = File(this::class.java.getResource("/sql/function/Test2").toURI())
|
||||
val resources2 = this::class.java.getResource("/sql/function/Test2").toURI()
|
||||
Migrations(resources2, connection).apply {
|
||||
run().size `should be equal to` 1
|
||||
}
|
||||
|
||||
@@ -6,17 +6,16 @@ import fr.postgresjson.entity.mutable.IdEntity
|
||||
import org.junit.Assert
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
|
||||
class RequesterTest : TestAbstract() {
|
||||
class ObjTest(var name: String) : IdEntity(1)
|
||||
|
||||
@Test
|
||||
fun `get query from file`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val objTest: ObjTest? = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOne")
|
||||
.getQuery("selectOne")
|
||||
.selectOne()
|
||||
|
||||
assertEquals(objTest!!.id, 2)
|
||||
@@ -25,7 +24,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `get function from file`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function/Test").toURI())
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val objTest: ObjTest? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function")
|
||||
@@ -37,10 +36,10 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call exec on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val result = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOne")
|
||||
.getQuery("selectOne")
|
||||
.exec()
|
||||
|
||||
assertEquals(1, result.rowsAffected)
|
||||
@@ -48,7 +47,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call exec on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function/Test").toURI())
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val result = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function")
|
||||
@@ -58,11 +57,11 @@ class RequesterTest : TestAbstract() {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call sendQuery on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
fun `call sendQuery on query with name`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val result = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/exec")
|
||||
.getQuery("DeleteTest")
|
||||
.sendQuery()
|
||||
|
||||
assertEquals(0, result)
|
||||
@@ -70,7 +69,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call sendQuery on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function/Test").toURI())
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val result = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("function_void")
|
||||
@@ -81,7 +80,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call selectOne on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function/Test").toURI())
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function")
|
||||
@@ -92,7 +91,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call selectOne on function with object`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function/Test").toURI())
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val obj2 = ObjTest("original")
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addFunction(resources)
|
||||
@@ -105,10 +104,10 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call selectOne on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOneWithParameters")
|
||||
.getQuery("selectOneWithParameters")
|
||||
.selectOne(mapOf("name" to "myName"))!!
|
||||
|
||||
assertEquals("myName", obj.name)
|
||||
@@ -116,7 +115,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call select (multiple) on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function/Test").toURI())
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val obj: List<ObjTest>? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function_multiple")
|
||||
@@ -127,10 +126,10 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call select paginated on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val result: Paginated<ObjTest> = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectPaginated")
|
||||
.getQuery("selectPaginated")
|
||||
.select(1, 2, mapOf("name" to "ff"))
|
||||
Assert.assertNotNull(result)
|
||||
Assert.assertEquals("ff", result.result[0].name)
|
||||
@@ -141,7 +140,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call select paginated on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val resources = this::class.java.getResource("/sql/function").toURI()
|
||||
val result: Paginated<ObjTest> = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function_paginated")
|
||||
@@ -155,10 +154,10 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call selectOne on query with extra parameter`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOneWithParameters")
|
||||
.getQuery("selectOneWithParameters")
|
||||
.selectOne(mapOf("name" to "myName")) {
|
||||
assertEquals("myName", it!!.name)
|
||||
Assert.assertEquals("plop", rows[0].getString("other"))
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.io.File
|
||||
|
||||
@TestInstance(PER_CLASS)
|
||||
abstract class TestAbstract {
|
||||
protected val connection = Connection(database = "test_json", username = "test", password = "test")
|
||||
protected val connection = Connection(database = "json_test", username = "test", password = "test", port = 5555)
|
||||
|
||||
@BeforeEach
|
||||
fun beforeAll() {
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
-- name: DeleteTest
|
||||
delete FROM test where 2038538 = 2;
|
||||
Reference in New Issue
Block a user