Remove Mutable entities
This commit is contained in:
@@ -1,26 +1,27 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.entity.mutable.IdEntity
|
||||
import fr.postgresjson.entity.UuidEntity
|
||||
import fr.postgresjson.entity.Parameter
|
||||
import org.junit.Assert.*
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import java.util.*
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class ConnectionTest() : TestAbstract() {
|
||||
private class ObjTest(var name: String) : 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 ObjTestWithParameterObject(var first: ParameterObject, var seconde: ParameterObject) : IdEntity()
|
||||
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()
|
||||
private class ObjTestWithParameterObject(var first: ParameterObject, var seconde: ParameterObject) : UuidEntity()
|
||||
private class ParameterObject(var third: String) : Parameter
|
||||
|
||||
@Test
|
||||
fun getObject() {
|
||||
val obj: ObjTest? = connection.selectOne("select to_json(a) from test a limit 1")
|
||||
assertTrue(obj is ObjTest)
|
||||
assertTrue(obj!!.id == 1)
|
||||
assertTrue(obj!!.id == UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -39,32 +40,31 @@ class ConnectionTest() : TestAbstract() {
|
||||
)
|
||||
assertNotNull(objs)
|
||||
assertEquals(objs.size, 2)
|
||||
assertEquals(objs[0].id, 1)
|
||||
assertEquals(objs[0].test!!.id, 1)
|
||||
assertEquals(objs[0].id, UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
|
||||
assertEquals(objs[0].test!!.id, UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun callRequestWithArgs() {
|
||||
val result: ObjTest? = connection.selectOne("select json_build_object('id', 1, 'name', ?::text)", listOf("myName"))
|
||||
val result: ObjTest? = connection.selectOne("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::text)", listOf("myName"))
|
||||
assertNotNull(result)
|
||||
assertEquals("myName", result!!.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun callRequestWithArgsEntity() {
|
||||
val o = ObjTest("myName")
|
||||
o.id = 88
|
||||
val obj: ObjTest? = connection.selectOne("select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id int, name text);", listOf(o))
|
||||
val o = ObjTest("myName", id = UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"))
|
||||
val obj: ObjTest? = connection.selectOne("select json_build_object('id', id, 'name', name) FROM json_to_record(?::json) as o(id uuid, name text);", listOf(o))
|
||||
assertNotNull(obj)
|
||||
assertTrue(obj is ObjTest)
|
||||
assertEquals(obj!!.id, 88)
|
||||
assertEquals(obj!!.id, UUID.fromString("2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00"))
|
||||
assertEquals(obj.name, "myName")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun callExec() {
|
||||
val o = ObjTest("myName")
|
||||
val result = connection.exec("select json_build_object('id', 1, 'name', ?::json->>'name')", listOf(o))
|
||||
val result = connection.exec("select json_build_object('id', '2c0243ed-ff4d-4b9f-a52b-e38c71b0ed00', 'name', ?::json->>'name')", listOf(o))
|
||||
Assertions.assertEquals(1, result.rowsAffected)
|
||||
}
|
||||
|
||||
@@ -141,8 +141,8 @@ class ConnectionTest() : TestAbstract() {
|
||||
val result: Paginated<ObjTest> = connection.select(
|
||||
"""
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', 3, 'name', :name::text),
|
||||
json_build_object('id', 4, 'name', :name::text || '-2')
|
||||
json_build_object('id', '417aaa7e-7bc6-49b7-9fe8-6c8433b3f430', 'name', :name::text),
|
||||
json_build_object('id', 'abd46e7a-e749-4ce4-8361-e7b64da89da6', 'name', :name::text || '-2')
|
||||
), 10 as total
|
||||
LIMIT :limit OFFSET :offset
|
||||
""".trimIndent(),
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.entity.mutable.*
|
||||
import fr.postgresjson.entity.*
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import java.util.*
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class EntityTest() {
|
||||
private class User(id: Int?) : Entity<Int?>(id)
|
||||
private class ObjTest(var name: String) : UuidEntityExtended<Int?, User>(User(1), User(2))
|
||||
private class User(id: UUID = UUID.randomUUID()) : Entity<UUID>(id)
|
||||
private class ObjTest(var name: String) : UuidEntityExtended<Int?, User>(User(), User())
|
||||
|
||||
@Test
|
||||
fun getObject() {
|
||||
@@ -17,7 +17,7 @@ class EntityTest() {
|
||||
assertTrue(obj is ObjTest)
|
||||
assertTrue(obj is UuidEntityExtended<Int?, User>)
|
||||
assertTrue(obj is EntityI)
|
||||
assertTrue(obj is Entity<Int?>)
|
||||
assertTrue(obj is Entity<UUID>)
|
||||
assertTrue(obj is Published<User>)
|
||||
assertTrue(obj is EntityCreatedBy<User>)
|
||||
assertTrue(obj is EntityUpdatedBy<User>)
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.amshove.kluent.shouldThrow
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import java.util.*
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class MigrationTest() : TestAbstract() {
|
||||
@@ -103,7 +104,7 @@ class MigrationTest() : TestAbstract() {
|
||||
.getFunction("test_function")
|
||||
.selectOne(listOf("test", "plip"))
|
||||
|
||||
Assertions.assertEquals(objTest!!.id, 3)
|
||||
Assertions.assertEquals(objTest!!.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d"))
|
||||
Assertions.assertEquals(objTest.name, "test")
|
||||
}
|
||||
|
||||
@@ -119,7 +120,7 @@ class MigrationTest() : TestAbstract() {
|
||||
.getFunction("test_function_duplicate")
|
||||
.selectOne(listOf("test"))
|
||||
|
||||
Assertions.assertEquals(objTest!!.id, 3)
|
||||
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()
|
||||
|
||||
@@ -2,13 +2,14 @@ package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.entity.mutable.IdEntity
|
||||
import fr.postgresjson.entity.UuidEntity
|
||||
import org.junit.Assert
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.*
|
||||
|
||||
class RequesterTest : TestAbstract() {
|
||||
class ObjTest(var name: String) : IdEntity(1)
|
||||
class ObjTest(var name: String, id: UUID = UUID.fromString("5623d902-3067-42f3-bfd9-095dbb12c29f")) : UuidEntity(id)
|
||||
|
||||
@Test
|
||||
fun `get query from file`() {
|
||||
@@ -18,7 +19,7 @@ class RequesterTest : TestAbstract() {
|
||||
.getQuery("selectOne")
|
||||
.selectOne()
|
||||
|
||||
assertEquals(objTest!!.id, 2)
|
||||
assertEquals(objTest!!.id, UUID.fromString("829b1a29-5db8-47f9-9562-961c561ac528"))
|
||||
assertEquals(objTest.name, "test")
|
||||
}
|
||||
|
||||
@@ -30,7 +31,7 @@ class RequesterTest : TestAbstract() {
|
||||
.getFunction("test_function")
|
||||
.selectOne(listOf("test", "plip"))
|
||||
|
||||
assertEquals(objTest!!.id, 3)
|
||||
assertEquals(objTest!!.id, UUID.fromString("457daad5-4f1b-4eb7-80ec-6882adb8cc7d"))
|
||||
assertEquals(objTest.name, "test")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.entity.mutable.IdEntity
|
||||
import fr.postgresjson.entity.UuidEntity
|
||||
import fr.postgresjson.serializer.Serializer
|
||||
import fr.postgresjson.serializer.deserialize
|
||||
import fr.postgresjson.serializer.serialize
|
||||
@@ -10,23 +10,23 @@ import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import java.util.*
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
internal class SerializerTest {
|
||||
private class ObjTest(var val1: String, var val2: Int) : IdEntity(1)
|
||||
private class ObjTestDate(var val1: DateTime) : IdEntity(2)
|
||||
private class ObjTest(var val1: String, var val2: Int, id: UUID = UUID.fromString("1e5f5d41-6d14-4007-897b-0ed2616bec96")) : UuidEntity(id)
|
||||
private class ObjTestDate(var val1: DateTime, id: UUID = UUID.fromString("829b1a29-5db8-47f9-9562-961c561ac528")) : UuidEntity(id)
|
||||
|
||||
private val serializer = Serializer()
|
||||
|
||||
private val objSerialized: String = """{"val1":"plop","val2":123,"id":2}"""
|
||||
private val objSerializedWithExtra: String = """{"val1":"plop","val2":123,"id":2,"toto":"tata"}"""
|
||||
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
|
||||
fun before() {
|
||||
obj = ObjTest("plop", 123)
|
||||
obj.id = 2
|
||||
obj = ObjTest("plop", 123, UUID.fromString("829b1a29-5db8-47f9-9562-961c561ac528"))
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -8,7 +8,7 @@ create schema if not exists public;
|
||||
|
||||
create table if not exists test
|
||||
(
|
||||
id serial not null
|
||||
id uuid not null
|
||||
constraint test_pk
|
||||
primary key,
|
||||
name text
|
||||
@@ -16,24 +16,24 @@ create table if not exists test
|
||||
|
||||
create table if not exists test2
|
||||
(
|
||||
id serial not null,
|
||||
id uuid not null,
|
||||
title text,
|
||||
test_id integer
|
||||
test_id uuid
|
||||
constraint test2_test_id_fk
|
||||
references test
|
||||
);
|
||||
|
||||
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;
|
||||
INSERT INTO test (id, name) VALUES ('1e5f5d41-6d14-4007-897b-0ed2616bec96', 'plop') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO test2 (id, title, test_id) VALUES ('1e5f5d41-6d14-4007-897b-0ed2616bec96', 'plop', '1e5f5d41-6d14-4007-897b-0ed2616bec96') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO test2 (id, title, test_id) VALUES ('829b1a29-5db8-47f9-9562-961c561ac528', 'plip', '1e5f5d41-6d14-4007-897b-0ed2616bec96') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO test2 (id, title, test_id) VALUES ('457daad5-4f1b-4eb7-80ec-6882adb8cc7d', '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', name);
|
||||
result = json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name);
|
||||
END;
|
||||
$$;
|
||||
|
||||
@@ -44,8 +44,8 @@ AS
|
||||
$$
|
||||
BEGIN
|
||||
result = json_build_array(
|
||||
json_build_object('id', 3, 'name', name),
|
||||
json_build_object('id', 4, 'name', hi)
|
||||
json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name),
|
||||
json_build_object('id', '8d20abb0-7f77-4b6c-9991-44acd3c88faa', 'name', hi)
|
||||
);
|
||||
END;
|
||||
$$;
|
||||
@@ -56,8 +56,8 @@ AS
|
||||
$$
|
||||
BEGIN
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', 3, 'name', name::text),
|
||||
json_build_object('id', 4, 'name', name::text || '-2')
|
||||
json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name::text),
|
||||
json_build_object('id', '8d20abb0-7f77-4b6c-9991-44acd3c88faa', 'name', name::text || '-2')
|
||||
),
|
||||
10
|
||||
INTO result, total
|
||||
@@ -70,7 +70,7 @@ CREATE OR REPLACE FUNCTION test_function_object (inout resource json)
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
resource = json_build_object('id', 1, 'name', 'changedName');
|
||||
resource = json_build_object('id', '1e5f5d41-6d14-4007-897b-0ed2616bec96', 'name', 'changedName');
|
||||
END;
|
||||
$$;
|
||||
|
||||
|
||||
@@ -3,6 +3,6 @@ LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
result = json_build_object('id', 3, 'name', name);
|
||||
result = json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name);
|
||||
END;
|
||||
$$
|
||||
@@ -3,6 +3,6 @@ LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
resource = json_build_object('id', 1, 'name', 'changedName');
|
||||
resource = json_build_object('id', '1e5f5d41-6d14-4007-897b-0ed2616bec96', 'name', 'changedName');
|
||||
END;
|
||||
$$
|
||||
@@ -4,8 +4,8 @@ AS
|
||||
$$
|
||||
BEGIN
|
||||
result = json_build_array(
|
||||
json_build_object('id', 3, 'name', name),
|
||||
json_build_object('id', 4, 'name', hi)
|
||||
json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name),
|
||||
json_build_object('id', '8d20abb0-7f77-4b6c-9991-44acd3c88faa', 'name', hi)
|
||||
);
|
||||
END;
|
||||
$$
|
||||
@@ -4,8 +4,8 @@ AS
|
||||
$$
|
||||
BEGIN
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', 3, 'name', name::text),
|
||||
json_build_object('id', 4, 'name', name::text || '-2')
|
||||
json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name::text),
|
||||
json_build_object('id', '8d20abb0-7f77-4b6c-9991-44acd3c88faa', 'name', name::text || '-2')
|
||||
),
|
||||
10
|
||||
INTO result, total
|
||||
|
||||
@@ -3,6 +3,6 @@ CREATE OR REPLACE FUNCTION test_function_duplicate (name text default 'plop') re
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
return json_build_object('id', 3, 'name', name);
|
||||
return json_build_object('id', '457daad5-4f1b-4eb7-80ec-6882adb8cc7d', 'name', name);
|
||||
END;
|
||||
$$
|
||||
|
||||
@@ -1 +1 @@
|
||||
select json_build_object('id', 2, 'name', 'test');
|
||||
select json_build_object('id', '829b1a29-5db8-47f9-9562-961c561ac528', 'name', 'test');
|
||||
@@ -1 +1 @@
|
||||
select json_build_object('id', 2, 'name', :name::text), 'plop'::text as other;
|
||||
select json_build_object('id', '829b1a29-5db8-47f9-9562-961c561ac528', 'name', :name::text), 'plop'::text as other;
|
||||
@@ -1,5 +1,5 @@
|
||||
SELECT json_build_array(
|
||||
json_build_object('id', 3, 'name', :name::text),
|
||||
json_build_object('id', 4, 'name', :name::text || '-2')
|
||||
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')
|
||||
), 10 as total
|
||||
LIMIT :limit OFFSET :offset
|
||||
Reference in New Issue
Block a user