improve-tests #28
@@ -4,13 +4,13 @@ import fr.postgresjson.utils.searchSqlFiles
|
||||
import java.net.URI
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
|
||||
fun DefinitionFunction.toConnection(connection: Connection): Function = Function(this, connection)
|
||||
fun DefinitionFunction.toRunnable(connection: Connection): Function = Function(this, connection)
|
||||
|
||||
fun Sequence<DefinitionFunction>.toConnection(connection: Connection): Sequence<Function> = map { it.toConnection(connection) }
|
||||
fun Sequence<DefinitionFunction>.toRunnable(connection: Connection): Sequence<Function> = map { it.toRunnable(connection) }
|
||||
|
||||
fun Sequence<Function>.toMutableMap(): MutableMap<String, Function> = map { it.name to it }.toMap().toMutableMap()
|
||||
|
||||
internal fun URI.toFunction(connection: Connection): MutableMap<String, Function> = searchSqlFiles()
|
||||
.filterIsInstance(DefinitionFunction::class.java)
|
||||
.toConnection(connection)
|
||||
.toRunnable(connection)
|
||||
.toMutableMap()
|
||||
|
||||
@@ -4,13 +4,13 @@ import fr.postgresjson.utils.searchSqlFiles
|
||||
import java.net.URI
|
||||
import fr.postgresjson.definition.Query as QueryDefinition
|
||||
|
||||
fun QueryDefinition.toConnection(connection: Connection): Query = Query(name, script, connection)
|
||||
fun QueryDefinition.toRunnable(connection: Connection): Query = Query(name, script, connection)
|
||||
|
||||
fun Sequence<QueryDefinition>.toConnection(connection: Connection): Sequence<Query> = map { it.toConnection(connection) }
|
||||
fun Sequence<QueryDefinition>.toRunnable(connection: Connection): Sequence<Query> = map { it.toRunnable(connection) }
|
||||
|
||||
fun Sequence<Query>.toMutableMap(): MutableMap<String, Query> = map { it.name to it }.toMap().toMutableMap()
|
||||
|
||||
internal fun URI.toQuery(connection: Connection): MutableMap<String, Query> = searchSqlFiles()
|
||||
.filterIsInstance(QueryDefinition::class.java)
|
||||
.toConnection(connection)
|
||||
.toRunnable(connection)
|
||||
.toMutableMap()
|
||||
|
||||
@@ -22,67 +22,46 @@ class Requester(
|
||||
functions = functionsDirectory?.toFunction(connection) ?: mutableMapOf(),
|
||||
)
|
||||
|
||||
fun addQuery(query: Query): Requester {
|
||||
fun addQuery(query: Query) {
|
||||
queries[query.name] = query
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(query: QueryDefinition): Requester = addQuery(query.name, query.script)
|
||||
fun addQuery(query: QueryDefinition) = addQuery(query.toRunnable(connection))
|
||||
|
||||
fun addQuery(name: String, sql: String): Requester {
|
||||
fun addQuery(name: String, sql: String) {
|
||||
addQuery(Query(name, sql, connection))
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(queriesDirectory: URI): Requester {
|
||||
queriesDirectory.searchSqlFiles()
|
||||
.forEach {
|
||||
if (it is QueryDefinition) {
|
||||
addQuery(it)
|
||||
}
|
||||
}
|
||||
return this
|
||||
fun addQuery(queriesDirectory: URI) {
|
||||
queriesDirectory
|
||||
.searchSqlFiles()
|
||||
.filterIsInstance(QueryDefinition::class.java)
|
||||
.forEach(this::addQuery)
|
||||
}
|
||||
|
||||
fun getQueries(): List<Query> {
|
||||
return queries.map { it.value }
|
||||
fun getQueries(): List<Query> = queries.map { it.value }
|
||||
|
||||
fun addFunction(definition: DefinitionFunction) {
|
||||
definition
|
||||
.run { toRunnable(connection) }
|
||||
.run { functions[name] = this }
|
||||
}
|
||||
|
||||
fun addFunction(definition: DefinitionFunction): Requester {
|
||||
functions[definition.name] = Function(definition, connection)
|
||||
return this
|
||||
fun addFunction(sql: String) {
|
||||
DefinitionFunction(sql)
|
||||
.run { toRunnable(connection) }
|
||||
.run { functions[name] = this }
|
||||
}
|
||||
|
||||
fun addFunction(sql: String): Requester {
|
||||
DefinitionFunction(sql).let {
|
||||
functions[it.name] = Function(it, connection)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun addFunction(functionsDirectory: URI): Requester {
|
||||
fun addFunctions(functionsDirectory: URI) {
|
||||
functionsDirectory.searchSqlFiles()
|
||||
.forEach {
|
||||
if (it is DefinitionFunction) {
|
||||
addFunction(it)
|
||||
}
|
||||
}
|
||||
return this
|
||||
.filterIsInstance(DefinitionFunction::class.java)
|
||||
.forEach(this::addFunction)
|
||||
}
|
||||
|
||||
fun getFunction(name: String): Function {
|
||||
if (functions[name] === null) {
|
||||
throw NoFunctionDefined(name)
|
||||
}
|
||||
return functions[name]!!
|
||||
}
|
||||
fun getFunction(name: String): Function = functions[name] ?: throw NoFunctionDefined(name)
|
||||
|
||||
fun getQuery(path: String): Query {
|
||||
if (queries[path] === null) {
|
||||
throw NoQueryDefined(path)
|
||||
}
|
||||
return queries[path]!!
|
||||
}
|
||||
fun getQuery(path: String): Query = queries[path] ?: throw NoQueryDefined(path)
|
||||
|
||||
class NoFunctionDefined(name: String) : Exception("No function defined for $name")
|
||||
class NoQueryDefined(path: String) : Exception("No query defined in $path")
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.nio.file.Path
|
||||
|
||||
class Function(
|
||||
override val script: String,
|
||||
override var source: Path? = null
|
||||
override val source: Path? = null
|
||||
) : Resource, ParametersInterface {
|
||||
val returns: String
|
||||
override val name: String
|
||||
|
||||
@@ -4,14 +4,12 @@ import java.nio.file.Path
|
||||
|
||||
class Migration(
|
||||
override val script: String,
|
||||
source: Path
|
||||
override var 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 {
|
||||
|
||||
@@ -4,9 +4,8 @@ import java.nio.file.Path
|
||||
|
||||
class Query(
|
||||
override val script: String,
|
||||
source: Path
|
||||
override var source: Path
|
||||
) : Resource {
|
||||
override var source: Path? = source
|
||||
override val name: String = getNameFromComment(script) ?: getNameFromFile(source)
|
||||
|
||||
/** Try to get name from comment in file */
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.nio.file.Path
|
||||
sealed interface Resource {
|
||||
val name: String
|
||||
val script: String
|
||||
var source: Path?
|
||||
val source: Path?
|
||||
|
||||
open class ParseException(message: String, cause: Throwable? = null) : Exception(message, cause)
|
||||
|
||||
|
||||
@@ -99,8 +99,7 @@ class MigrationTest() : TestAbstract() {
|
||||
run().size `should be equal to` 5
|
||||
}
|
||||
|
||||
val objTest: RequesterTest.ObjTest? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val objTest: RequesterTest.ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(listOf("test", "plip"))
|
||||
|
||||
@@ -115,8 +114,7 @@ class MigrationTest() : TestAbstract() {
|
||||
run().size `should be equal to` 1
|
||||
}
|
||||
|
||||
val objTest: RequesterTest.ObjTest? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val objTest: RequesterTest.ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_duplicate")
|
||||
.selectOne(listOf("test"))
|
||||
|
||||
|
||||
@@ -15,10 +15,10 @@ class RequesterTest : TestAbstract() {
|
||||
class ObjTest(val name: String, id: UUID = UUID.fromString("5623d902-3067-42f3-bfd9-095dbb12c29f")) : UuidEntity(id)
|
||||
|
||||
@Test
|
||||
fun `function toString`() {
|
||||
fun `requester constructor empty`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val name: String = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.apply { addFunctions(resources) }
|
||||
.getFunction("test_function")
|
||||
.name
|
||||
|
||||
@@ -58,18 +58,29 @@ class RequesterTest : TestAbstract() {
|
||||
$$
|
||||
""".trimIndent()
|
||||
val name: String = Requester(connection)
|
||||
.addFunction(sql)
|
||||
.apply { addFunction(sql) }
|
||||
.getFunction("test_function")
|
||||
.name
|
||||
|
||||
assertEquals("test_function", name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `add query from string`() {
|
||||
val result: Int = Requester(connection)
|
||||
.apply { addQuery("simpleTest", "select 42;") }
|
||||
.getQuery("simpleTest")
|
||||
.exec()
|
||||
.rows[0].getInt(0)!!
|
||||
|
||||
assertEquals(result, 42)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get query from file`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val objTest: ObjTest? = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.apply { addQuery(resources) }
|
||||
.getQuery("selectOne")
|
||||
.selectOne()
|
||||
|
||||
@@ -81,8 +92,7 @@ class RequesterTest : TestAbstract() {
|
||||
fun `get query from file with wrong name throw exception`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
assertThrows(NoQueryDefined::class.java) {
|
||||
Requester(connection)
|
||||
.addQuery(resources)
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("wrongName")
|
||||
}
|
||||
}
|
||||
@@ -90,8 +100,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `get queries from file`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val name: String = Requester(connection)
|
||||
.addQuery(resources)
|
||||
val name: String = Requester(connection, queriesDirectory = resources)
|
||||
.getQueries()[0].name
|
||||
|
||||
assertEquals(name, "DeleteTest")
|
||||
@@ -101,8 +110,7 @@ class RequesterTest : TestAbstract() {
|
||||
fun `get function from file with wrong name throw exception`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
assertThrows(NoFunctionDefined::class.java) {
|
||||
Requester(connection)
|
||||
.addFunction(resources)
|
||||
Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("wrongName")
|
||||
}
|
||||
}
|
||||
@@ -110,8 +118,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `get function from file`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val objTest: ObjTest? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val objTest: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(listOf("test", "plip"))
|
||||
|
||||
@@ -122,8 +129,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call exec on query`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val result = Requester(connection)
|
||||
.addQuery(resources)
|
||||
val result = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectOne")
|
||||
.exec()
|
||||
|
||||
@@ -133,8 +139,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call exec on function`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val result = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val result = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.exec(listOf("test", "plip"))
|
||||
|
||||
@@ -144,8 +149,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call sendQuery on query with name`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val result = Requester(connection)
|
||||
.addQuery(resources)
|
||||
val result = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("DeleteTest")
|
||||
.sendQuery()
|
||||
|
||||
@@ -155,8 +159,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call sendQuery on function`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val result = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val result = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("function_void")
|
||||
.sendQuery(listOf("test"))
|
||||
|
||||
@@ -166,8 +169,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call selectOne on function`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(mapOf("name" to "myName"))!!
|
||||
|
||||
@@ -178,8 +180,7 @@ class RequesterTest : TestAbstract() {
|
||||
fun `call selectOne on function with object and named argument`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val obj2 = ObjTest("original")
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_object")
|
||||
.selectOne("resource" to obj2)!!
|
||||
|
||||
@@ -191,8 +192,7 @@ class RequesterTest : TestAbstract() {
|
||||
fun `call selectOne on function with object`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val obj2 = ObjTest("original")
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_object")
|
||||
.selectOne(obj2)!!
|
||||
|
||||
@@ -203,8 +203,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call selectOne on function with object and no arguments`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne()!!
|
||||
|
||||
@@ -214,8 +213,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call selectOne on query`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addQuery(resources)
|
||||
val obj: ObjTest = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectOneWithParameters")
|
||||
.selectOne(mapOf("name" to "myName"))!!
|
||||
|
||||
@@ -225,8 +223,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call select (multiple) on function`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val obj: List<ObjTest>? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val obj: List<ObjTest>? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_multiple")
|
||||
.select(mapOf("name" to "myName"))
|
||||
|
||||
@@ -236,8 +233,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call select paginated on query`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val result: Paginated<ObjTest> = Requester(connection)
|
||||
.addQuery(resources)
|
||||
val result: Paginated<ObjTest> = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectPaginated")
|
||||
.select(1, 2, mapOf("name" to "ff"))
|
||||
Assert.assertNotNull(result)
|
||||
@@ -250,8 +246,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call select paginated on function`() {
|
||||
val resources = this::class.java.getResource("/sql/function").toURI()
|
||||
val result: Paginated<ObjTest> = Requester(connection)
|
||||
.addFunction(resources)
|
||||
val result: Paginated<ObjTest> = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_paginated")
|
||||
.select(1, 2, mapOf("name" to "ff"))
|
||||
Assert.assertNotNull(result)
|
||||
@@ -264,8 +259,7 @@ class RequesterTest : TestAbstract() {
|
||||
@Test
|
||||
fun `call selectOne on query with extra parameter`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addQuery(resources)
|
||||
val obj: ObjTest = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectOneWithParameters")
|
||||
.selectOne(mapOf("name" to "myName")) {
|
||||
assertEquals("myName", it!!.name)
|
||||
|
||||
Reference in New Issue
Block a user