Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Balcan committed Nov 26, 2024
1 parent 4d6f874 commit 3e64f5e
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 280 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.dhis2.form.data

import junit.framework.TestCase.assertTrue
import org.dhis2.commons.resources.MetadataIconProvider
import org.dhis2.form.data.EnrollmentRepository.Companion.ORG_UNIT_UID
import org.dhis2.form.data.metadata.EnrollmentConfiguration
import org.dhis2.form.model.EnrollmentMode
Expand All @@ -20,6 +21,7 @@ class EnrollmentRepositoryTest {
private val conf: EnrollmentConfiguration = mock()
private val enrollmentMode: EnrollmentMode = mock()
private val enrolmentFormLabelsProvider: EnrollmentFormLabelsProvider = mock()
private val metadataIconProvider: MetadataIconProvider = mock()
lateinit var repository: DataEntryRepository
val programSection: ProgramSection = mock()

Expand Down Expand Up @@ -56,6 +58,7 @@ class EnrollmentRepositoryTest {
conf,
enrollmentMode,
enrolmentFormLabelsProvider,
metadataIconProvider,
)
}

Expand Down
92 changes: 0 additions & 92 deletions form/src/test/java/org/dhis2/form/data/FieldUiModelTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.dhis2.form.data

import org.dhis2.commons.prefs.PreferenceProvider
import org.dhis2.commons.resources.MetadataIconProvider
import org.dhis2.form.data.metadata.EnrollmentConfiguration
import org.dhis2.form.model.EnrollmentMode
import org.dhis2.form.model.SectionUiModelImpl
Expand Down Expand Up @@ -43,6 +44,7 @@ class FormRepositoryIntegrationTest {
on { provideEnrollmentOrgUnitLabel() } doReturn "OrgUnit label"
on { provideEnrollmentDataSectionLabel(any()) } doReturn "Enrollment data"
}
private val metadataIconProvider: MetadataIconProvider = mock()

private val program: Program = mock {
on { uid() } doReturn "programUid"
Expand Down Expand Up @@ -173,6 +175,7 @@ class FormRepositoryIntegrationTest {
conf,
enrollmentMode,
enrollmentFormLabelsProvider,
metadataIconProvider,
)

return FormRepositoryImpl(
Expand Down
35 changes: 0 additions & 35 deletions form/src/test/java/org/dhis2/form/data/GeometryControllerTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.dhis2.form.data

import org.dhis2.form.ui.event.RecyclerViewUiEvents
import org.dhis2.form.ui.intent.FormIntent
import org.hisp.dhis.android.core.common.FeatureType
import org.junit.Assert.assertTrue
import org.junit.Before
Expand Down Expand Up @@ -45,37 +43,4 @@ class GeometryControllerTest {
result.coordinates()?.isNotEmpty() == true,
)
}

@Test
fun `Should return coordinates callback`() {
var currentCallback: Int = -1
val coordinateCallback = controller.getCoordinatesCallback(
{
currentCallback = 0
},
{ currentCallback = 1 },
{ _, _, _ -> currentCallback = 2 },
)

coordinateCallback.intent(
FormIntent.SaveCurrentLocation(
uid = "fieldUid",
value = null,
featureType = "none",
),
)
assertTrue(currentCallback == 0)
coordinateCallback.recyclerViewUiEvents(
RecyclerViewUiEvents.RequestCurrentLocation("fieldUid"),
)
assertTrue(currentCallback == 1)
coordinateCallback.recyclerViewUiEvents(
RecyclerViewUiEvents.RequestLocationByMap(
"fieldUid",
FeatureType.POINT,
null,
),
)
assertTrue(currentCallback == 2)
}
}
77 changes: 31 additions & 46 deletions form/src/test/java/org/dhis2/form/integration/ProgramRulesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.reactivex.Flowable
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
Expand All @@ -31,9 +32,7 @@ import org.dhis2.form.ui.FormViewModel
import org.dhis2.form.ui.intent.FormIntent
import org.dhis2.mobileProgramRules.RuleEngineHelper
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.common.ObjectWithUid
import org.hisp.dhis.android.core.common.ValueType
import org.hisp.dhis.android.core.option.Option
import org.hisp.dhis.android.core.program.ProgramRuleActionType
import org.hisp.dhis.rules.models.RuleAction
import org.hisp.dhis.rules.models.RuleEffect
Expand All @@ -48,6 +47,7 @@ import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

