improve-tests #28
@@ -1,7 +1,6 @@
|
|||||||
package fr.postgresjson.connexion
|
package fr.postgresjson.connexion
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
import com.fasterxml.jackson.core.type.TypeReference
|
||||||
import com.github.jasync.sql.db.Connection
|
|
||||||
import com.github.jasync.sql.db.QueryResult
|
import com.github.jasync.sql.db.QueryResult
|
||||||
import com.github.jasync.sql.db.ResultSet
|
import com.github.jasync.sql.db.ResultSet
|
||||||
import com.github.jasync.sql.db.general.ArrayRowData
|
import com.github.jasync.sql.db.general.ArrayRowData
|
||||||
@@ -14,7 +13,6 @@ import fr.postgresjson.entity.Serializable
|
|||||||
import fr.postgresjson.serializer.Serializer
|
import fr.postgresjson.serializer.Serializer
|
||||||
import fr.postgresjson.utils.LoggerDelegate
|
import fr.postgresjson.utils.LoggerDelegate
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import java.util.concurrent.CompletableFuture
|
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
|
|
||||||
typealias SelectOneCallback<T> = QueryResult.(T?) -> Unit
|
typealias SelectOneCallback<T> = QueryResult.(T?) -> Unit
|
||||||
@@ -50,7 +48,15 @@ class Connection(
|
|||||||
connection?.disconnect()
|
connection?.disconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
|
fun <A> inTransaction(block: Connection.() -> A?): A? = connect().run {
|
||||||
|
sendQuery("BEGIN")
|
||||||
|
try {
|
||||||
|
block().apply { sendQuery("COMMIT") }
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
sendQuery("ROLLBACK")
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Select One [EntityI] with [List] of parameters
|
* Select One [EntityI] with [List] of parameters
|
||||||
|
|||||||
@@ -73,8 +73,8 @@ data class Function(
|
|||||||
connection.inTransaction {
|
connection.inTransaction {
|
||||||
up()
|
up()
|
||||||
down()
|
down()
|
||||||
it.sendQuery("ROLLBACK")
|
sendQuery("ROLLBACK")
|
||||||
}.join()
|
}
|
||||||
|
|
||||||
return Status.OK
|
return Status.OK
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ data class MigrationScript(
|
|||||||
connection.inTransaction {
|
connection.inTransaction {
|
||||||
up()
|
up()
|
||||||
down()
|
down()
|
||||||
it.sendQuery("ROLLBACK")
|
sendQuery("ROLLBACK")
|
||||||
}.join()
|
}
|
||||||
|
|
||||||
return Migration.Status.OK
|
return Migration.Status.OK
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -360,4 +360,20 @@ class ConnectionTest : TestAbstract() {
|
|||||||
assertEquals("myName", rows[0].getString(0))
|
assertEquals("myName", rows[0].getString(0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `select one in transaction`() {
|
||||||
|
connection.inTransaction {
|
||||||
|
selectOne<ObjTestWithParameterObject>(
|
||||||
|
"SELECT json_build_object('first', :first::json, 'second', :second::json)",
|
||||||
|
mapOf(
|
||||||
|
"first" to ParameterObject("one"),
|
||||||
|
"second" to ParameterObject("two")
|
||||||
|
)
|
||||||
|
).let { result ->
|
||||||
|
assertEquals("one", result!!.first.third)
|
||||||
|
assertEquals("two", result.second.third)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user