-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MemoryView implementation -- tests and remarks
- Loading branch information
1 parent
e9f081e
commit 145a95c
Showing
5 changed files
with
147 additions
and
10 deletions.
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
...ication-arrow/src/commonTest/kotlin/com/fraktalio/fmodel/application/EphemeralViewTest.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,86 @@ | ||
package com.fraktalio.fmodel.application | ||
|
||
import arrow.core.Either | ||
import com.fraktalio.fmodel.application.examples.numbers.even.query.EvenNumberEphemeralViewRepository | ||
import com.fraktalio.fmodel.application.examples.numbers.even.query.evenNumberEphemeralViewRepository | ||
import com.fraktalio.fmodel.domain.IView | ||
import com.fraktalio.fmodel.domain.examples.numbers.api.Description | ||
import com.fraktalio.fmodel.domain.examples.numbers.api.EvenNumberState | ||
import com.fraktalio.fmodel.domain.examples.numbers.api.NumberValue | ||
import com.fraktalio.fmodel.domain.examples.numbers.even.query.evenNumberView | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.types.shouldBeInstanceOf | ||
|
||
/** | ||
* DSL - Given | ||
*/ | ||
private suspend fun <S, E, Q> IView<S, E>.given(repository: EphemeralViewRepository<E, Q>, query: () -> Q): Either<Error, S> = | ||
EphemeralView( | ||
view = this, | ||
ephemeralViewRepository = repository | ||
).handleWithEffect(query()) | ||
|
||
/** | ||
* DSL - When | ||
*/ | ||
private fun <Q> whenQuery(query: Q): Q = query | ||
|
||
/** | ||
* DSL - Then | ||
*/ | ||
private infix fun <S> Either<Error, S>.thenState(expected: S) { | ||
val state = when (this) { | ||
is Either.Right -> value | ||
is Either.Left -> throw AssertionError("Expected Either.Right, but found Either.Left with value $value") | ||
} | ||
state shouldBe expected | ||
} | ||
|
||
private fun <S> Either<Error, S>.thenError() { | ||
val error = when (this) { | ||
is Either.Right -> throw AssertionError("Expected Either.Left, but found Either.Right with value $value") | ||
is Either.Left -> value | ||
} | ||
error.shouldBeInstanceOf<Error>() | ||
} | ||
|
||
/** | ||
* Ephemeral View Test | ||
*/ | ||
class EphemeralViewTest : FunSpec({ | ||
val evenView = evenNumberView() | ||
val ephemeralViewRepository = evenNumberEphemeralViewRepository() as EvenNumberEphemeralViewRepository | ||
|
||
test("Ephemeral View - load number flow 1") { | ||
with(evenView) { | ||
given(ephemeralViewRepository) { | ||
whenQuery(1) | ||
} thenState EvenNumberState(Description("Initial state, Number 2, Number 4"), NumberValue(6)) | ||
} | ||
} | ||
|
||
test("Ephemeral View - load number flow 2") { | ||
with(evenView) { | ||
given(ephemeralViewRepository) { | ||
whenQuery(2) | ||
} thenState EvenNumberState(Description("Initial state, Number 4, Number 2"), NumberValue(2)) | ||
} | ||
} | ||
|
||
test("Ephemeral View - load number flow 3 - with error") { | ||
with(evenView) { | ||
given(ephemeralViewRepository) { | ||
whenQuery(3) | ||
}.thenError() | ||
} | ||
} | ||
|
||
test("Ephemeral View - load non-existing number flow") { | ||
with(evenView) { | ||
given(ephemeralViewRepository) { | ||
whenQuery(4) | ||
} thenState EvenNumberState(Description("Initial state"), NumberValue(0)) | ||
} | ||
} | ||
}) |
44 changes: 44 additions & 0 deletions
44
...talio/fmodel/application/examples/numbers/even/query/EvenNumberEphemeralViewRepository.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,44 @@ | ||
package com.fraktalio.fmodel.application.examples.numbers.even.query | ||
|
||
import com.fraktalio.fmodel.application.EphemeralViewRepository | ||
import com.fraktalio.fmodel.domain.examples.numbers.api.Description | ||
import com.fraktalio.fmodel.domain.examples.numbers.api.NumberEvent | ||
import com.fraktalio.fmodel.domain.examples.numbers.api.NumberValue | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.emptyFlow | ||
import kotlinx.coroutines.flow.flowOf | ||
|
||
/** | ||
* Simple flows of events to represent previously stored events in the event store | ||
*/ | ||
private var numberFlow1 = flowOf( | ||
NumberEvent.EvenNumberEvent.EvenNumberAdded(Description("Number 2"), NumberValue(2)), | ||
NumberEvent.EvenNumberEvent.EvenNumberAdded(Description("Number 4"), NumberValue(4)) | ||
) | ||
|
||
private var numberFlow2 = flowOf( | ||
NumberEvent.EvenNumberEvent.EvenNumberAdded(Description("Number 4"), NumberValue(4)), | ||
NumberEvent.EvenNumberEvent.EvenNumberSubtracted(Description("Number 2"), NumberValue(2)) | ||
) | ||
|
||
/** | ||
* Even number ephemeral view implementation | ||
*/ | ||
class EvenNumberEphemeralViewRepository : EphemeralViewRepository<NumberEvent.EvenNumberEvent?, Int> { | ||
|
||
override fun Int.fetchEvents(): Flow<NumberEvent.EvenNumberEvent?> { | ||
return when (this) { | ||
1 -> numberFlow1 | ||
2 -> numberFlow2 | ||
3 -> throw RuntimeException("Some fake error while fetching events.") | ||
else -> emptyFlow() | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Helper function to create an [EvenNumberEphemeralViewRepository] | ||
*/ | ||
fun evenNumberEphemeralViewRepository(): EphemeralViewRepository<NumberEvent.EvenNumberEvent?, Int> = | ||
EvenNumberEphemeralViewRepository() |
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
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