Skip to content

Commit

Permalink
Add tests for formula validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux committed Sep 12, 2024
1 parent 4c59783 commit e912f72
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
}
}

// TODO: I'm not sure if this is the right behaviro
// TODO: I'm not sure if this is the right behavior
@Test
fun `action termination events are ignored`() {
val formula = object : Formula<Boolean, Int, Int>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.instacart.formula

import com.google.common.truth.Truth
import com.instacart.formula.test.test
import org.junit.Test
import java.util.concurrent.atomic.AtomicInteger

class FormulaValidationTest {

@Test
fun `input changed during re-evaluation will throw validation error`() {
val childFormula = object : StatelessFormula<Int, Int>() {
override fun Snapshot<Int, Unit>.evaluate(): Evaluation<Int> {
return Evaluation(output = input)
}
}

val parentFormula = object : StatelessFormula<Unit, Int>() {
val unstableInput = AtomicInteger(0)
override fun Snapshot<Unit, Unit>.evaluate(): Evaluation<Int> {
return Evaluation(
output = context.child(childFormula, unstableInput.incrementAndGet())
)
}
}

val error = runCatching {
parentFormula.test(isValidationEnabled = true).input(Unit)
}

Truth.assertThat(error.exceptionOrNull()).hasMessageThat().contains(
"- input changed during identical re-evaluation - old: 1, new: 2"
)
}

@Test
fun `output changed during re-evaluation will throw validation error`() {
val formula = object : StatelessFormula<Unit, Int>() {
val unstableOutput = AtomicInteger(0)
override fun Snapshot<Unit, Unit>.evaluate(): Evaluation<Int> {
return Evaluation(
output = unstableOutput.incrementAndGet()
)
}
}
val error = runCatching {
formula.test(isValidationEnabled = true).input(Unit)
}
Truth.assertThat(error.exceptionOrNull()).hasMessageThat().contains(
"- output changed during identical re-evaluation - old: 1, new: 2"
)
}

@Test
fun `action key changed during re-evaluation will throw validation error`() {
val formula = object : StatelessFormula<Unit, Int>() {
val unstableActionKey = AtomicInteger(0)
override fun Snapshot<Unit, Unit>.evaluate(): Evaluation<Int> {
return Evaluation(
output = 0,
context.actions {
val action = object : Action<Unit> {
override fun start(send: (Unit) -> Unit): Cancelable? {
return null
}

override fun key(): Any {
return unstableActionKey.incrementAndGet()
}
}

action.onEvent {
none()
}
}
)
}
}
val error = runCatching {
formula.test(isValidationEnabled = true).input(Unit)
}
Truth.assertThat(error.exceptionOrNull()).hasMessageThat().contains(
"action keys changed during identical re-evaluation"
)
}
}

0 comments on commit e912f72

Please sign in to comment.