Minor fix & clean #32
@@ -41,7 +41,7 @@ tasks.test {
|
|||||||
useJUnit()
|
useJUnit()
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
systemProperty("junit.jupiter.execution.parallel.enabled", true)
|
systemProperty("junit.jupiter.execution.parallel.enabled", true)
|
||||||
if (disableLint.toBoolean() == false) {
|
if (!disableLint.toBoolean()) {
|
||||||
finalizedBy(tasks.ktlintCheck)
|
finalizedBy(tasks.ktlintCheck)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ services:
|
|||||||
context: docker/postgresql
|
context: docker/postgresql
|
||||||
restart: always
|
restart: always
|
||||||
ports:
|
ports:
|
||||||
- 5555:5432
|
- "5555:5432"
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: json_test
|
POSTGRES_DB: json_test
|
||||||
POSTGRES_USER: test
|
POSTGRES_USER: test
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ data class Paginated<T : EntityI>(
|
|||||||
val totalPages: Int = (total.toDouble() / limit.toDouble()).ceil()
|
val totalPages: Int = (total.toDouble() / limit.toDouble()).ceil()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (offset < 0) error("offset must be greather or equal than 0")
|
if (offset < 0) error("offset must be greater or equal than 0")
|
||||||
if (limit < 1) error("limit must be greather or equal than 1")
|
if (limit < 1) error("limit must be greater or equal than 1")
|
||||||
if (total < 0) error("total must be greather or equal than 0")
|
if (total < 0) error("total must be greater or equal than 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isLastPage(): Boolean = currentPage >= totalPages
|
fun isLastPage(): Boolean = currentPage >= totalPages
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class Function(
|
|||||||
|
|
||||||
val queryMatch = functionRegex.find(script)
|
val queryMatch = functionRegex.find(script)
|
||||||
if (queryMatch !== null) {
|
if (queryMatch !== null) {
|
||||||
val functionName = queryMatch.groups.get("name")?.value?.trim() ?: error("Function name not found")
|
val functionName = queryMatch.groups["name"]?.value?.trim() ?: error("Function name not found")
|
||||||
val functionParameters = queryMatch.groups["params"]?.value?.trim()
|
val functionParameters = queryMatch.groups["params"]?.value?.trim()
|
||||||
this.returns = queryMatch.groups["return"]?.value?.trim() ?: ""
|
this.returns = queryMatch.groups["return"]?.value?.trim() ?: ""
|
||||||
|
|
||||||
@@ -51,15 +51,12 @@ class Function(
|
|||||||
fun getDefinition(): String {
|
fun getDefinition(): String {
|
||||||
return parameters
|
return parameters
|
||||||
.filter { it.direction == Parameter.Direction.IN }
|
.filter { it.direction == Parameter.Direction.IN }
|
||||||
.joinToString(", ") { "${it.name} ${it.type}" }.let {
|
.joinToString(", ") { "${it.name} ${it.type}" }
|
||||||
"$name ($it)"
|
.let { "$name ($it)" }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getParametersIndexedByName(): Map<String, Parameter> {
|
fun getParametersIndexedByName(): Map<String, Parameter> {
|
||||||
return parameters.map {
|
return parameters.associateBy { it.name }
|
||||||
it.name to it
|
|
||||||
}.toMap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
infix fun `has same definition`(other: Function): Boolean {
|
infix fun `has same definition`(other: Function): Boolean {
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ data class Function(
|
|||||||
)
|
)
|
||||||
|
|
||||||
override fun up(): Status {
|
override fun up(): Status {
|
||||||
|
return try {
|
||||||
try {
|
try {
|
||||||
connection.sendQuery(up.script)
|
connection.sendQuery(up.script)
|
||||||
} catch (e: CompletionException) {
|
} catch (e: CompletionException) {
|
||||||
@@ -55,10 +56,14 @@ data class Function(
|
|||||||
doExecute = Action.OK
|
doExecute = Action.OK
|
||||||
}
|
}
|
||||||
|
|
||||||
return Status.OK
|
Status.OK
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
Status.UP_FAIL
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun down(): Status {
|
override fun down(): Status {
|
||||||
|
return try {
|
||||||
connection.sendQuery(down.script)
|
connection.sendQuery(down.script)
|
||||||
|
|
||||||
this::class.java.classLoader
|
this::class.java.classLoader
|
||||||
@@ -66,7 +71,10 @@ data class Function(
|
|||||||
.readText()
|
.readText()
|
||||||
.let { connection.sendQuery(it, listOf(down.name)) }
|
.let { connection.sendQuery(it, listOf(down.name)) }
|
||||||
|
|
||||||
return Status.OK
|
Status.OK
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
Status.DOWN_FAIL
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun test(): Status {
|
override fun test(): Status {
|
||||||
|
|||||||
@@ -29,11 +29,11 @@ interface Migration {
|
|||||||
fun down(): Status
|
fun down(): Status
|
||||||
fun test(): Status
|
fun test(): Status
|
||||||
|
|
||||||
enum class Status(i: Int) { OK(2), UP_FAIL(0), DOWN_FAIL(1) }
|
enum class Status(val i: Int) { OK(2), UP_FAIL(0), DOWN_FAIL(1) }
|
||||||
enum class Action { OK, UP, DOWN }
|
enum class Action { OK, UP, DOWN }
|
||||||
}
|
}
|
||||||
|
|
||||||
data class Migrations private constructor(
|
class Migrations private constructor(
|
||||||
private val connection: Connection,
|
private val connection: Connection,
|
||||||
private val migrationsScripts: MutableMap<String, MigrationScript> = mutableMapOf(),
|
private val migrationsScripts: MutableMap<String, MigrationScript> = mutableMapOf(),
|
||||||
private val functions: MutableMap<String, Function> = mutableMapOf()
|
private val functions: MutableMap<String, Function> = mutableMapOf()
|
||||||
@@ -49,7 +49,7 @@ data class Migrations private constructor(
|
|||||||
reset()
|
reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun reset() {
|
private fun reset() {
|
||||||
migrationsScripts.clear()
|
migrationsScripts.clear()
|
||||||
functions.clear()
|
functions.clear()
|
||||||
|
|
||||||
@@ -76,14 +76,14 @@ data class Migrations private constructor(
|
|||||||
*/
|
*/
|
||||||
private fun getMigrationFromDB() {
|
private fun getMigrationFromDB() {
|
||||||
this::class.java.classLoader.getResource("sql/migration/findAllFunction.sql")!!.readText().let {
|
this::class.java.classLoader.getResource("sql/migration/findAllFunction.sql")!!.readText().let {
|
||||||
connection.select<MigrationEntity>(it, object : TypeReference<List<MigrationEntity>>() {})
|
connection.select(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||||
.map { function ->
|
.map { function ->
|
||||||
functions[function.filename] = Function(function.up, function.down, connection, function.executedAt)
|
functions[function.filename] = Function(function.up, function.down, connection, function.executedAt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let {
|
this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let {
|
||||||
connection.select<MigrationEntity>(it, object : TypeReference<List<MigrationEntity>>() {})
|
connection.select(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||||
.map { query ->
|
.map { query ->
|
||||||
migrationsScripts[query.filename] = MigrationScript(query.filename, query.up, query.down, connection, query.executedAt)
|
migrationsScripts[query.filename] = MigrationScript(query.filename, query.up, query.down, connection, query.executedAt)
|
||||||
}
|
}
|
||||||
@@ -130,7 +130,7 @@ data class Migrations private constructor(
|
|||||||
enum class Direction { UP, DOWN }
|
enum class Direction { UP, DOWN }
|
||||||
|
|
||||||
internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException? = null) :
|
internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException? = null) :
|
||||||
Throwable("The file $path whas not found", cause)
|
Throwable("The file $path was not found", cause)
|
||||||
|
|
||||||
fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations {
|
fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations {
|
||||||
val currentFunction = functions[newDefinition.name]
|
val currentFunction = functions[newDefinition.name]
|
||||||
@@ -174,10 +174,10 @@ data class Migrations private constructor(
|
|||||||
|
|
||||||
private fun initDB() {
|
private fun initDB() {
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
this::class.java.classLoader.getResource("sql/migration/createHistoryShema.sql")!!.readText().let {
|
this::class.java.classLoader.getResource("sql/migration/createHistorySchema.sql")!!.readText().let {
|
||||||
connection.sendQuery(it)
|
connection.sendQuery(it)
|
||||||
}
|
}
|
||||||
this::class.java.classLoader.getResource("sql/migration/createFunctionShema.sql")!!.readText().let {
|
this::class.java.classLoader.getResource("sql/migration/createFunctionSchema.sql")!!.readText().let {
|
||||||
connection.sendQuery(it)
|
connection.sendQuery(it)
|
||||||
}
|
}
|
||||||
initialized = true
|
initialized = true
|
||||||
@@ -297,7 +297,7 @@ data class Migrations private constructor(
|
|||||||
return list.toMap()
|
return list.toMap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun copy(): Migrations {
|
private fun copy(): Migrations {
|
||||||
val queriesCopy = migrationsScripts.map {
|
val queriesCopy = migrationsScripts.map {
|
||||||
it.key to it.value.copy()
|
it.key to it.value.copy()
|
||||||
}.toMap().toMutableMap()
|
}.toMap().toMutableMap()
|
||||||
|
|||||||
@@ -6,5 +6,5 @@ import kotlin.properties.ReadOnlyProperty
|
|||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
internal class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
internal class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
||||||
override fun getValue(thisRef: R, property: KProperty<*>) = LoggerFactory.getLogger(thisRef.javaClass.packageName)
|
override fun getValue(thisRef: R, property: KProperty<*>): Logger = LoggerFactory.getLogger(thisRef.javaClass.packageName)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import kotlin.streams.asSequence
|
|||||||
|
|
||||||
fun URL.searchSqlFiles() = this.toURI().searchSqlFiles()
|
fun URL.searchSqlFiles() = this.toURI().searchSqlFiles()
|
||||||
|
|
||||||
fun URI.searchSqlFiles() = sequence<Resource> {
|
fun URI.searchSqlFiles() = sequence {
|
||||||
val logger: Logger = LoggerFactory.getLogger("sqlFilesSearch")
|
val logger: Logger = LoggerFactory.getLogger("sqlFilesSearch")
|
||||||
val uri: URI = this@searchSqlFiles
|
val uri: URI = this@searchSqlFiles
|
||||||
logger.debug("""SQL files found in "${uri.toString().substringAfter('!')}" :""")
|
logger.debug("""SQL files found in "${uri.toString().substringAfter('!')}" :""")
|
||||||
|
|||||||
@@ -9,16 +9,15 @@ import fr.postgresjson.entity.Parameter
|
|||||||
import fr.postgresjson.entity.UuidEntity
|
import fr.postgresjson.entity.UuidEntity
|
||||||
import fr.postgresjson.serializer.deserialize
|
import fr.postgresjson.serializer.deserialize
|
||||||
import fr.postgresjson.serializer.toTypeReference
|
import fr.postgresjson.serializer.toTypeReference
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Assert.assertNotNull
|
|
||||||
import org.junit.Assert.assertTrue
|
|
||||||
import org.junit.jupiter.api.Assertions
|
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import kotlin.test.assertContains
|
import kotlin.test.assertContains
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertNotNull
|
||||||
import kotlin.test.assertNull
|
import kotlin.test.assertNull
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
class ConnectionTest : TestAbstract() {
|
class ConnectionTest : TestAbstract() {
|
||||||
@@ -32,7 +31,7 @@ class ConnectionTest : TestAbstract() {
|
|||||||
fun getObject() {
|
fun getObject() {
|
||||||
val obj: ObjTest? = connection.selectOne("select to_json(a) from test a limit 1")
|
val obj: ObjTest? = connection.selectOne("select to_json(a) from test a limit 1")
|
||||||
assertTrue(obj is ObjTest)
|
assertTrue(obj is ObjTest)
|
||||||
assertTrue(obj!!.id == UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
|
assertEquals(UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"), obj.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -60,7 +59,7 @@ class ConnectionTest : TestAbstract() {
|
|||||||
fun `test call request with args`() {
|
fun `test call request with args`() {
|
||||||
val result: ObjTest? = connection.selectOne("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::text)", listOf("myName"))
|
val result: ObjTest? = connection.selectOne("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::text)", listOf("myName"))
|
||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertEquals("myName", result!!.name)
|
assertEquals("myName", result.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -69,7 +68,7 @@ class ConnectionTest : TestAbstract() {
|
|||||||
assertEquals("myName", this.rows[0].getString(0)?.deserialize<ObjTest>()?.name)
|
assertEquals("myName", this.rows[0].getString(0)?.deserialize<ObjTest>()?.name)
|
||||||
}
|
}
|
||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertEquals("myName", result!!.name)
|
assertEquals("myName", result.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -89,9 +88,8 @@ class ConnectionTest : TestAbstract() {
|
|||||||
val o = ObjTest("myName", id = UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"))
|
val o = ObjTest("myName", id = UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"))
|
||||||
val obj: ObjTest? = connection.selectOne("select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id uuid, name text);", listOf(o))
|
val obj: ObjTest? = connection.selectOne("select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id uuid, name text);", listOf(o))
|
||||||
assertNotNull(obj)
|
assertNotNull(obj)
|
||||||
assertTrue(obj is ObjTest)
|
assertEquals(UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"), obj.id)
|
||||||
assertEquals(obj!!.id, UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"))
|
assertEquals("myName", obj.name)
|
||||||
assertEquals(obj.name, "myName")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -99,15 +97,15 @@ class ConnectionTest : TestAbstract() {
|
|||||||
val obj = ObjTest("before", id = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
|
val obj = ObjTest("before", id = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
|
||||||
val objUpdated: ObjTest? = connection.update("select ?::jsonb || jsonb_build_object('name', 'after');", obj.toTypeReference(), obj)
|
val objUpdated: ObjTest? = connection.update("select ?::jsonb || jsonb_build_object('name', 'after');", obj.toTypeReference(), obj)
|
||||||
assertTrue(objUpdated is ObjTest)
|
assertTrue(objUpdated is ObjTest)
|
||||||
assertTrue(objUpdated!!.id == UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
|
assertEquals(UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"), objUpdated.id)
|
||||||
assertTrue(objUpdated.name == "after")
|
assertEquals("after", objUpdated.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun callExec() {
|
fun callExec() {
|
||||||
val o = ObjTest("myName")
|
val o = ObjTest("myName")
|
||||||
val result = connection.exec("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::json->>'name')", listOf(o))
|
val result = connection.exec("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::json->>'name')", listOf(o))
|
||||||
Assertions.assertEquals(1, result.rowsAffected)
|
assertEquals(1, result.rowsAffected)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -120,9 +118,10 @@ class ConnectionTest : TestAbstract() {
|
|||||||
"third" to 123
|
"third" to 123
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
assertEquals(result!!.first, "ff")
|
assertNotNull(result)
|
||||||
assertEquals(result.second, "sec")
|
assertEquals("ff", result.first)
|
||||||
assertEquals(result.third, 123)
|
assertEquals("sec", result.second)
|
||||||
|
assertEquals(123, result.third)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -134,7 +133,8 @@ class ConnectionTest : TestAbstract() {
|
|||||||
"second" to ParameterObject("two")
|
"second" to ParameterObject("two")
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
assertEquals("one", result!!.first.third)
|
assertNotNull(result)
|
||||||
|
assertEquals("one", result.first.third)
|
||||||
assertEquals("two", result.second.third)
|
assertEquals("two", result.second.third)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,9 +153,9 @@ class ConnectionTest : TestAbstract() {
|
|||||||
"second" to "sec"
|
"second" to "sec"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
assertEquals(result[0].first, "ff")
|
assertEquals("ff", result[0].first)
|
||||||
assertEquals(result[0].second, "sec")
|
assertEquals("sec", result[0].second)
|
||||||
assertEquals(result[0].third, 123)
|
assertEquals(123, result[0].third)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -171,9 +171,9 @@ class ConnectionTest : TestAbstract() {
|
|||||||
"third" to 123,
|
"third" to 123,
|
||||||
"second" to "sec"
|
"second" to "sec"
|
||||||
)
|
)
|
||||||
assertEquals(result[0].first, "ff")
|
assertEquals("ff", result[0].first)
|
||||||
assertEquals(result[0].second, "sec")
|
assertEquals("sec", result[0].second)
|
||||||
assertEquals(result[0].third, 123)
|
assertEquals(123, result[0].third)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -192,10 +192,10 @@ class ConnectionTest : TestAbstract() {
|
|||||||
|
|
||||||
)
|
)
|
||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertEquals(result.result[0].name, "ff")
|
assertEquals("ff", result.result[0].name)
|
||||||
assertEquals(result.result[1].name, "ff-2")
|
assertEquals("ff-2", result.result[1].name)
|
||||||
assertEquals(result.total, 10)
|
assertEquals(10, result.total)
|
||||||
assertEquals(result.offset, 0)
|
assertEquals(0, result.offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -214,8 +214,8 @@ class ConnectionTest : TestAbstract() {
|
|||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertTrue(result.result.isEmpty())
|
assertTrue(result.result.isEmpty())
|
||||||
assertEquals(0, result.result.size)
|
assertEquals(0, result.result.size)
|
||||||
assertEquals(result.total, 10)
|
assertEquals(10, result.total)
|
||||||
assertEquals(result.offset, 0)
|
assertEquals(0, result.offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -242,8 +242,8 @@ class ConnectionTest : TestAbstract() {
|
|||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertEquals("myName", result.result[0].name)
|
assertEquals("myName", result.result[0].name)
|
||||||
assertEquals(1, result.result.size)
|
assertEquals(1, result.result.size)
|
||||||
assertEquals(result.total, 10)
|
assertEquals(10, result.total)
|
||||||
assertEquals(result.offset, 0)
|
assertEquals(0, result.offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -336,11 +336,12 @@ class ConnectionTest : TestAbstract() {
|
|||||||
""".trimIndent(),
|
""".trimIndent(),
|
||||||
params
|
params
|
||||||
) {
|
) {
|
||||||
assertEquals("ff", it!!.first)
|
assertNotNull(it)
|
||||||
|
assertEquals("ff", it.first)
|
||||||
assertEquals("plop", rows[0].getString("other"))
|
assertEquals("plop", rows[0].getString("other"))
|
||||||
}
|
}
|
||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertEquals("ff", result!!.first)
|
assertEquals("ff", result.first)
|
||||||
assertEquals("sec", result.second)
|
assertEquals("sec", result.second)
|
||||||
assertEquals(123, result.third)
|
assertEquals(123, result.third)
|
||||||
}
|
}
|
||||||
@@ -371,7 +372,8 @@ class ConnectionTest : TestAbstract() {
|
|||||||
"second" to ParameterObject("two")
|
"second" to ParameterObject("two")
|
||||||
)
|
)
|
||||||
).let { result ->
|
).let { result ->
|
||||||
assertEquals("one", result!!.first.third)
|
assertNotNull(result)
|
||||||
|
assertEquals("one", result.first.third)
|
||||||
assertEquals("two", result.second.third)
|
assertEquals("two", result.second.third)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ import fr.postgresjson.connexion.selectOne
|
|||||||
import fr.postgresjson.connexion.update
|
import fr.postgresjson.connexion.update
|
||||||
import fr.postgresjson.entity.UuidEntity
|
import fr.postgresjson.entity.UuidEntity
|
||||||
import fr.postgresjson.serializer.deserialize
|
import fr.postgresjson.serializer.deserialize
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
|
||||||
import org.junit.jupiter.api.Assertions.assertThrows
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
|
import kotlin.test.assertEquals
|
||||||
import kotlin.test.assertNotNull
|
import kotlin.test.assertNotNull
|
||||||
|
|
||||||
class RequesterTest : TestAbstract() {
|
class RequesterTest : TestAbstract() {
|
||||||
@@ -83,13 +83,14 @@ class RequesterTest : TestAbstract() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `add query from string`() {
|
fun `add query from string`() {
|
||||||
val result: Int = Requester(connection)
|
val result: Int? = Requester(connection)
|
||||||
.apply { addQuery("simpleTest", "select 42;") }
|
.apply { addQuery("simpleTest", "select 42;") }
|
||||||
.getQuery("simpleTest")
|
.getQuery("simpleTest")
|
||||||
.exec()
|
.exec()
|
||||||
.rows[0].getInt(0)!!
|
.rows[0].getInt(0)
|
||||||
|
|
||||||
assertEquals(result, 42)
|
assertNotNull(result)
|
||||||
|
assertEquals(42, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -100,8 +101,9 @@ class RequesterTest : TestAbstract() {
|
|||||||
.getQuery("selectOne")
|
.getQuery("selectOne")
|
||||||
.selectOne()
|
.selectOne()
|
||||||
|
|
||||||
assertEquals(objTest!!.id, UUID.fromString("829b1a29-5db8-47f9-9562-961c561ac528"))
|
assertNotNull(objTest)
|
||||||
assertEquals(objTest.name, "test")
|
assertEquals(objTest.id, UUID.fromString("829b1a29-5db8-47f9-9562-961c561ac528"))
|
||||||
|
assertEquals("test", objTest.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -138,8 +140,9 @@ class RequesterTest : TestAbstract() {
|
|||||||
.getFunction("test_function")
|
.getFunction("test_function")
|
||||||
.selectOne(listOf("test", "plip"))
|
.selectOne(listOf("test", "plip"))
|
||||||
|
|
||||||
assertEquals(objTest!!.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d"))
|
assertNotNull(objTest)
|
||||||
assertEquals(objTest.name, "test")
|
assertEquals(objTest.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d"))
|
||||||
|
assertEquals("test", objTest.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -284,10 +287,11 @@ class RequesterTest : TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `call selectOne on function`() {
|
fun `call selectOne on function`() {
|
||||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
val obj: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||||
.getFunction("test_function")
|
.getFunction("test_function")
|
||||||
.selectOne(mapOf("name" to "myName"))!!
|
.selectOne(mapOf("name" to "myName"))
|
||||||
|
|
||||||
|
assertNotNull(obj)
|
||||||
assertEquals("myName", obj.name)
|
assertEquals("myName", obj.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,10 +299,11 @@ class RequesterTest : TestAbstract() {
|
|||||||
fun `call selectOne on function with object and named argument`() {
|
fun `call selectOne on function with object and named argument`() {
|
||||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||||
val obj2 = ObjTest("original")
|
val obj2 = ObjTest("original")
|
||||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
val obj: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||||
.getFunction("test_function_object")
|
.getFunction("test_function_object")
|
||||||
.selectOne("resource" to obj2)!!
|
.selectOne("resource" to obj2)
|
||||||
|
|
||||||
|
assertNotNull(obj)
|
||||||
assertEquals("changedName", obj.name)
|
assertEquals("changedName", obj.name)
|
||||||
assertEquals("original", obj2.name)
|
assertEquals("original", obj2.name)
|
||||||
}
|
}
|
||||||
@@ -307,10 +312,11 @@ class RequesterTest : TestAbstract() {
|
|||||||
fun `call selectOne on function with object`() {
|
fun `call selectOne on function with object`() {
|
||||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||||
val obj2 = ObjTest("original")
|
val obj2 = ObjTest("original")
|
||||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
val obj: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||||
.getFunction("test_function_object")
|
.getFunction("test_function_object")
|
||||||
.update(obj2)!!
|
.update(obj2)
|
||||||
|
|
||||||
|
assertNotNull(obj)
|
||||||
assertEquals("changedName", obj.name)
|
assertEquals("changedName", obj.name)
|
||||||
assertEquals("original", obj2.name)
|
assertEquals("original", obj2.name)
|
||||||
}
|
}
|
||||||
@@ -318,20 +324,22 @@ class RequesterTest : TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `call selectOne on function with object and no arguments`() {
|
fun `call selectOne on function with object and no arguments`() {
|
||||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
val obj: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||||
.getFunction("test_function")
|
.getFunction("test_function")
|
||||||
.selectOne()!!
|
.selectOne()
|
||||||
|
|
||||||
|
assertNotNull(obj)
|
||||||
assertEquals("plop", obj.name)
|
assertEquals("plop", obj.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `call selectOne on query`() {
|
fun `call selectOne on query`() {
|
||||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||||
val obj: ObjTest = Requester(connection, queriesDirectory = resources)
|
val obj: ObjTest? = Requester(connection, queriesDirectory = resources)
|
||||||
.getQuery("selectOneWithParameters")
|
.getQuery("selectOneWithParameters")
|
||||||
.selectOne(mapOf("name" to "myName"))!!
|
.selectOne(mapOf("name" to "myName"))
|
||||||
|
|
||||||
|
assertNotNull(obj)
|
||||||
assertEquals("myName", obj.name)
|
assertEquals("myName", obj.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,6 +350,8 @@ class RequesterTest : TestAbstract() {
|
|||||||
.getFunction("test_function_multiple")
|
.getFunction("test_function_multiple")
|
||||||
.select(mapOf("name" to "myName"))
|
.select(mapOf("name" to "myName"))
|
||||||
|
|
||||||
|
assertNotNull(obj[0])
|
||||||
|
assertEquals("myName", obj[0].name)
|
||||||
assertEquals("myName", obj[0].name)
|
assertEquals("myName", obj[0].name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,7 +376,7 @@ class RequesterTest : TestAbstract() {
|
|||||||
assertEquals("ff-2", result[1].name)
|
assertEquals("ff-2", result[1].name)
|
||||||
}
|
}
|
||||||
}.apply {
|
}.apply {
|
||||||
select<ObjTest>(object : TypeReference<List<ObjTest>>() {}, mapOf("name" to "ff")).let { result ->
|
select(object : TypeReference<List<ObjTest>>() {}, mapOf("name" to "ff")).let { result ->
|
||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertEquals("ff", result[0].name)
|
assertEquals("ff", result[0].name)
|
||||||
assertEquals("ff-2", result[1].name)
|
assertEquals("ff-2", result[1].name)
|
||||||
@@ -385,7 +395,7 @@ class RequesterTest : TestAbstract() {
|
|||||||
assertEquals("ff-2", result[1].name)
|
assertEquals("ff-2", result[1].name)
|
||||||
}
|
}
|
||||||
}.apply {
|
}.apply {
|
||||||
select<ObjTest>(object : TypeReference<List<ObjTest>>() {}, "name" to "ff").let { result ->
|
select(object : TypeReference<List<ObjTest>>() {}, "name" to "ff").let { result ->
|
||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertEquals("ff", result[0].name)
|
assertEquals("ff", result[0].name)
|
||||||
assertEquals("ff-2", result[1].name)
|
assertEquals("ff-2", result[1].name)
|
||||||
@@ -404,7 +414,7 @@ class RequesterTest : TestAbstract() {
|
|||||||
assertEquals("aa-2", result[1].name)
|
assertEquals("aa-2", result[1].name)
|
||||||
}
|
}
|
||||||
}.apply {
|
}.apply {
|
||||||
select<ObjTest>(object : TypeReference<List<ObjTest>>() {}, listOf("ff", "aa")).let { result ->
|
select(object : TypeReference<List<ObjTest>>() {}, listOf("ff", "aa")).let { result ->
|
||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertEquals("ff", result[0].name)
|
assertEquals("ff", result[0].name)
|
||||||
assertEquals("aa-2", result[1].name)
|
assertEquals("aa-2", result[1].name)
|
||||||
@@ -438,7 +448,7 @@ class RequesterTest : TestAbstract() {
|
|||||||
assertEquals(0, offset)
|
assertEquals(0, offset)
|
||||||
}
|
}
|
||||||
}.apply {
|
}.apply {
|
||||||
select<ObjTest>(1, 2, object : TypeReference<List<ObjTest>>() {}, mapOf("name" to "ff")).run {
|
select(1, 2, object : TypeReference<List<ObjTest>>() {}, mapOf("name" to "ff")).run {
|
||||||
assertNotNull(result)
|
assertNotNull(result)
|
||||||
assertEquals("ff", result[0].name)
|
assertEquals("ff", result[0].name)
|
||||||
assertEquals("ff-2", result[1].name)
|
assertEquals("ff-2", result[1].name)
|
||||||
@@ -478,17 +488,19 @@ class RequesterTest : TestAbstract() {
|
|||||||
Requester(connection, queriesDirectory = resources)
|
Requester(connection, queriesDirectory = resources)
|
||||||
.getQuery("selectOneWithParameters").apply {
|
.getQuery("selectOneWithParameters").apply {
|
||||||
selectOne<ObjTest>(mapOf("name" to "myName")) {
|
selectOne<ObjTest>(mapOf("name" to "myName")) {
|
||||||
assertEquals("myName", it!!.name)
|
assertNotNull(it)
|
||||||
|
assertEquals("myName", it.name)
|
||||||
assertEquals("plop", rows[0].getString("other"))
|
assertEquals("plop", rows[0].getString("other"))
|
||||||
}!!.run {
|
}.run {
|
||||||
assertEquals("myName", name)
|
assertEquals("selectOneWithParameters", name)
|
||||||
}
|
}
|
||||||
}.apply {
|
}.apply {
|
||||||
selectOne<ObjTest>(typeReference = object : TypeReference<ObjTest>() {}, values = mapOf("name" to "myName")) { it ->
|
selectOne(typeReference = object : TypeReference<ObjTest>() {}, values = mapOf("name" to "myName")) {
|
||||||
assertEquals("myName", it!!.name)
|
assertNotNull(it)
|
||||||
|
assertEquals("myName", it.name)
|
||||||
assertEquals("plop", rows[0].getString("other"))
|
assertEquals("plop", rows[0].getString("other"))
|
||||||
}!!.run {
|
}.run {
|
||||||
assertEquals("myName", name)
|
assertEquals("selectOneWithParameters", name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user