-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase coroutines module coverage. (#386)
- Loading branch information
Showing
5 changed files
with
161 additions
and
2 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
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
20 changes: 20 additions & 0 deletions
20
formula-coroutines/src/test/java/com/instacart/formula/coroutines/FlowActionTest.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,20 @@ | ||
package com.instacart.formula.coroutines | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.flow.flowOf | ||
import org.junit.Test | ||
|
||
class FlowActionTest { | ||
|
||
@Test | ||
fun `default key is null`() { | ||
val action = FlowAction.fromFlow { flowOf("") } | ||
assertThat(action.key()).isNull() | ||
} | ||
|
||
@Test | ||
fun `specified key`() { | ||
val action = FlowAction.fromFlow("unique-key") { flowOf("") } | ||
assertThat(action.key()).isEqualTo("unique-key") | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
formula-coroutines/src/test/java/com/instacart/formula/coroutines/FlowRuntimeTest.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,136 @@ | ||
package com.instacart.formula.coroutines | ||
|
||
import app.cash.turbine.test | ||
import com.google.common.truth.Truth | ||
import com.instacart.formula.Evaluation | ||
import com.instacart.formula.RuntimeConfig | ||
import com.instacart.formula.Snapshot | ||
import com.instacart.formula.StatelessFormula | ||
import com.instacart.formula.test.CountingInspector | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
|
||
class FlowRuntimeTest { | ||
|
||
@Test | ||
fun `toFlow with unit input and no config`() { | ||
val formula = object : StatelessFormula<Unit, String>() { | ||
override fun Snapshot<Unit, Unit>.evaluate(): Evaluation<String> { | ||
return Evaluation("output") | ||
} | ||
} | ||
|
||
runTest { | ||
formula.toFlow().test { | ||
Truth.assertThat(awaitItem()).isEqualTo("output") | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `toFlow with unit input and config`() { | ||
val formula = object : StatelessFormula<Unit, String>() { | ||
override fun Snapshot<Unit, Unit>.evaluate(): Evaluation<String> { | ||
return Evaluation("output") | ||
} | ||
} | ||
|
||
val inspector = CountingInspector() | ||
runTest { | ||
formula.toFlow(config = RuntimeConfig(inspector = inspector)).test { | ||
Truth.assertThat(awaitItem()).isEqualTo("output") | ||
} | ||
} | ||
inspector.assertEvaluationCount(1) | ||
} | ||
|
||
@Test | ||
fun `toFlow with input value and no config`() { | ||
val formula = object : StatelessFormula<String, String>() { | ||
override fun Snapshot<String, Unit>.evaluate(): Evaluation<String> { | ||
return Evaluation(input) | ||
} | ||
} | ||
|
||
runTest { | ||
formula.toFlow("output").test { | ||
Truth.assertThat(awaitItem()).isEqualTo("output") | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `toFlow with input value and config`() { | ||
val formula = object : StatelessFormula<String, String>() { | ||
override fun Snapshot<String, Unit>.evaluate(): Evaluation<String> { | ||
return Evaluation(input) | ||
} | ||
} | ||
|
||
val inspector = CountingInspector() | ||
runTest { | ||
formula.toFlow("output", RuntimeConfig(inspector = inspector)).test { | ||
Truth.assertThat(awaitItem()).isEqualTo("output") | ||
} | ||
} | ||
inspector.assertEvaluationCount(1) | ||
} | ||
|
||
@Test | ||
fun `toFlow with input flow and no config`() { | ||
val formula = object : StatelessFormula<String, String>() { | ||
override fun Snapshot<String, Unit>.evaluate(): Evaluation<String> { | ||
return Evaluation(input) | ||
} | ||
} | ||
|
||
runTest { | ||
formula.toFlow(flowOf("output", "output-2")).test { | ||
Truth.assertThat(awaitItem()).isEqualTo("output") | ||
Truth.assertThat(awaitItem()).isEqualTo("output-2") | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `toFlow with input flow and config`() { | ||
val formula = object : StatelessFormula<String, String>() { | ||
override fun Snapshot<String, Unit>.evaluate(): Evaluation<String> { | ||
return Evaluation(input) | ||
} | ||
} | ||
|
||
val inspector = CountingInspector() | ||
runTest { | ||
formula.toFlow( | ||
input = flowOf("output", "output-2"), | ||
config = RuntimeConfig(inspector = inspector), | ||
).test { | ||
Truth.assertThat(awaitItem()).isEqualTo("output") | ||
Truth.assertThat(awaitItem()).isEqualTo("output-2") | ||
} | ||
} | ||
inspector.assertEvaluationCount(2) | ||
} | ||
|
||
@Test | ||
fun `toFlow with input flow with duplicate values`() { | ||
val formula = object : StatelessFormula<String, String>() { | ||
override fun Snapshot<String, Unit>.evaluate(): Evaluation<String> { | ||
return Evaluation(input) | ||
} | ||
} | ||
|
||
val inspector = CountingInspector() | ||
runTest { | ||
formula.toFlow( | ||
input = flowOf("output", "output", "output"), | ||
config = RuntimeConfig(inspector = inspector), | ||
).test { | ||
Truth.assertThat(awaitItem()).isEqualTo("output") | ||
} | ||
} | ||
inspector.assertEvaluationCount(1) | ||
} | ||
} |
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