Skip to content

Commit

Permalink
test: more tests for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jbee committed Oct 12, 2023
1 parent f79c6ee commit f581729
Show file tree
Hide file tree
Showing 32 changed files with 752 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ fun interface Typed {
}

fun toStringTypeCoercion(value: Any?): String? {
return if (value == null) null
else (value as? VariableValue)?.valueOrDefault()?.toString() ?: value.toString()
if (value == null) return null
if (value is VariableValue) return toStringTypeCoercion(value.valueOrDefault());
if (value is Number) return if (isNonFractionValue(value)) value.toInt().toString() else value.toString();
return value.toString()
}

fun toMixedTypeTypeCoercion(value: Any?): Any? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fun interface ExpressionFunctions {
fun d2_minutesBetween(start: LocalDate?, end: LocalDate?): Int {
require(start != null) { "start parameter of d2:minutesBetween must not be null" }
require(end != null) { "end parameter of d2:minutesBetween must not be null" }
return d2_daysBetween(start, end).times(25 * 60)
return d2_daysBetween(start, end).times(24 * 60)
}

fun d2_minValue(value: VariableValue?): Double {
Expand Down Expand Up @@ -285,7 +285,7 @@ fun interface ExpressionFunctions {
fun d2_yearsBetween(start: LocalDate?, end: LocalDate?): Int {
require(start != null) { "start parameter of d2:yearsBetween must not be null" }
require(end != null) { "end parameter of d2:yearsBetween must not be null" }
return if (start.toEpochDays() < end.toEpochDays()) start.yearsUntil(end) / 7 else end.yearsUntil(start) / 7
return if (start.toEpochDays() < end.toEpochDays()) start.yearsUntil(end) else end.yearsUntil(start)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object Literals {
}
}

fun parseUnaryOp(expr: Expr): String {
private fun parseUnaryOp(expr: Expr): String {
val c = expr.peek()
if (isUnaryOperator(c)) {
expr.error("unary operator")
Expand Down Expand Up @@ -82,7 +82,7 @@ object Literals {
return expr.rawMatch("name", Chars::isName)
}

fun parseInteger(expr: Expr): String {
private fun parseInteger(expr: Expr): String {
val s = expr.position()
expr.gobbleIf(Chars::isSignOperator)
expr.skipWhile(Chars::isDigit)
Expand All @@ -109,11 +109,11 @@ object Literals {
return expr.raw(s)
}

fun parseBoolean(expr: Expr): String {
private fun parseBoolean(expr: Expr): String {
return expr.rawMatch("boolean", if (expr.peek() == 't') "true" else "false")
}

fun parseDate(expr: Expr): String {
private fun parseDate(expr: Expr): String {
// [1-9] [0-9] [0-9] [0-9] '-' [0-1]? [0-9] '-' [0-3]? [0-9]
val s = expr.position()
expr.expect("digit", Chars::isDigit)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hisp.dhis.lib.expression
package org.hisp.dhis.lib.expression.function

import kotlinx.datetime.LocalDate
import org.hisp.dhis.lib.expression.Expression
import org.hisp.dhis.lib.expression.spi.IllegalExpressionException
import org.hisp.dhis.lib.expression.Expression.Mode

Expand All @@ -11,12 +12,11 @@ import kotlin.test.assertFailsWith
/**
* Test of the `d2:ceil` function.
*
*
* Translated from existing test of same name in rule-engine.
*
* @author Jan Bernitt
*/
internal class RuleFunctionAddDaysTest {
internal class AddDaysTest {
@Test
fun return_new_date_with_days_added() {
assertEquals(LocalDate.parse("2011-01-07"), evaluate("d2:addDays('2011-01-01', 6.0)"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.hisp.dhis.lib.expression.function

import org.hisp.dhis.lib.expression.Expression
import org.hisp.dhis.lib.expression.spi.*

abstract class AggregateFunctionTest {

fun evaluate(expression: String, dataValues: Map<DataItem, Any>): Double {
return Expression(expression).evaluate(
{ _: String -> null },
ExpressionData().copy(dataItemValues = dataValues)) as Double
}

fun newDeDataItem(u1234567890: String): DataItem {
return DataItem(
DataItemType.DATA_ELEMENT, ID(ID.Type.DataElementUID, u1234567890),
QueryModifiers().copy(periodAggregation = true))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.hisp.dhis.lib.expression.function

import kotlin.test.Test
import kotlin.test.assertEquals

/**
* Tests the `avg` function
*
* @author Jan Bernitt
*/
internal class AvgTest : AggregateFunctionTest() {

@Test
fun testAvg_Single() {
val dataValues = mapOf(
newDeDataItem("u1234567890") to doubleArrayOf(0.0, 10.0, 5.0, 3.0, 7.0))
assertEquals(5.0, evaluate("avg(#{u1234567890})", dataValues))
}

@Test
fun testAvg_Complex() {
val dataValues = mapOf(
newDeDataItem("u1234567890") to doubleArrayOf(0.0, 10.0, 5.0, 3.0, 7.0),
newDeDataItem("v1234567890") to doubleArrayOf(0.0, 10.0, 5.0, 3.0, 7.0))
assertEquals(10.0, evaluate("avg(#{u1234567890} + #{v1234567890})", dataValues))
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hisp.dhis.lib.expression
package org.hisp.dhis.lib.expression.function

import org.hisp.dhis.lib.expression.Expression
import org.hisp.dhis.lib.expression.spi.IllegalExpressionException
import org.hisp.dhis.lib.expression.Expression.Mode

Expand All @@ -15,7 +16,7 @@ import kotlin.test.assertFailsWith
*
* @author Jan Bernitt
*/
internal class RuleFunctionCeilTest {
internal class CeilTest {
@Test
fun evaluateMustReturnCeiledValue() {
assertEquals(5.0, evaluate("d2:ceil(4.1)"))
Expand Down
Loading

0 comments on commit f581729

Please sign in to comment.