clean code
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import com.github.jasync.sql.db.util.length
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
data class Paginated<T: EntityI<*>>(
|
||||
@@ -10,7 +9,7 @@ data class Paginated<T: EntityI<*>>(
|
||||
val total: Int
|
||||
) {
|
||||
val currentPage: Int = (offset / limit) + 1
|
||||
val count: Int = result.length
|
||||
val count: Int = result.size
|
||||
|
||||
init {
|
||||
if (offset < 0) error("offset must be greather or equal than 0")
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.entity.IdEntity
|
||||
import org.junit.Assert.*
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
|
||||
@@ -15,13 +13,6 @@ class ConnectionTest(): TestAbstract() {
|
||||
private class ObjTest2(var title: String, var test: ObjTest?): IdEntity()
|
||||
private class ObjTest3(var first: String, var seconde: String, var third: Int): IdEntity()
|
||||
|
||||
private lateinit var connection: Connection
|
||||
|
||||
@BeforeEach
|
||||
fun before() {
|
||||
connection = getConnextion()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getObject() {
|
||||
val obj: ObjTest? = connection.selectOne("select to_json(a) from test a limit 1")
|
||||
|
||||
@@ -17,7 +17,7 @@ class MigrationTest(): TestAbstract() {
|
||||
@Test
|
||||
fun `run up query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||
val m = Migrations(resources, getConnextion())
|
||||
val m = Migrations(resources, connection)
|
||||
m.up().apply {
|
||||
this `should contain` Pair("1", Migration.Status.OK)
|
||||
size `should be equal to` 1
|
||||
@@ -30,14 +30,14 @@ class MigrationTest(): TestAbstract() {
|
||||
fun `migration up Query should throw error if no down`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migration_without_down").toURI())
|
||||
invoking {
|
||||
Migrations(resources, getConnextion())
|
||||
Migrations(resources, connection)
|
||||
} shouldThrow Migrations.DownMigrationNotDefined::class
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `run forced down query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||
val m = Migrations(resources, getConnextion())
|
||||
val m = Migrations(resources, connection)
|
||||
repeat(3) {
|
||||
m.down(true).apply {
|
||||
this `should contain` Pair("1", Migration.Status.OK)
|
||||
@@ -49,10 +49,10 @@ class MigrationTest(): TestAbstract() {
|
||||
@Test
|
||||
fun `run dry migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
Migrations(resources, getConnextion()).apply {
|
||||
Migrations(resources, connection).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
Migrations(resources, getConnextion()).apply {
|
||||
Migrations(resources, connection).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,7 @@ class MigrationTest(): TestAbstract() {
|
||||
@Test
|
||||
fun `run dry migrations launch twice`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
Migrations(resources, getConnextion()).apply {
|
||||
Migrations(resources, connection).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
@@ -69,7 +69,7 @@ class MigrationTest(): TestAbstract() {
|
||||
@Test
|
||||
fun `run migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
Migrations(resources, getConnextion()).apply {
|
||||
Migrations(resources, connection).apply {
|
||||
run().apply {
|
||||
size `should be equal to` 1
|
||||
}
|
||||
@@ -80,12 +80,12 @@ class MigrationTest(): TestAbstract() {
|
||||
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").toURI())
|
||||
Migrations(listOf(resources, resourcesFunctions), getConnextion()).apply {
|
||||
Migrations(listOf(resources, resourcesFunctions), connection).apply {
|
||||
up().apply {
|
||||
size `should be equal to` 6
|
||||
}
|
||||
}
|
||||
Migrations(listOf(resources, resourcesFunctions), getConnextion()).apply {
|
||||
Migrations(listOf(resources, resourcesFunctions), connection).apply {
|
||||
forceAllDown().apply {
|
||||
size `should be equal to` 6
|
||||
}
|
||||
@@ -95,11 +95,11 @@ class MigrationTest(): TestAbstract() {
|
||||
@Test
|
||||
fun `run functions migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
Migrations(resources, getConnextion()).apply {
|
||||
Migrations(resources, connection).apply {
|
||||
run().size `should be equal to` 5
|
||||
}
|
||||
|
||||
val objTest: RequesterTest.ObjTest? = Requester(getConnextion())
|
||||
val objTest: RequesterTest.ObjTest? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(listOf("test", "plip"))
|
||||
|
||||
@@ -14,7 +14,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `get query from file`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val objTest: ObjTest? = Requester(getConnextion())
|
||||
val objTest: ObjTest? = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOne")
|
||||
.selectOne()
|
||||
@@ -26,7 +26,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `get function from file`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val objTest: ObjTest? = Requester(getConnextion())
|
||||
val objTest: ObjTest? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(listOf("test", "plip"))
|
||||
@@ -38,7 +38,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `call exec on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val result = Requester(getConnextion())
|
||||
val result = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOne")
|
||||
.exec()
|
||||
@@ -49,7 +49,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `call exec on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val result = Requester(getConnextion())
|
||||
val result = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function")
|
||||
.exec(listOf("test", "plip"))
|
||||
@@ -60,7 +60,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `call sendQuery on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val result = Requester(getConnextion())
|
||||
val result = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/exec")
|
||||
.sendQuery()
|
||||
@@ -71,7 +71,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `call sendQuery on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val result = Requester(getConnextion())
|
||||
val result = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("function_void")
|
||||
.sendQuery(listOf("test"))
|
||||
@@ -82,7 +82,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `call selectOne on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val obj: ObjTest = Requester(getConnextion())
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(mapOf("name" to "myName"))!!
|
||||
@@ -94,7 +94,7 @@ class RequesterTest: TestAbstract() {
|
||||
fun `call selectOne on function with object`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val obj2 = ObjTest("original")
|
||||
val obj: ObjTest = Requester(getConnextion())
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function_object")
|
||||
.selectOne("resource" to obj2)!!
|
||||
@@ -106,7 +106,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `call selectOne on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val obj: ObjTest = Requester(getConnextion())
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOneWithParameters")
|
||||
.selectOne(mapOf("name" to "myName"))!!
|
||||
@@ -117,7 +117,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `call select (multiple) on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val obj: List<ObjTest>? = Requester(getConnextion())
|
||||
val obj: List<ObjTest>? = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function_multiple")
|
||||
.select(mapOf("name" to "myName"))
|
||||
@@ -128,7 +128,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `call select paginated on query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val result: Paginated<ObjTest> = Requester(getConnextion())
|
||||
val result: Paginated<ObjTest> = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectPaginated")
|
||||
.select(1, 2, mapOf("name" to "ff"))
|
||||
@@ -142,7 +142,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `call select paginated on function`() {
|
||||
val resources = File(this::class.java.getResource("/sql/function").toURI())
|
||||
val result: Paginated<ObjTest> = Requester(getConnextion())
|
||||
val result: Paginated<ObjTest> = Requester(connection)
|
||||
.addFunction(resources)
|
||||
.getFunction("test_function_paginated")
|
||||
.select(1, 2, mapOf("name" to "ff"))
|
||||
@@ -156,7 +156,7 @@ class RequesterTest: TestAbstract() {
|
||||
@Test
|
||||
fun `call selectOne on query with extra parameter`() {
|
||||
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||
val obj: ObjTest = Requester(getConnextion())
|
||||
val obj: ObjTest = Requester(connection)
|
||||
.addQuery(resources)
|
||||
.getQuery("Test/selectOneWithParameters")
|
||||
.selectOne(mapOf("name" to "myName")) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
internal class SerializerTest: TestAbstract() {
|
||||
internal class SerializerTest {
|
||||
private class ObjTest(var val1: String, var val2: Int) : IdEntity(1)
|
||||
private class ObjTestDate(var val1: DateTime) : IdEntity(2)
|
||||
|
||||
|
||||
@@ -9,22 +9,22 @@ import java.io.File
|
||||
|
||||
@TestInstance(PER_CLASS)
|
||||
abstract class TestAbstract {
|
||||
private val con = Connection(database = "test", username = "test", password = "test")
|
||||
protected fun getConnextion(): Connection {
|
||||
return con
|
||||
}
|
||||
protected val connection = Connection(database = "test", username = "test", password = "test")
|
||||
|
||||
@BeforeEach
|
||||
fun beforeAll() {
|
||||
val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI())
|
||||
val promise = getConnextion().connect().sendQuery(initSQL.readText())
|
||||
promise.join()
|
||||
connection
|
||||
.connect()
|
||||
.sendQuery(initSQL.readText())
|
||||
.join()
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun afterAll() {
|
||||
val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI())
|
||||
getConnextion().connect().sendQuery(downSQL.readText()).join()
|
||||
getConnextion().connect().disconnect()
|
||||
connection.connect().apply {
|
||||
sendQuery(downSQL.readText()).join()
|
||||
}.disconnect()
|
||||
}
|
||||
}
|
||||
@@ -1 +1,5 @@
|
||||
SELECT 1;
|
||||
do $$
|
||||
begin
|
||||
PERFORM 1;
|
||||
end;
|
||||
$$
|
||||
|
||||
@@ -1 +1,5 @@
|
||||
SELECT 1;
|
||||
do $$
|
||||
begin
|
||||
PERFORM 1;
|
||||
end;
|
||||
$$
|
||||
@@ -1 +1,5 @@
|
||||
SELECT 1;
|
||||
do $$
|
||||
begin
|
||||
PERFORM 1;
|
||||
end;
|
||||
$$
|
||||
Reference in New Issue
Block a user