Skip to content

Commit

Permalink
extracted evolveFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodolfo committed Dec 6, 2024
1 parent c0f1731 commit bc0003e
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions src/test/kotlin/crablet/postgres/AccountTransferScenarioTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -348,40 +348,37 @@ 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) {
eventsAppender = CrabletEventsAppender(pool)
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() }
}
}
Expand Down

0 comments on commit bc0003e

Please sign in to comment.