Skip to content

Commit

Permalink
Move tests to common
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Dec 14, 2023
1 parent c47c2f9 commit dbc11b0
Show file tree
Hide file tree
Showing 24 changed files with 228 additions and 229 deletions.
6 changes: 1 addition & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ kotlin {
}
}
val jvmMain by getting
val jvmTest by getting {
dependencies {
implementation("io.mockk:mockk:1.13.8")
}
}
val jvmTest by getting
val jsMain by getting
val jsTest by getting
val nativeMain by getting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class ProgramRuleVariableTest {
fun testCurrentDateProgramVariableIsAssigned() {
val rule = getRule("V{current_date}")
val ruleEffects = callEnrollmentRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule, org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.CURRENT_DATE.toString())
assertProgramRuleVariableAssignment(ruleEffects, rule, CURRENT_DATE.toString())
}

@Test
fun testDueDateProgramVariableIsAssigned() {
val rule = getRule("V{due_date}")
val ruleEffects = callEventRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.DUE_DATE_STRING
DUE_DATE_STRING
)
}

Expand All @@ -61,7 +61,7 @@ class ProgramRuleVariableTest {
val rule = getRule("V{enrollment_date}")
val ruleEffects = callEnrollmentRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.ENROLLMENT_DATE_STRING
ENROLLMENT_DATE_STRING
)
}

Expand All @@ -70,15 +70,15 @@ class ProgramRuleVariableTest {
val rule = getRule("V{enrollment_id}")
val ruleEffects = callEnrollmentRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.ENROLLMENT_ID
ENROLLMENT_ID
)
}

@Test
fun testEnrollmentStatusProgramVariableIsAssigned() {
val rule = getRule("V{enrollment_status}")
val ruleEffects = callEnrollmentRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule, org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.ENROLLMENT_STATUS.name)
assertProgramRuleVariableAssignment(ruleEffects, rule, ENROLLMENT_STATUS.name)
}

@Test
Expand All @@ -100,7 +100,7 @@ class ProgramRuleVariableTest {
val rule = getRule("V{event_date}")
val ruleEffects = callEventRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.EVENT_DATE_STRING
EVENT_DATE_STRING
)
}

Expand All @@ -109,23 +109,23 @@ class ProgramRuleVariableTest {
val rule = getRule("V{event_id}")
val ruleEffects = callEventRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.EVENT_ID
EVENT_ID
)
}

@Test
fun testEventStatusProgramVariableIsAssigned() {
val rule = getRule("V{event_status}")
val ruleEffects = callEventRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule, org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.RULE_EVENT_STATUS.name)
assertProgramRuleVariableAssignment(ruleEffects, rule, RULE_EVENT_STATUS.name)
}

@Test
fun testIncidentDateProgramVariableIsAssigned() {
val rule = getRule("V{incident_date}")
val ruleEffects = callEventRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.INCIDENT_DATE_STRING
INCIDENT_DATE_STRING
)
}

Expand All @@ -134,7 +134,7 @@ class ProgramRuleVariableTest {
val rule = getRule("V{org_unit}")
val ruleEffects = callEnrollmentRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.ORGANISATION_UNIT
ORGANISATION_UNIT
)
}

Expand All @@ -143,7 +143,7 @@ class ProgramRuleVariableTest {
val rule = getRule("V{orgunit_code}")
val ruleEffects = callEventRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.ORGANISATION_UNIT_CODE
ORGANISATION_UNIT_CODE
)
}

Expand All @@ -152,7 +152,7 @@ class ProgramRuleVariableTest {
val rule = getRule("V{program_name}")
val ruleEffects = callEnrollmentRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.PROGRAM_NAME
PROGRAM_NAME
)
}

Expand All @@ -161,7 +161,7 @@ class ProgramRuleVariableTest {
val rule = getRule("V{program_stage_id}")
val ruleEffects = callEventRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.PROGRAM_STAGE
PROGRAM_STAGE
)
}

Expand All @@ -170,7 +170,7 @@ class ProgramRuleVariableTest {
val rule = getRule("V{program_stage_name}")
val ruleEffects = callEventRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.PROGRAM_STAGE_NAME
PROGRAM_STAGE_NAME
)
}

