Skip to content

Commit

Permalink
test: Verify variable access in filter expression
Browse files Browse the repository at this point in the history
Add a test case to verify that a list filter expression can access an item property and the property is not overridden by a variable with the same name.

(cherry picked from commit a61f15e)
  • Loading branch information
saig0 authored and github-actions[bot] committed Jan 2, 2024
1 parent 7a6f3b9 commit f5cff6e
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.camunda.feel.impl.interpreter

import org.camunda.feel.api.EvaluationFailureType
import org.camunda.feel.context.{CustomContext, VariableProvider}
import org.camunda.feel.impl.{EvaluationResultMatchers, FeelEngineTest}
import org.camunda.feel.syntaxtree._
import org.scalatest.flatspec.AnyFlatSpec
Expand Down Expand Up @@ -400,4 +401,36 @@ class InterpreterListExpressionTest
)
}

"A filter expression" should "access an item property if the context contains a variable with the same name" in {
evaluateExpression(
expression =
"""sum({"loans" : [
{"loanId" : "AAA001", "amount" : 10},
{"loanId" : "AAA002", "amount" : 20},
{"loanId" : "AAA001", "amount" : 50}
]}.loans[loanId = id].amount)""",
variables = Map("id" -> "AAA002", "loanId" -> "AAA002")
) should returnResult(20)
}

it should "access an item property if the custom context contains a variable with the same name" in {
evaluateExpression(
expression =
"""sum({"loans" : [
{"loanId" : "AAA001", "amount" : 10},
{"loanId" : "AAA002", "amount" : 20},
{"loanId" : "AAA001", "amount" : 50}
]}.loans[loanId = id].amount)""",
context = new MyContext(Map("id" -> "AAA002", "loanId" -> "AAA002"))
) should returnResult(20)
}

class MyContext(val vars: Map[String, Any]) extends CustomContext {
override def variableProvider: VariableProvider = new VariableProvider {
override def getVariable(name: String): Option[Any] = vars.get(name)

override def keys: Iterable[String] = vars.keys
}
}

}

0 comments on commit f5cff6e

Please sign in to comment.