Skip to content

Commit

Permalink
causation and correlation test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodolfo committed Dec 26, 2024
1 parent 2079e80 commit 4377c93
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/test/kotlin/crablet/postgres/CausationCorrelationIdsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package crablet.postgres

import crablet.AppendCondition
import crablet.DomainIdentifier
import crablet.EventName
import crablet.SequenceNumber
import crablet.StateId
import crablet.StateName
import crablet.StreamQuery
import io.kotest.matchers.equals.shouldBeEqual
import io.kotest.matchers.ints.shouldBeExactly
import io.vertx.core.json.JsonObject
import io.vertx.junit5.VertxExtension
import io.vertx.junit5.VertxTestContext
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.MethodOrderer
import org.junit.jupiter.api.Order
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestMethodOrder
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(VertxExtension::class)
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class CausationCorrelationIdsTest : AbstractCrabletTest() {

@Test
@Order(1)
fun `it can open Account 1 with $100`(testContext: VertxTestContext) {
val testRepository = TestRepository(pool)

val streamQuery = StreamQuery(
identifiers = listOf(DomainIdentifier(name = StateName("Account"), id = StateId("1"))),
eventTypes = eventTypes
)
val appendCondition = AppendCondition(query = streamQuery, maximumEventSequence = SequenceNumber(0))
val eventsToAppend = listOf(
JsonObject().put("type", "AccountOpened").put("id", 1),
JsonObject().put("type", "AmountDeposited").put("amount", 50),
JsonObject().put("type", "AmountDeposited").put("amount", 50),
JsonObject().put("type", "AmountDeposited").put("amount", 50)
)
eventsAppender.appendIf(eventsToAppend, appendCondition)
.compose {
dumpEvents()
}
.compose {
testRepository.getSequences()
}
.onSuccess { ids ->
testContext.verify {
val expectedResults = listOf(
Triple(1L, 1L, 1L),
Triple(2L, 1L, 1L),
Triple(3L, 2L, 1L),
Triple(4L, 3L, 1L)
)
expectedResults.size shouldBeExactly ids.size
ids.forEachIndexed { index, triple ->
triple shouldBeEqual expectedResults[index]
}
}
testContext.completeNow()
}
.onFailure { it ->
testContext.failNow(it)
}

}

companion object {

lateinit var eventsAppender: CrabletEventsAppender
lateinit var testRepository: TestRepository

val eventTypes = listOf("AccountOpened", "AmountDeposited", "AmountTransferred").map { EventName(it) }

@BeforeAll
@JvmStatic
fun setUp(testContext: VertxTestContext) {
eventsAppender = CrabletEventsAppender(pool)
testRepository = TestRepository(pool)
cleanDatabase().onSuccess { testContext.completeNow() }
}
}
}
22 changes: 22 additions & 0 deletions src/test/kotlin/crablet/postgres/TestRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package crablet.postgres

import io.vertx.core.Future
import io.vertx.sqlclient.Pool
import io.vertx.sqlclient.Row

class TestRepository(private val client: Pool) {

fun getSequences(): Future<List<Triple<Long, Long, Long>>>? {
return client.query("select sequence_id, causation_id, correlation_id from events")
.execute()
.map {
it.map { row: Row ->
Triple(
row.getLong("sequence_id"),
row.getLong("causation_id"),
row.getLong("correlation_id")
)
}
}
}
}

0 comments on commit 4377c93

Please sign in to comment.