clean code

This commit is contained in:
2019-08-04 22:23:12 +02:00
parent 24349fc71f
commit 01a0000b15
9 changed files with 49 additions and 47 deletions

View File

@@ -1,6 +1,5 @@
package fr.postgresjson.connexion package fr.postgresjson.connexion
import com.github.jasync.sql.db.util.length
import fr.postgresjson.entity.EntityI import fr.postgresjson.entity.EntityI
data class Paginated<T: EntityI<*>>( data class Paginated<T: EntityI<*>>(
@@ -10,7 +9,7 @@ data class Paginated<T: EntityI<*>>(
val total: Int val total: Int
) { ) {
val currentPage: Int = (offset / limit) + 1 val currentPage: Int = (offset / limit) + 1
val count: Int = result.length val count: Int = result.size
init { init {
if (offset < 0) error("offset must be greather or equal than 0") if (offset < 0) error("offset must be greather or equal than 0")

View File

@@ -1,11 +1,9 @@
package fr.postgresjson package fr.postgresjson
import fr.postgresjson.connexion.Connection
import fr.postgresjson.connexion.Paginated import fr.postgresjson.connexion.Paginated
import fr.postgresjson.entity.IdEntity import fr.postgresjson.entity.IdEntity
import org.junit.Assert.* import org.junit.Assert.*
import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance 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 ObjTest2(var title: String, var test: ObjTest?): IdEntity()
private class ObjTest3(var first: String, var seconde: String, var third: Int): 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 @Test
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")

View File

@@ -17,7 +17,7 @@ class MigrationTest(): TestAbstract() {
@Test @Test
fun `run up query`() { fun `run up query`() {
val resources = File(this::class.java.getResource("/sql/migrations").toURI()) val resources = File(this::class.java.getResource("/sql/migrations").toURI())
val m = Migrations(resources, getConnextion()) val m = Migrations(resources, connection)
m.up().apply { m.up().apply {
this `should contain` Pair("1", Migration.Status.OK) this `should contain` Pair("1", Migration.Status.OK)
size `should be equal to` 1 size `should be equal to` 1
@@ -30,14 +30,14 @@ class MigrationTest(): TestAbstract() {
fun `migration up Query should throw error if no down`() { fun `migration up Query should throw error if no down`() {
val resources = File(this::class.java.getResource("/sql/migration_without_down").toURI()) val resources = File(this::class.java.getResource("/sql/migration_without_down").toURI())
invoking { invoking {
Migrations(resources, getConnextion()) Migrations(resources, connection)
} shouldThrow Migrations.DownMigrationNotDefined::class } shouldThrow Migrations.DownMigrationNotDefined::class
} }
@Test @Test
fun `run forced down query`() { fun `run forced down query`() {
val resources = File(this::class.java.getResource("/sql/migrations").toURI()) val resources = File(this::class.java.getResource("/sql/migrations").toURI())
val m = Migrations(resources, getConnextion()) val m = Migrations(resources, connection)
repeat(3) { repeat(3) {
m.down(true).apply { m.down(true).apply {
this `should contain` Pair("1", Migration.Status.OK) this `should contain` Pair("1", Migration.Status.OK)
@@ -49,10 +49,10 @@ class MigrationTest(): TestAbstract() {
@Test @Test
fun `run dry migrations`() { fun `run dry migrations`() {
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI()) 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
} }
Migrations(resources, getConnextion()).apply { Migrations(resources, connection).apply {
runDry().size `should be equal to` 2 runDry().size `should be equal to` 2
} }
} }
@@ -60,7 +60,7 @@ class MigrationTest(): TestAbstract() {
@Test @Test
fun `run dry migrations launch twice`() { fun `run dry migrations launch twice`() {
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI()) 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
runDry().size `should be equal to` 2 runDry().size `should be equal to` 2
} }
@@ -69,7 +69,7 @@ class MigrationTest(): TestAbstract() {
@Test @Test
fun `run migrations`() { fun `run migrations`() {
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI()) val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
Migrations(resources, getConnextion()).apply { Migrations(resources, connection).apply {
run().apply { run().apply {
size `should be equal to` 1 size `should be equal to` 1
} }
@@ -80,12 +80,12 @@ class MigrationTest(): TestAbstract() {
fun `run migrations force down`() { fun `run migrations force down`() {
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI()) val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
val resourcesFunctions = File(this::class.java.getResource("/sql/function").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 { up().apply {
size `should be equal to` 6 size `should be equal to` 6
} }
} }
Migrations(listOf(resources, resourcesFunctions), getConnextion()).apply { Migrations(listOf(resources, resourcesFunctions), connection).apply {
forceAllDown().apply { forceAllDown().apply {
size `should be equal to` 6 size `should be equal to` 6
} }
@@ -95,11 +95,11 @@ class MigrationTest(): TestAbstract() {
@Test @Test
fun `run functions migrations`() { fun `run functions migrations`() {
val resources = File(this::class.java.getResource("/sql/function").toURI()) 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 run().size `should be equal to` 5
} }
val objTest: RequesterTest.ObjTest? = Requester(getConnextion()) val objTest: RequesterTest.ObjTest? = Requester(connection)
.addFunction(resources) .addFunction(resources)
.getFunction("test_function") .getFunction("test_function")
.selectOne(listOf("test", "plip")) .selectOne(listOf("test", "plip"))

View File

@@ -14,7 +14,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `get query from file`() { fun `get query from file`() {
val resources = File(this::class.java.getResource("/sql/query").toURI()) val resources = File(this::class.java.getResource("/sql/query").toURI())
val objTest: ObjTest? = Requester(getConnextion()) val objTest: ObjTest? = Requester(connection)
.addQuery(resources) .addQuery(resources)
.getQuery("Test/selectOne") .getQuery("Test/selectOne")
.selectOne() .selectOne()
@@ -26,7 +26,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `get function from file`() { fun `get function from file`() {
val resources = File(this::class.java.getResource("/sql/function").toURI()) val resources = File(this::class.java.getResource("/sql/function").toURI())
val objTest: ObjTest? = Requester(getConnextion()) val objTest: ObjTest? = Requester(connection)
.addFunction(resources) .addFunction(resources)
.getFunction("test_function") .getFunction("test_function")
.selectOne(listOf("test", "plip")) .selectOne(listOf("test", "plip"))
@@ -38,7 +38,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `call exec on query`() { fun `call exec on query`() {
val resources = File(this::class.java.getResource("/sql/query").toURI()) val resources = File(this::class.java.getResource("/sql/query").toURI())
val result = Requester(getConnextion()) val result = Requester(connection)
.addQuery(resources) .addQuery(resources)
.getQuery("Test/selectOne") .getQuery("Test/selectOne")
.exec() .exec()
@@ -49,7 +49,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `call exec on function`() { fun `call exec on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI()) val resources = File(this::class.java.getResource("/sql/function").toURI())
val result = Requester(getConnextion()) val result = Requester(connection)
.addFunction(resources) .addFunction(resources)
.getFunction("test_function") .getFunction("test_function")
.exec(listOf("test", "plip")) .exec(listOf("test", "plip"))
@@ -60,7 +60,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `call sendQuery on query`() { fun `call sendQuery on query`() {
val resources = File(this::class.java.getResource("/sql/query").toURI()) val resources = File(this::class.java.getResource("/sql/query").toURI())
val result = Requester(getConnextion()) val result = Requester(connection)
.addQuery(resources) .addQuery(resources)
.getQuery("Test/exec") .getQuery("Test/exec")
.sendQuery() .sendQuery()
@@ -71,7 +71,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `call sendQuery on function`() { fun `call sendQuery on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI()) val resources = File(this::class.java.getResource("/sql/function").toURI())
val result = Requester(getConnextion()) val result = Requester(connection)
.addFunction(resources) .addFunction(resources)
.getFunction("function_void") .getFunction("function_void")
.sendQuery(listOf("test")) .sendQuery(listOf("test"))
@@ -82,7 +82,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `call selectOne on function`() { fun `call selectOne on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI()) val resources = File(this::class.java.getResource("/sql/function").toURI())
val obj: ObjTest = Requester(getConnextion()) val obj: ObjTest = Requester(connection)
.addFunction(resources) .addFunction(resources)
.getFunction("test_function") .getFunction("test_function")
.selectOne(mapOf("name" to "myName"))!! .selectOne(mapOf("name" to "myName"))!!
@@ -94,7 +94,7 @@ class RequesterTest: TestAbstract() {
fun `call selectOne on function with object`() { fun `call selectOne on function with object`() {
val resources = File(this::class.java.getResource("/sql/function").toURI()) val resources = File(this::class.java.getResource("/sql/function").toURI())
val obj2 = ObjTest("original") val obj2 = ObjTest("original")
val obj: ObjTest = Requester(getConnextion()) val obj: ObjTest = Requester(connection)
.addFunction(resources) .addFunction(resources)
.getFunction("test_function_object") .getFunction("test_function_object")
.selectOne("resource" to obj2)!! .selectOne("resource" to obj2)!!
@@ -106,7 +106,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `call selectOne on query`() { fun `call selectOne on query`() {
val resources = File(this::class.java.getResource("/sql/query").toURI()) val resources = File(this::class.java.getResource("/sql/query").toURI())
val obj: ObjTest = Requester(getConnextion()) val obj: ObjTest = Requester(connection)
.addQuery(resources) .addQuery(resources)
.getQuery("Test/selectOneWithParameters") .getQuery("Test/selectOneWithParameters")
.selectOne(mapOf("name" to "myName"))!! .selectOne(mapOf("name" to "myName"))!!
@@ -117,7 +117,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `call select (multiple) on function`() { fun `call select (multiple) on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI()) val resources = File(this::class.java.getResource("/sql/function").toURI())
val obj: List<ObjTest>? = Requester(getConnextion()) val obj: List<ObjTest>? = Requester(connection)
.addFunction(resources) .addFunction(resources)
.getFunction("test_function_multiple") .getFunction("test_function_multiple")
.select(mapOf("name" to "myName")) .select(mapOf("name" to "myName"))
@@ -128,7 +128,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `call select paginated on query`() { fun `call select paginated on query`() {
val resources = File(this::class.java.getResource("/sql/query").toURI()) val resources = File(this::class.java.getResource("/sql/query").toURI())
val result: Paginated<ObjTest> = Requester(getConnextion()) val result: Paginated<ObjTest> = Requester(connection)
.addQuery(resources) .addQuery(resources)
.getQuery("Test/selectPaginated") .getQuery("Test/selectPaginated")
.select(1, 2, mapOf("name" to "ff")) .select(1, 2, mapOf("name" to "ff"))
@@ -142,7 +142,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `call select paginated on function`() { fun `call select paginated on function`() {
val resources = File(this::class.java.getResource("/sql/function").toURI()) val resources = File(this::class.java.getResource("/sql/function").toURI())
val result: Paginated<ObjTest> = Requester(getConnextion()) val result: Paginated<ObjTest> = Requester(connection)
.addFunction(resources) .addFunction(resources)
.getFunction("test_function_paginated") .getFunction("test_function_paginated")
.select(1, 2, mapOf("name" to "ff")) .select(1, 2, mapOf("name" to "ff"))
@@ -156,7 +156,7 @@ class RequesterTest: TestAbstract() {
@Test @Test
fun `call selectOne on query with extra parameter`() { fun `call selectOne on query with extra parameter`() {
val resources = File(this::class.java.getResource("/sql/query").toURI()) val resources = File(this::class.java.getResource("/sql/query").toURI())
val obj: ObjTest = Requester(getConnextion()) val obj: ObjTest = Requester(connection)
.addQuery(resources) .addQuery(resources)
.getQuery("Test/selectOneWithParameters") .getQuery("Test/selectOneWithParameters")
.selectOne(mapOf("name" to "myName")) { .selectOne(mapOf("name" to "myName")) {

View File

@@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.TestInstance
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @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 ObjTest(var val1: String, var val2: Int) : IdEntity(1)
private class ObjTestDate(var val1: DateTime) : IdEntity(2) private class ObjTestDate(var val1: DateTime) : IdEntity(2)

View File

@@ -9,22 +9,22 @@ import java.io.File
@TestInstance(PER_CLASS) @TestInstance(PER_CLASS)
abstract class TestAbstract { abstract class TestAbstract {
private val con = Connection(database = "test", username = "test", password = "test") protected val connection = Connection(database = "test", username = "test", password = "test")
protected fun getConnextion(): Connection {
return con
}
@BeforeEach @BeforeEach
fun beforeAll() { fun beforeAll() {
val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI()) val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI())
val promise = getConnextion().connect().sendQuery(initSQL.readText()) connection
promise.join() .connect()
.sendQuery(initSQL.readText())
.join()
} }
@AfterEach @AfterEach
fun afterAll() { fun afterAll() {
val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI()) val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI())
getConnextion().connect().sendQuery(downSQL.readText()).join() connection.connect().apply {
getConnextion().connect().disconnect() sendQuery(downSQL.readText()).join()
}.disconnect()
} }
} }

View File

@@ -1 +1,5 @@
SELECT 1; do $$
begin
PERFORM 1;
end;
$$

View File

@@ -1 +1,5 @@
SELECT 1; do $$
begin
PERFORM 1;
end;
$$

View File

@@ -1 +1,5 @@
SELECT 1; do $$
begin
PERFORM 1;
end;
$$