From bc0003e41944b2818cf05e327930a9438488138a Mon Sep 17 00:00:00 2001 From: Rodolfo Date: Fri, 6 Dec 2024 12:38:49 -0300 Subject: [PATCH] extracted evolveFunction --- .../postgres/AccountTransferScenarioTest.kt | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/src/test/kotlin/crablet/postgres/AccountTransferScenarioTest.kt b/src/test/kotlin/crablet/postgres/AccountTransferScenarioTest.kt index 07ba88e..3660552 100644 --- a/src/test/kotlin/crablet/postgres/AccountTransferScenarioTest.kt +++ b/src/test/kotlin/crablet/postgres/AccountTransferScenarioTest.kt @@ -348,6 +348,28 @@ class AccountTransferScenarioTest : AbstractCrabletTest() { val eventTypes = listOf("AccountOpened", "AmountDeposited", "AmountTransferred").map { EventName(it) } + private val evolveFunction: (Account, JsonObject) -> Account = { state, event -> + when (event.getString("type")) { + "AccountOpened" -> state.copy(id = event.getInteger("id")) + "AmountDeposited" -> state.copy(balance = state.balance.plus(event.getInteger("amount"))) + "AmountTransferred" -> { + when { + event.getInteger("fromAcct") == state.id -> state.copy( + balance = state.balance.minus(event.getInteger("amount")) + ) + + event.getInteger("toAcct") == state.id -> state.copy( + balance = state.balance.plus(event.getInteger("amount")) + ) + + else -> state + } + } + + else -> state + } + } + @BeforeAll @JvmStatic fun setUp(testContext: VertxTestContext) { @@ -355,33 +377,8 @@ class AccountTransferScenarioTest : AbstractCrabletTest() { stateBuilder = CrabletStateBuilder( client = pool, initialState = Account(), - evolveFunction = { state, event -> - when (event.getString("type")) { - "AccountOpened" -> state.copy(id = event.getInteger("id")) - "AmountDeposited" -> state.copy(balance = state.balance.plus(event.getInteger("amount"))) - "AmountTransferred" -> { - when { - event.getInteger("fromAcct") == state.id -> state.copy( - balance = state.balance.minus( - event.getInteger("amount") - ) - ) - - event.getInteger("toAcct") == state.id -> state.copy( - balance = state.balance.plus( - event.getInteger( - "amount" - ) - ) - ) - - else -> state - } - } - - else -> state - } - }) + evolveFunction = evolveFunction + ) cleanDatabase().onSuccess { testContext.completeNow() } } }