libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit f8c3bc1e9d29d7d9af6f35fb77e46cfe183c8225
parent 8a4535f52757f481f86c5e75cc42087488523528
Author: Florian Dold <florian.dold@gmail.com>
Date:   Fri,  5 Jun 2020 00:16:39 +0530

flush before using entity in a foreign key column

Diffstat:
Mnexus/src/test/kotlin/DBTest.kt | 77++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 52 insertions(+), 25 deletions(-)

diff --git a/nexus/src/test/kotlin/DBTest.kt b/nexus/src/test/kotlin/DBTest.kt @@ -6,35 +6,62 @@ import org.jetbrains.exposed.sql.StdOutSqlLogger import org.jetbrains.exposed.sql.addLogger import org.jetbrains.exposed.sql.transactions.transaction import org.junit.Test +import java.io.File + +/** + * Run a block after connecting to the test database. + * Cleans up the DB file afterwards. + */ +fun withTestDatabase(f: () -> Unit) { + val dbfile = "nexus-test.sqlite3" + File(dbfile).also { + if (it.exists()) { + it.delete() + } + } + Database.connect("jdbc:sqlite:$dbfile", "org.sqlite.JDBC") + try { + f() + } + finally { + File(dbfile).also { + if (it.exists()) { + it.delete() + } + } + } +} + class DBTest { @Test fun facadeConfigTest() { - Database.connect("jdbc:sqlite:on-the-fly-db.sqlite3", "org.sqlite.JDBC") - val talerConfig = transaction { - addLogger(StdOutSqlLogger) - SchemaUtils.create( - FacadesTable, - TalerFacadeConfigsTable, - NexusUsersTable - ) - TalerFacadeConfigEntity.new { - bankAccount = "b" - bankConnection = "b" - reserveTransferLevel = "any" - intervalIncrement = "any" - } - } - transaction { - val user = NexusUserEntity.new("u") { - passwordHash = "x" - superuser = true - } - FacadeEntity.new("my-id") { - type = "any" - creator = user - config = talerConfig - highestSeenMsgID = 0 + withTestDatabase { + transaction { + addLogger(StdOutSqlLogger) + SchemaUtils.create( + FacadesTable, + TalerFacadeConfigsTable, + NexusUsersTable + ) + val talerConfig = TalerFacadeConfigEntity.new { + bankAccount = "b" + bankConnection = "b" + reserveTransferLevel = "any" + intervalIncrement = "any" + } + talerConfig.id + talerConfig.flush() + val user = NexusUserEntity.new("u") { + passwordHash = "x" + superuser = true + } + FacadeEntity.new("my-id") { + type = "any" + creator = user + config = talerConfig + highestSeenMsgID = 0 + } } } }