Extract Reified method to Extension
Add update method
This commit is contained in:
@@ -12,7 +12,7 @@ import org.junit.jupiter.api.TestInstance
|
||||
import java.util.UUID
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class ConnectionTest() : TestAbstract() {
|
||||
class ConnectionTest : TestAbstract() {
|
||||
private class ObjTest(val name: String, id: UUID = UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00")) : UuidEntity(id)
|
||||
private class ObjTest2(val title: String, var test: ObjTest?) : UuidEntity()
|
||||
private class ObjTest3(val first: String, var seconde: String, var third: Int) : UuidEntity()
|
||||
|
||||
@@ -16,11 +16,11 @@ import java.util.UUID
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class EntityTest() {
|
||||
private class User(id: UUID = UUID.randomUUID()) : Entity<UUID>(id)
|
||||
private class ObjTest(var name: String) : UuidEntityExtended<Int?, User>(User(), User())
|
||||
private class ObjTest(val name: String) : UuidEntityExtended<Int?, User>(User(), User())
|
||||
|
||||
@Test
|
||||
fun getObject() {
|
||||
val obj: ObjTest? = ObjTest("plop")
|
||||
val obj = ObjTest("plop")
|
||||
assertTrue(obj is ObjTest)
|
||||
assertTrue(obj is UuidEntityExtended<Int?, User>)
|
||||
assertTrue(obj is EntityI)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.connexion.selectOne
|
||||
import fr.postgresjson.migration.Migration
|
||||
import fr.postgresjson.migration.Migrations
|
||||
import org.amshove.kluent.`should be equal to`
|
||||
@@ -13,10 +14,10 @@ import org.junit.jupiter.api.TestInstance
|
||||
import java.util.UUID
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class MigrationTest() : TestAbstract() {
|
||||
class MigrationTest : TestAbstract() {
|
||||
@Test
|
||||
fun `run up query`() {
|
||||
val resources = this::class.java.getResource("/sql/migrations").toURI()
|
||||
val resources = this::class.java.getResource("/sql/migrations")!!.toURI()
|
||||
val m = Migrations(connection, resources)
|
||||
m.up().apply {
|
||||
this `should contain` Pair("1", Migration.Status.OK)
|
||||
@@ -28,7 +29,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `migration up Query should throw error if no down`() {
|
||||
val resources = this::class.java.getResource("/sql/migration_without_down").toURI()
|
||||
val resources = this::class.java.getResource("/sql/migration_without_down")!!.toURI()
|
||||
invoking {
|
||||
Migrations(resources, connection)
|
||||
} shouldThrow Migrations.DownMigrationNotDefined::class
|
||||
@@ -36,7 +37,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run forced down query`() {
|
||||
val resources = this::class.java.getResource("/sql/migrations").toURI()
|
||||
val resources = this::class.java.getResource("/sql/migrations")!!.toURI()
|
||||
val m = Migrations(resources, connection)
|
||||
repeat(3) {
|
||||
m.down(true).apply {
|
||||
@@ -48,7 +49,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run dry migrations`() {
|
||||
val resources = this::class.java.getResource("/sql/real_migrations").toURI()
|
||||
val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI()
|
||||
Migrations(resources, connection).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
@@ -59,7 +60,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run dry migrations launch twice`() {
|
||||
val resources = this::class.java.getResource("/sql/real_migrations").toURI()
|
||||
val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI()
|
||||
Migrations(resources, connection).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
runDry().size `should be equal to` 2
|
||||
@@ -68,7 +69,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run migrations`() {
|
||||
val resources = this::class.java.getResource("/sql/real_migrations").toURI()
|
||||
val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI()
|
||||
Migrations(resources, connection).apply {
|
||||
run().apply {
|
||||
size `should be equal to` 1
|
||||
@@ -78,8 +79,8 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run migrations force down`() {
|
||||
val resources = this::class.java.getResource("/sql/real_migrations").toURI()
|
||||
val resourcesFunctions = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI()
|
||||
val resourcesFunctions = this::class.java.getResource("/sql/function/Test")!!.toURI()
|
||||
Migrations(listOf(resources, resourcesFunctions), connection).apply {
|
||||
up().apply {
|
||||
size `should be equal to` 6
|
||||
@@ -94,7 +95,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run functions migrations`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")!!.toURI()
|
||||
Migrations(resources, connection).apply {
|
||||
run().size `should be equal to` 5
|
||||
}
|
||||
@@ -109,7 +110,7 @@ class MigrationTest() : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `run functions migrations and drop if exist`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test1").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test1")!!.toURI()
|
||||
Migrations(resources, connection).apply {
|
||||
run().size `should be equal to` 1
|
||||
}
|
||||
@@ -121,7 +122,7 @@ class MigrationTest() : TestAbstract() {
|
||||
Assertions.assertEquals(objTest!!.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d"))
|
||||
Assertions.assertEquals(objTest.name, "test")
|
||||
|
||||
val resources2 = this::class.java.getResource("/sql/function/Test2").toURI()
|
||||
val resources2 = this::class.java.getResource("/sql/function/Test2")!!.toURI()
|
||||
Migrations(resources2, connection).apply {
|
||||
run().size `should be equal to` 1
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.connexion.Requester.NoFunctionDefined
|
||||
import fr.postgresjson.connexion.Requester.NoQueryDefined
|
||||
import fr.postgresjson.connexion.select
|
||||
import fr.postgresjson.connexion.selectOne
|
||||
import fr.postgresjson.connexion.update
|
||||
import fr.postgresjson.entity.UuidEntity
|
||||
import org.junit.Assert
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
@@ -16,7 +19,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `requester constructor empty`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")!!.toURI()
|
||||
val name: String = Requester(connection)
|
||||
.apply { addFunctions(resources) }
|
||||
.getFunction("test_function")
|
||||
@@ -27,7 +30,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `requester constructor function directory`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val name: String = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.name
|
||||
@@ -37,7 +40,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `requester constructor query directory`() {
|
||||
val resources = this::class.java.getResource("/sql/query/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/query/Test")?.toURI()
|
||||
val name: String = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("DeleteTest")
|
||||
.name
|
||||
@@ -47,7 +50,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `function toString`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val name: String = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.toString()
|
||||
@@ -88,7 +91,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `get query from file`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val resources = this::class.java.getResource("/sql/query")!!.toURI()
|
||||
val objTest: ObjTest? = Requester(connection)
|
||||
.apply { addQuery(resources) }
|
||||
.getQuery("selectOne")
|
||||
@@ -100,7 +103,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `get query from file with wrong name throw exception`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
assertThrows(NoQueryDefined::class.java) {
|
||||
Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("wrongName")
|
||||
@@ -109,7 +112,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `get queries from file`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val name: String = Requester(connection, queriesDirectory = resources)
|
||||
.getQueries()[0].name
|
||||
|
||||
@@ -118,7 +121,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `get function from file with wrong name throw exception`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
assertThrows(NoFunctionDefined::class.java) {
|
||||
Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("wrongName")
|
||||
@@ -127,7 +130,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `get function from file`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val objTest: ObjTest? = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(listOf("test", "plip"))
|
||||
@@ -138,7 +141,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call exec on query`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val result = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectOne")
|
||||
.exec()
|
||||
@@ -148,7 +151,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call exec on function`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val result = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.exec(listOf("test", "plip"))
|
||||
@@ -158,7 +161,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call sendQuery on query with name`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val result = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("DeleteTest")
|
||||
.sendQuery()
|
||||
@@ -168,7 +171,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call sendQuery on function`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val result = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("function_void")
|
||||
.sendQuery(listOf("test"))
|
||||
@@ -178,7 +181,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call selectOne on function`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne(mapOf("name" to "myName"))!!
|
||||
@@ -188,7 +191,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call selectOne on function with object and named argument`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj2 = ObjTest("original")
|
||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_object")
|
||||
@@ -200,11 +203,11 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call selectOne on function with object`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj2 = ObjTest("original")
|
||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_object")
|
||||
.selectOne(obj2)!!
|
||||
.update(obj2)!!
|
||||
|
||||
assertEquals("changedName", obj.name)
|
||||
assertEquals("original", obj2.name)
|
||||
@@ -212,7 +215,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call selectOne on function with object and no arguments`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj: ObjTest = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function")
|
||||
.selectOne()!!
|
||||
@@ -222,7 +225,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call selectOne on query`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val obj: ObjTest = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectOneWithParameters")
|
||||
.selectOne(mapOf("name" to "myName"))!!
|
||||
@@ -232,27 +235,60 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call select (multiple) on function with named argument`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val obj: List<ObjTest>? = Requester(connection, functionsDirectory = resources)
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj: List<ObjTest> = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_multiple")
|
||||
.select(mapOf("name" to "myName"))
|
||||
|
||||
assertEquals("myName", obj!![0].name)
|
||||
assertEquals("myName", obj[0].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call select (multiple) on function with ordered arguments`() {
|
||||
val resources = this::class.java.getResource("/sql/function/Test").toURI()
|
||||
val obj: List<ObjTest>? = Requester(connection, functionsDirectory = resources)
|
||||
val resources = this::class.java.getResource("/sql/function/Test")?.toURI()
|
||||
val obj: List<ObjTest> = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_multiple")
|
||||
.select(listOf("myName"))
|
||||
|
||||
assertEquals("myName", obj!![0].name)
|
||||
assertEquals("myName", obj[0].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call select multiple (named arguments)`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val result: List<ObjTest> = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultiple")
|
||||
.select(mapOf("name" to "ff"))
|
||||
Assert.assertNotNull(result)
|
||||
Assert.assertEquals("ff", result[0].name)
|
||||
Assert.assertEquals("ff-2", result[1].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call select multiple (named arguments as pair)`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val result: List<ObjTest> = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultiple")
|
||||
.select("name" to "ff")
|
||||
Assert.assertNotNull(result)
|
||||
Assert.assertEquals("ff", result[0].name)
|
||||
Assert.assertEquals("ff-2", result[1].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call select multiple (ordered argument)`() {
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val result: List<ObjTest> = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectMultipleOrderedArgs")
|
||||
.select(listOf("ff", "aa"))
|
||||
Assert.assertNotNull(result)
|
||||
Assert.assertEquals("ff", result[0].name)
|
||||
Assert.assertEquals("aa-2", result[1].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `call select paginated on query`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val result: Paginated<ObjTest> = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectPaginated")
|
||||
.select(1, 2, mapOf("name" to "ff"))
|
||||
@@ -265,7 +301,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call select paginated on function`() {
|
||||
val resources = this::class.java.getResource("/sql/function").toURI()
|
||||
val resources = this::class.java.getResource("/sql/function")?.toURI()
|
||||
val result: Paginated<ObjTest> = Requester(connection, functionsDirectory = resources)
|
||||
.getFunction("test_function_paginated")
|
||||
.select(1, 2, mapOf("name" to "ff"))
|
||||
@@ -278,7 +314,7 @@ class RequesterTest : TestAbstract() {
|
||||
|
||||
@Test
|
||||
fun `call selectOne on query with extra parameter`() {
|
||||
val resources = this::class.java.getResource("/sql/query").toURI()
|
||||
val resources = this::class.java.getResource("/sql/query")?.toURI()
|
||||
val obj: ObjTest = Requester(connection, queriesDirectory = resources)
|
||||
.getQuery("selectOneWithParameters")
|
||||
.selectOne(mapOf("name" to "myName")) {
|
||||
|
||||
@@ -2,7 +2,6 @@ package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.entity.UuidEntity
|
||||
import fr.postgresjson.serializer.Serializer
|
||||
import fr.postgresjson.serializer.deserialize
|
||||
import fr.postgresjson.serializer.serialize
|
||||
import org.joda.time.DateTime
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
@@ -21,7 +20,6 @@ internal class SerializerTest {
|
||||
|
||||
private val objSerialized: String = """{"val1":"plop","val2":123,"id":"829b1a29-5db8-47f9-9562-961c561ac528"}"""
|
||||
private val objSerializedWithExtra: String = """{"val1":"plop","val2":123,"id":"829b1a29-5db8-47f9-9562-961c561ac528","toto":"tata"}"""
|
||||
private val objSerializedUpdate = """{"val1":"update","val2":123}"""
|
||||
private lateinit var obj: ObjTest
|
||||
|
||||
@BeforeEach
|
||||
|
||||
@@ -13,7 +13,7 @@ abstract class TestAbstract {
|
||||
|
||||
@BeforeEach
|
||||
fun beforeAll() {
|
||||
val initSQL = File(this::class.java.getResource("/fixtures/init.sql").toURI())
|
||||
val initSQL = File(this::class.java.getResource("/fixtures/init.sql")!!.toURI())
|
||||
connection
|
||||
.connect()
|
||||
.sendQuery(initSQL.readText())
|
||||
@@ -22,9 +22,9 @@ abstract class TestAbstract {
|
||||
|
||||
@AfterEach
|
||||
fun afterAll() {
|
||||
val downSQL = File(this::class.java.getResource("/fixtures/down.sql").toURI())
|
||||
connection.connect().apply {
|
||||
sendQuery(downSQL.readText()).join()
|
||||
}.disconnect()
|
||||
val downSQL = File(this::class.java.getResource("/fixtures/down.sql")!!.toURI())
|
||||
connection
|
||||
.apply { connect().sendQuery(downSQL.readText()).join() }
|
||||
.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
4
src/test/resources/sql/query/Test/selectMultiple.sql
Normal file
4
src/test/resources/sql/query/Test/selectMultiple.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', :name::text),
|
||||
json_build_object('id', '6085c12e-e94d-4ae1-b7ad-23acc7a82a98', 'name', :name::text || '-2')
|
||||
)
|
||||
@@ -0,0 +1,4 @@
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', ?::text),
|
||||
json_build_object('id', '6085c12e-e94d-4ae1-b7ad-23acc7a82a98', 'name', ?::text || '-2')
|
||||
)
|
||||
Reference in New Issue
Block a user