-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rodolfo
committed
Dec 26, 2024
1 parent
2079e80
commit 4377c93
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/test/kotlin/crablet/postgres/CausationCorrelationIdsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) | ||
} | ||
} | ||
} | ||
} |