Skip to content

Commit b07b67c

Browse files
committed
Add unit tests for BooksAvailableReadModel to validate restoreOnly and catchup behaviors
1 parent 4d94dcc commit b07b67c

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

krescent-core/src/test/kotlin/dev/helight/krescent/bookstore/BookCountReadModelTest.kt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,52 @@
11
package dev.helight.krescent.bookstore
22

3+
import dev.helight.krescent.checkpoint.AlwaysCheckpointStrategy
4+
import dev.helight.krescent.checkpoint.impl.InMemoryCheckpointStorage
35
import dev.helight.krescent.event.Event
46
import dev.helight.krescent.model.EventModelBase.Extension.withConfiguration
57
import dev.helight.krescent.model.ReadModelBase.Extension.catchup
8+
import dev.helight.krescent.model.ReadModelBase.Extension.restoreOnly
69
import dev.helight.krescent.model.ReducingReadModel
710
import dev.helight.krescent.source.impl.InMemoryEventStore
811
import kotlinx.coroutines.runBlocking
912
import kotlinx.serialization.Serializable
1013
import java.util.concurrent.atomic.AtomicInteger
1114
import kotlin.test.Test
15+
import kotlin.test.assertEquals
16+
import kotlin.test.assertNotNull
17+
import kotlin.test.assertNull
1218

1319
class BookCountReadModelTest {
1420

1521
@Test
16-
fun `Test my other model shit`() = runBlocking {
22+
fun `Test runs without exception`(): Unit = runBlocking {
1723
BooksAvailableReadModel().withConfiguration {
1824

1925
}.catchup(InMemoryEventStore())
2026
}
27+
28+
@Test
29+
fun `Test restore only operation`(): Unit = runBlocking {
30+
val source = InMemoryEventStore()
31+
val checkpointStorage = InMemoryCheckpointStorage()
32+
source.publishAll(bookstoreSimulatedEventStream)
33+
34+
val initial = BooksAvailableReadModel().withConfiguration {
35+
useCheckpoints(checkpointStorage, AlwaysCheckpointStrategy)
36+
}.restoreOnly()
37+
assertNull(initial)
38+
39+
val caughtUp = BooksAvailableReadModel().withConfiguration {
40+
useCheckpoints(checkpointStorage, AlwaysCheckpointStrategy)
41+
}.catchup(source)
42+
assertEquals(9, caughtUp.target["1"])
43+
44+
val late = BooksAvailableReadModel().withConfiguration {
45+
useCheckpoints(checkpointStorage, AlwaysCheckpointStrategy)
46+
}.restoreOnly()
47+
assertNotNull(late)
48+
assertEquals(9, late.target["1"])
49+
}
2150
}
2251

2352
class BooksAvailableReadModel(

0 commit comments

Comments
 (0)