Expand Down Expand Up @@ -216,31 +216,32 @@ class ProgramRuleVariableTest {

private val enrollment: org.hisp.dhis.rules.models.RuleEnrollment
get() = org.hisp.dhis.rules.models.RuleEnrollment(
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.ENROLLMENT_ID,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.PROGRAM_NAME,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.INCIDENT_DATE,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.ENROLLMENT_DATE,
ENROLLMENT_ID,
PROGRAM_NAME,
INCIDENT_DATE,
ENROLLMENT_DATE,
org.hisp.dhis.rules.models.RuleEnrollment.Status.ACTIVE,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.ORGANISATION_UNIT,
org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.ORGANISATION_UNIT_CODE,
ORGANISATION_UNIT,
ORGANISATION_UNIT_CODE,
listOf(org.hisp.dhis.rules.models.RuleAttributeValue("test_attribute", "test_value"))
)

private fun getRuleEngine(rules: List<Rule>): RuleEngine {
return RuleEngine(RuleEngineContext(rules, listOf(), emptyMap(), emptyMap()),
return RuleEngine(
RuleEngineContext(rules, listOf(), emptyMap(), emptyMap()),
emptyList(), null, TriggerEnvironment.SERVER)
}

companion object {
private val CURRENT_DATE = LocalDate.Companion.currentDate()
private const val DUE_DATE_STRING = "2020-06-01"
private val DUE_DATE = LocalDate.parse(org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.DUE_DATE_STRING)
private val DUE_DATE = LocalDate.parse(DUE_DATE_STRING)
private const val ENROLLMENT_DATE_STRING = "2019-01-01"
private val ENROLLMENT_DATE = LocalDate.parse(org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.ENROLLMENT_DATE_STRING)
private val ENROLLMENT_DATE = LocalDate.parse(ENROLLMENT_DATE_STRING)
private const val EVENT_DATE_STRING = "2019-02-02"
private val EVENT_DATE = LocalDate.parse(org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.EVENT_DATE_STRING)
private val EVENT_DATE = LocalDate.parse(EVENT_DATE_STRING)
private const val INCIDENT_DATE_STRING = "2020-01-01"
private val INCIDENT_DATE = LocalDate.parse(org.hisp.dhis.rules.ProgramRuleVariableTest.Companion.INCIDENT_DATE_STRING)
private val INCIDENT_DATE = LocalDate.parse(INCIDENT_DATE_STRING)
private const val PROGRAM_STAGE = "program stage"
private const val PROGRAM_STAGE_NAME = "program stage name"
private val RULE_EVENT_STATUS = RuleEvent.Status.ACTIVE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2628,6 +2628,6 @@ class RuleEngineFunctionTest {
companion object {
private const val DATE_PATTERN = "yyyy-MM-dd"
private const val USE_CODE_FOR_OPTION_SET = true
private const val USE_NAME_FOR_OPTION_SET = !org.hisp.dhis.rules.RuleEngineFunctionTest.Companion.USE_CODE_FOR_OPTION_SET
private const val USE_NAME_FOR_OPTION_SET = !USE_CODE_FOR_OPTION_SET
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,47 @@ import kotlin.test.*
* @author Zubair Asghar
*/
class RuleEngineGetDescriptionTest {
private var itemStore: MutableMap<String, org.hisp.dhis.rules.DataItem> = HashMap()
private var itemStore: MutableMap<String, DataItem> = HashMap()
private val ruleAction: org.hisp.dhis.rules.models.RuleAction = RuleActionText.createForFeedback(
RuleActionText.Type.DISPLAYTEXT, "", "")
@BeforeTest
fun setUp() {
itemStore = HashMap()
val var_1 = org.hisp.dhis.rules.DataItem(
org.hisp.dhis.rules.RuleEngineGetDescriptionTest.Companion.test_var_one,
org.hisp.dhis.rules.ItemValueType.TEXT
val var_1 = DataItem(
test_var_one,
ItemValueType.TEXT
)
val var_2 = org.hisp.dhis.rules.DataItem(
org.hisp.dhis.rules.RuleEngineGetDescriptionTest.Companion.test_var_two,
org.hisp.dhis.rules.ItemValueType.TEXT
val var_2 = DataItem(
test_var_two,
ItemValueType.TEXT
)
val var_8 = org.hisp.dhis.rules.DataItem(
org.hisp.dhis.rules.RuleEngineGetDescriptionTest.Companion.test_var_three,
org.hisp.dhis.rules.ItemValueType.TEXT
val var_8 = DataItem(
test_var_three,
ItemValueType.TEXT
)
val var_3 = org.hisp.dhis.rules.DataItem(
org.hisp.dhis.rules.RuleEngineGetDescriptionTest.Companion.test_var_date_one,
org.hisp.dhis.rules.ItemValueType.DATE
val var_3 = DataItem(
test_var_date_one,
ItemValueType.DATE
)
val var_4 = org.hisp.dhis.rules.DataItem(
org.hisp.dhis.rules.RuleEngineGetDescriptionTest.Companion.test_var_date_two,
org.hisp.dhis.rules.ItemValueType.DATE
val var_4 = DataItem(
test_var_date_two,
ItemValueType.DATE
)
val var_5 = org.hisp.dhis.rules.DataItem(
org.hisp.dhis.rules.RuleEngineGetDescriptionTest.Companion.completionDate,
org.hisp.dhis.rules.ItemValueType.DATE
val var_5 = DataItem(
completionDate,
ItemValueType.DATE
)
val var_6 = org.hisp.dhis.rules.DataItem(
org.hisp.dhis.rules.RuleEngineGetDescriptionTest.Companion.constant,
org.hisp.dhis.rules.ItemValueType.TEXT
val var_6 = DataItem(
constant,
ItemValueType.TEXT
)
val var_7 = org.hisp.dhis.rules.DataItem(
org.hisp.dhis.rules.RuleEngineGetDescriptionTest.Companion.currentDate,
org.hisp.dhis.rules.ItemValueType.DATE
val var_7 = DataItem(
currentDate,
ItemValueType.DATE
)
val var_9 = org.hisp.dhis.rules.DataItem(
org.hisp.dhis.rules.RuleEngineGetDescriptionTest.Companion.test_var_number,
org.hisp.dhis.rules.ItemValueType.NUMBER
val var_9 = DataItem(
test_var_number,
ItemValueType.NUMBER
)
itemStore["test_var_one"] = var_1
itemStore["test_var_two"] = var_2
Expand Down Expand Up @@ -293,9 +293,11 @@ class RuleEngineGetDescriptionTest {
assertTrue(result.exception is org.hisp.dhis.rules.models.RuleEngineValidationException)
}

private fun getRuleEngineForDescription(itemStore: Map<String, org.hisp.dhis.rules.DataItem>): RuleEngine {
return RuleEngine(RuleEngineContext(emptyList(), emptyList(), emptyMap(),
emptyMap(), RuleEngineIntent.DESCRIPTION, itemStore))
private fun getRuleEngineForDescription(itemStore: Map<String, DataItem>): RuleEngine {
return RuleEngine(
RuleEngineContext(emptyList(), emptyList(), emptyMap(),
emptyMap(), RuleEngineIntent.DESCRIPTION, itemStore)
)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package org.hisp.dhis.rules

import io.mockk.mockk
import kotlinx.datetime.LocalDate
import org.hisp.dhis.rules.models.Rule
import org.hisp.dhis.rules.models.RuleEvent
import org.hisp.dhis.rules.models.RuleVariable
import kotlin.test.Test
import kotlin.test.assertFailsWith

internal class RuleEngineTest {

private val ruleEngineContext: RuleEngineContext = RuleEngineContext(
listOf(Rule("true", listOf())),listOf(mockk<RuleVariable>()))
listOf(Rule("true", listOf())),listOf())

@Test(expected = IllegalStateException::class)
@Test
fun evaluateShouldThrowIfEventIsAlreadyInContext() {
val ruleEvent = RuleEvent(
"test_event",
Expand All @@ -29,6 +28,6 @@ internal class RuleEngineTest {
val ruleEvents: MutableList<RuleEvent> = ArrayList()
ruleEvents.add(ruleEvent)
val ruleEngine = RuleEngine(ruleEngineContext, ruleEvents)
ruleEngine.evaluate(ruleEvent)
assertFailsWith(IllegalStateException::class) { ruleEngine.evaluate(ruleEvent) }
}
}
Loading

0 comments on commit dbc11b0

Please sign in to comment.