From d58f367843fecfb5102c80580d9b4ab91aefaa11 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sun, 4 Aug 2019 22:30:20 +0200 Subject: [PATCH] clean code --- .../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 | 17 ++++++------ 5 files changed, 34 insertions(+), 42 deletions(-) diff --git a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt index 232b568..dfe5208 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 7d41302..e116499 100644 --- a/src/test/kotlin/fr/postgresjson/RequesterTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequesterTest.kt @@ -15,7 +15,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() @@ -27,7 +27,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")) @@ -39,7 +39,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() @@ -50,7 +50,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")) @@ -61,7 +61,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() @@ -72,7 +72,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")) @@ -83,7 +83,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"))!! @@ -95,7 +95,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)!! @@ -107,7 +107,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"))!! @@ -118,7 +118,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")) @@ -129,7 +129,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")) @@ -143,7 +143,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")) @@ -157,7 +157,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 96394d2..cc700ea 100644 --- a/src/test/kotlin/fr/postgresjson/TestAbstract.kt +++ b/src/test/kotlin/fr/postgresjson/TestAbstract.kt @@ -9,22 +9,23 @@ import java.io.File @TestInstance(PER_CLASS) abstract class TestAbstract { - private var connection = Connection(database = "test", username = "test", password = "test") - - protected fun getConnextion(): Connection { - return connection - } + protected val connection = Connection(database = "test", username = "test", password = "test") @BeforeEach fun beforeAll() { val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI()) - getConnextion().connect().createStatement().executeUpdate(initSQL.readText()) + connection + .connect() + .createStatement() + .executeUpdate(initSQL.readText()) } @AfterEach fun afterAll() { val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI()) - getConnextion().connect().createStatement().executeUpdate(downSQL.readText()) - getConnextion().connect().close() + connection.connect().apply { + createStatement() + .executeUpdate(downSQL.readText()) + }.close() } } \ No newline at end of file