From 01a0000b15ee173753097e2b25810f8ee9534f9b Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sun, 4 Aug 2019 22:23:12 +0200 Subject: [PATCH] clean code --- .../fr/postgresjson/connexion/Paginated.kt | 3 +-- .../kotlin/fr/postgresjson/ConnectionTest.kt | 9 ------- .../kotlin/fr/postgresjson/MigrationTest.kt | 22 ++++++++-------- .../kotlin/fr/postgresjson/RequesterTest.kt | 26 +++++++++---------- .../kotlin/fr/postgresjson/SerializerTest.kt | 2 +- .../kotlin/fr/postgresjson/TestAbstract.kt | 16 ++++++------ .../sql/migration_without_down/1.up.sql | 6 ++++- src/test/resources/sql/migrations/1.down.sql | 6 ++++- src/test/resources/sql/migrations/1.up.sql | 6 ++++- 9 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt b/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt index cb6d21f..9b00544 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Paginated.kt @@ -1,6 +1,5 @@ package fr.postgresjson.connexion -import com.github.jasync.sql.db.util.length import fr.postgresjson.entity.EntityI data class Paginated>( @@ -10,7 +9,7 @@ data class Paginated>( 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") diff --git a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt index e07fdbb..c615245 100644 --- a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt +++ b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt @@ -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") diff --git a/src/test/kotlin/fr/postgresjson/MigrationTest.kt b/src/test/kotlin/fr/postgresjson/MigrationTest.kt index aabc9fc..bc02160 100644 --- a/src/test/kotlin/fr/postgresjson/MigrationTest.kt +++ b/src/test/kotlin/fr/postgresjson/MigrationTest.kt @@ -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")) diff --git a/src/test/kotlin/fr/postgresjson/RequesterTest.kt b/src/test/kotlin/fr/postgresjson/RequesterTest.kt index 9177ed1..3ffbd77 100644 --- a/src/test/kotlin/fr/postgresjson/RequesterTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequesterTest.kt @@ -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? = Requester(getConnextion()) + val obj: List? = 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 = Requester(getConnextion()) + val result: Paginated = 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 = Requester(getConnextion()) + val result: Paginated = 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")) { diff --git a/src/test/kotlin/fr/postgresjson/SerializerTest.kt b/src/test/kotlin/fr/postgresjson/SerializerTest.kt index f46c1d0..9d9bd10 100644 --- a/src/test/kotlin/fr/postgresjson/SerializerTest.kt +++ b/src/test/kotlin/fr/postgresjson/SerializerTest.kt @@ -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) diff --git a/src/test/kotlin/fr/postgresjson/TestAbstract.kt b/src/test/kotlin/fr/postgresjson/TestAbstract.kt index b80a70c..ebc70f8 100644 --- a/src/test/kotlin/fr/postgresjson/TestAbstract.kt +++ b/src/test/kotlin/fr/postgresjson/TestAbstract.kt @@ -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() } } \ No newline at end of file diff --git a/src/test/resources/sql/migration_without_down/1.up.sql b/src/test/resources/sql/migration_without_down/1.up.sql index 027b7d6..89ac949 100644 --- a/src/test/resources/sql/migration_without_down/1.up.sql +++ b/src/test/resources/sql/migration_without_down/1.up.sql @@ -1 +1,5 @@ -SELECT 1; \ No newline at end of file +do $$ + begin + PERFORM 1; + end; +$$ diff --git a/src/test/resources/sql/migrations/1.down.sql b/src/test/resources/sql/migrations/1.down.sql index 027b7d6..1274bc7 100644 --- a/src/test/resources/sql/migrations/1.down.sql +++ b/src/test/resources/sql/migrations/1.down.sql @@ -1 +1,5 @@ -SELECT 1; \ No newline at end of file +do $$ + begin + PERFORM 1; + end; +$$ \ No newline at end of file diff --git a/src/test/resources/sql/migrations/1.up.sql b/src/test/resources/sql/migrations/1.up.sql index 027b7d6..1274bc7 100644 --- a/src/test/resources/sql/migrations/1.up.sql +++ b/src/test/resources/sql/migrations/1.up.sql @@ -1 +1,5 @@ -SELECT 1; \ No newline at end of file +do $$ + begin + PERFORM 1; + end; +$$ \ No newline at end of file