class ProgramRulesTest {
Expand Down Expand Up @@ -106,7 +106,10 @@ class ProgramRulesTest {
invocationOnMock.getArgument(0) as FieldUiModel
}

whenever(formValueStore.save(any(), anyOrNull(), anyOrNull())) doReturn StoreResult("", ValueStoreResult.VALUE_CHANGED)
whenever(formValueStore.save(any(), anyOrNull(), anyOrNull())) doReturn StoreResult(
"",
ValueStoreResult.VALUE_CHANGED,
)

repository = FormRepositoryImpl(
formValueStore = formValueStore,
Expand Down Expand Up @@ -372,13 +375,21 @@ class ProgramRulesTest {

val items = formViewModel.items.value ?: emptyList()

val optionsToDisplay: List<Option> =
items.find { it.uid == "uid006" }!!.optionSetConfiguration!!.optionsToDisplay()

assertTrue(optionsToDisplay.size == 3)
assertTrue(optionsToDisplay[0].uid() == "Option2")
assertTrue(optionsToDisplay[1].uid() == "Option3")
assertTrue(optionsToDisplay[2].uid() == "Option4")
verify(dataEntryRepository).options(
optionSetUid = "optionSetUid",
query = "",
optionsToHide = emptyList(),
optionGroupsToHide = emptyList(),
optionGroupsToShow = listOf("optionGroupId"),
)
/*
val optionsToDisplay: List<Option> =
items.find { it.uid == "uid006" }!!.optionSetConfiguration!!.optionsToDisplay()
assertTrue(optionsToDisplay.size == 3)
assertTrue(optionsToDisplay[0].uid() == "Option2")
assertTrue(optionsToDisplay[1].uid() == "Option3")
assertTrue(optionsToDisplay[2].uid() == "Option4")*/
}

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -414,15 +425,13 @@ class ProgramRulesTest {
formViewModel.submitIntent(intent)
advanceUntilIdle()

val items = formViewModel.items.value ?: emptyList()

val optionsToDisplay: List<Option> =
items.last().optionSetConfiguration!!.optionsToDisplay()

optionsToDisplay.forEach {
assert(it.uid() != "Option2")
}
assert(optionsToDisplay.size == 4)
verify(dataEntryRepository).options(
optionSetUid = "optionSetUid",
query = "",
optionsToHide = listOf("Option2"),
optionGroupsToHide = emptyList(),
optionGroupsToShow = emptyList(),
)
}

private fun provideItemList() = listOf(
Expand Down Expand Up @@ -486,11 +495,8 @@ class ProgramRulesTest {
value = "value06",
label = "field6",
valueType = ValueType.MULTI_TEXT,
optionSetConfiguration = OptionSetConfiguration.DefaultOptionSet(
options = "optionSetUid".listOfOptions(),
optionsToHide = listOf(),
optionsToShow = listOf("Option2", "Option3", "Option4"),
optionMetadataIcon = mapOf(),
optionSetConfiguration = OptionSetConfiguration(
emptyFlow(),
),
autocompleteList = null,
programStageSection = "section2",
Expand All @@ -501,31 +507,10 @@ class ProgramRulesTest {
value = "value07",
label = "field7",
valueType = ValueType.MULTI_TEXT,
optionSetConfiguration = OptionSetConfiguration.DefaultOptionSet(
options = "optionSetUid".listOfOptions(),
optionsToHide = listOf("Option2"),
optionsToShow = listOf(),
optionMetadataIcon = mapOf(),
),
optionSetConfiguration = OptionSetConfiguration(emptyFlow()),
autocompleteList = null,
programStageSection = "section2",
optionSet = "optionSetUid",
),
)

private fun String.listOfOptions(): List<Option> {
val optionSetUid = ObjectWithUid.create(this)
val options: MutableList<Option> = mutableListOf()
repeat(5) { index ->
options.add(
Option.builder()
.uid("Option$index")
.displayName("name$index")
.code("code$index")
.optionSet(optionSetUid)
.build(),
)
}
return options.toList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
Expand Down Expand Up @@ -94,11 +95,8 @@ class OptionSetDialogViewModelTest {
@Test
fun `Should search and filter options to hide`() {
val optionsToHide = listOf("Option1")
whenever(field.optionSetConfiguration) doReturn OptionSetConfiguration.DefaultOptionSet(
options = emptyList(),
optionsToHide = optionsToHide,
optionsToShow = emptyList(),
emptyMap(),
whenever(field.optionSetConfiguration) doReturn OptionSetConfiguration(
emptyFlow(),
)
viewModel.onSearchingOption("test")
testingDispatcher.scheduler.advanceUntilIdle()
Expand All @@ -114,11 +112,8 @@ class OptionSetDialogViewModelTest {
@Test
fun `Should search and filter options to show`() {
val optionsToShow = listOf("Option1")
whenever(field.optionSetConfiguration) doReturn OptionSetConfiguration.DefaultOptionSet(
options = emptyList(),
optionsToHide = emptyList(),
optionsToShow = optionsToShow,
emptyMap(),
whenever(field.optionSetConfiguration) doReturn OptionSetConfiguration(
emptyFlow(),
)
viewModel.onSearchingOption("test")
assertTrue(viewModel.searchValue.value == "test")
Expand All @@ -135,11 +130,8 @@ class OptionSetDialogViewModelTest {
fun `Should search and filter options to show and hide`() {
val optionsToShow = listOf("Option1")
val optionsToHide = listOf("Option1")
whenever(field.optionSetConfiguration) doReturn OptionSetConfiguration.DefaultOptionSet(
options = emptyList(),
optionsToHide = optionsToHide,
optionsToShow = optionsToShow,
emptyMap(),
whenever(field.optionSetConfiguration) doReturn OptionSetConfiguration(
emptyFlow(),
)
viewModel.onSearchingOption("test")
testingDispatcher.scheduler.advanceUntilIdle()
Expand Down
Loading

0 comments on commit 3e64f5e

Please sign in to comment.