refactoring: move Query/Function logic into Requester class

This commit is contained in:
2019-06-14 17:09:52 +02:00
parent baa2976e80
commit 00d2fa335d
8 changed files with 235 additions and 160 deletions

View File

@@ -2,39 +2,23 @@ package fr.postgresjson
import fr.postgresjson.connexion.Connection
import fr.postgresjson.entity.IdEntity
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertTrue
import java.io.File
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ConnectionTest() {
class ConnectionTest(): TestAbstract() {
private class ObjTest(var name: String): IdEntity()
private class ObjTest2(var title: String, var test: ObjTest?): IdEntity()
private lateinit var connection: Connection
fun getConnextion(): Connection {
return Connection(database = "test", username = "test", password = "test")
}
@BeforeAll
fun beforeAll() {
val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI())
val promise = getConnextion().connect().sendQuery(initSQL.readText())
promise.join()
}
@BeforeEach
fun before() {
connection = getConnextion()
}
@AfterAll
fun afterAll() {
val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI())
getConnextion().connect().sendQuery(downSQL.readText()).join()
}
@Test
fun getObject() {
val obj: ObjTest? = connection.selectOne<Int?, ObjTest>("select to_json(a) from test a limit 1")

View File

@@ -1,27 +1,33 @@
package fr.postgresjson
import fr.postgresjson.connexion.Connection
import fr.postgresjson.connexion.Requester
import fr.postgresjson.entity.IdEntity
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.io.File
class RequestTest {
class RequestTest: TestAbstract() {
class ObjTest(var name:String): IdEntity(1)
@Test
fun getRequestFromFile() {
fun getQueryFromFile() {
val resources = File(this::class.java.getResource("/sql/query").toURI())
val objTest: ObjTest? = Connection(queriesDirectory = resources).getQuery("Test/selectOne").selectOne()
assertTrue(objTest!!.id == 2)
assertTrue(objTest.name == "test")
val objTest: ObjTest? = Requester(getConnextion())
.addQuery(resources)
.getQuery("Test/selectOne")
.selectOne()
assertEquals(objTest!!.id, 2)
assertEquals(objTest.name, "test")
}
@Test
fun getRequestFromFunction() {
fun getFunctionFromFile() {
val resources = File(this::class.java.getResource("/sql/function").toURI())
val objTest: ObjTest? = Connection(functionsDirectory = resources).getFunction("test_function").selectOne()
assertTrue(objTest!!.id == 2)
assertTrue(objTest.name == "test")
val objTest: ObjTest? = Requester(getConnextion())
.addFunction(resources)
.getFunction("test_function")
.selectOne(listOf("ploop", "plip"))
assertEquals(objTest!!.id, 3)
assertEquals(objTest.name, "test")
}
}

View File

@@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class SerializerTest {
internal class SerializerTest: TestAbstract() {
private class ObjTest(var val1: String, var val2: Int) : IdEntity(1)
private val serializer = Serializer()

View File

@@ -0,0 +1,28 @@
package fr.postgresjson
import fr.postgresjson.connexion.Connection
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
import java.io.File
@TestInstance(PER_CLASS)
abstract class TestAbstract {
protected fun getConnextion(): Connection {
return Connection(database = "test", username = "test", password = "test")
}
@BeforeAll
fun beforeAll() {
val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI())
val promise = getConnextion().connect().sendQuery(initSQL.readText())
promise.join()
}
@AfterAll
fun afterAll() {
val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI())
getConnextion().connect().sendQuery(downSQL.readText()).join()
}
}

View File

@@ -24,3 +24,12 @@ INSERT INTO test (id, name) VALUES (1, 'plop') ON CONFLICT DO NOTHING;
INSERT INTO test2 (id, title, test_id) VALUES (1, 'plop', 1) ON CONFLICT DO NOTHING;
INSERT INTO test2 (id, title, test_id) VALUES (2, 'plip', 1) ON CONFLICT DO NOTHING;
INSERT INTO test2 (id, title, test_id) VALUES (3, 'ttt', null) ON CONFLICT DO NOTHING;
CREATE OR REPLACE FUNCTION test_function (name text default 'plop', IN hi text default 'hello', out result json)
LANGUAGE plpgsql
AS
$$
BEGIN
result = json_build_object('id', 3, 'name', 'test');
END;
$$

View File

@@ -1,8 +1,8 @@
CREATE OR REPLACE FUNCTION test_function (name text, IN hi text default 'hello', out result json)
CREATE OR REPLACE FUNCTION test_function (name text default 'plop', IN hi text default 'hello', out result json)
LANGUAGE plpgsql
AS
$$
BEGIN
result = json_build_object('id', 2, 'name', 'test');
result = json_build_object('id', 3, 'name', 'test');
END;
$$