From 3e64f5e715b138df4978de60706827dd32637158 Mon Sep 17 00:00:00 2001 From: Pablo Pajuelo Cabezas Date: Tue, 26 Nov 2024 10:18:09 +0100 Subject: [PATCH] fix unit tests --- .../form/data/EnrollmentRepositoryTest.kt | 3 + .../org/dhis2/form/data/FieldUiModelTest.kt | 92 ------------------- .../data/FormRepositoryIntegrationTest.kt | 3 + .../dhis2/form/data/GeometryControllerTest.kt | 35 ------- .../form/integration/ProgramRulesTest.kt | 77 +++++++--------- .../model/OptionSetDialogViewModelTest.kt | 22 ++--- .../dhis2/form/ui/DataEntryIntegrationTest.kt | 48 ++++++---- .../form/ui/event/UiEventFactoryImplTest.kt | 74 --------------- 8 files changed, 74 insertions(+), 280 deletions(-) delete mode 100644 form/src/test/java/org/dhis2/form/data/FieldUiModelTest.kt delete mode 100644 form/src/test/java/org/dhis2/form/ui/event/UiEventFactoryImplTest.kt diff --git a/form/src/test/java/org/dhis2/form/data/EnrollmentRepositoryTest.kt b/form/src/test/java/org/dhis2/form/data/EnrollmentRepositoryTest.kt index 16a79d78d9d..5ca3bb5c20c 100644 --- a/form/src/test/java/org/dhis2/form/data/EnrollmentRepositoryTest.kt +++ b/form/src/test/java/org/dhis2/form/data/EnrollmentRepositoryTest.kt @@ -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 @@ -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() @@ -56,6 +58,7 @@ class EnrollmentRepositoryTest { conf, enrollmentMode, enrolmentFormLabelsProvider, + metadataIconProvider, ) } diff --git a/form/src/test/java/org/dhis2/form/data/FieldUiModelTest.kt b/form/src/test/java/org/dhis2/form/data/FieldUiModelTest.kt deleted file mode 100644 index 1cb9a1c1789..00000000000 --- a/form/src/test/java/org/dhis2/form/data/FieldUiModelTest.kt +++ /dev/null @@ -1,92 +0,0 @@ -package org.dhis2.form.data - -import org.dhis2.form.model.FieldUiModelImpl -import org.dhis2.form.model.OptionSetConfiguration -import org.hisp.dhis.android.core.common.ValueType -import org.hisp.dhis.android.core.option.Option -import org.junit.Test - -class FieldUiModelTest { - - @Test - fun `should set optionsToDisplay when there is optionsToHide`() { - val optionsToHide = listOf("1", "2") - val matrixOptionSetModel = fieldUiModel().also { - val conf = it.optionSetConfiguration - it.optionSetConfiguration = when (conf) { - is OptionSetConfiguration.BigOptionSet -> - conf.copy(optionsToHide = optionsToHide) - is OptionSetConfiguration.DefaultOptionSet -> - conf.copy(optionsToHide = optionsToHide) - null -> conf - } - } - assert( - matrixOptionSetModel.optionSetConfiguration?.optionsToDisplay() - ?.map { it.uid() } == listOf("3", "4", "5"), - ) - } - - @Test - fun `should set optionsToDisplay when there is optionsToShow`() { - val optionsInGroupToShow = listOf("1", "2") - val matrixOptionSetModel = fieldUiModel().also { - val conf = it.optionSetConfiguration - it.optionSetConfiguration = when (conf) { - is OptionSetConfiguration.BigOptionSet -> - conf.copy(optionsToShow = optionsInGroupToShow) - is OptionSetConfiguration.DefaultOptionSet -> - conf.copy(optionsToShow = optionsInGroupToShow) - null -> conf - } - } - assert( - matrixOptionSetModel.optionSetConfiguration?.optionsToDisplay() - ?.map { it.uid() } == listOf("1", "2"), - ) - } - - @Test - fun `should set optionsToDisplay when there are optionsToShow and optionsToHide`() { - val optionsToHide = listOf("1") - val optionsInGroupToShow = listOf("3", "5") - - val matrixOptionSetModel = fieldUiModel().also { - val conf = it.optionSetConfiguration - it.optionSetConfiguration = when (conf) { - is OptionSetConfiguration.BigOptionSet -> conf.copy( - optionsToHide = optionsToHide, - optionsToShow = optionsInGroupToShow, - ) - is OptionSetConfiguration.DefaultOptionSet -> conf.copy( - optionsToHide = optionsToHide, - optionsToShow = optionsInGroupToShow, - ) - null -> conf - } - } - assert( - matrixOptionSetModel.optionSetConfiguration?.optionsToDisplay() - ?.map { it.uid() } == listOf("3", "5"), - ) - } - - private fun fieldUiModel() = FieldUiModelImpl( - "uid", - label = "label", - valueType = ValueType.TEXT, - optionSetConfiguration = OptionSetConfiguration.config(5) { - OptionSetConfiguration.OptionConfigData( - listOf( - Option.builder().uid("1").build(), - Option.builder().uid("2").build(), - Option.builder().uid("3").build(), - Option.builder().uid("4").build(), - Option.builder().uid("5").build(), - ), - emptyMap(), - ) - }, - autocompleteList = null, - ) -} diff --git a/form/src/test/java/org/dhis2/form/data/FormRepositoryIntegrationTest.kt b/form/src/test/java/org/dhis2/form/data/FormRepositoryIntegrationTest.kt index fb2d09ab5c3..dd5d9b4a7a4 100644 --- a/form/src/test/java/org/dhis2/form/data/FormRepositoryIntegrationTest.kt +++ b/form/src/test/java/org/dhis2/form/data/FormRepositoryIntegrationTest.kt @@ -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 @@ -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" @@ -173,6 +175,7 @@ class FormRepositoryIntegrationTest { conf, enrollmentMode, enrollmentFormLabelsProvider, + metadataIconProvider, ) return FormRepositoryImpl( diff --git a/form/src/test/java/org/dhis2/form/data/GeometryControllerTest.kt b/form/src/test/java/org/dhis2/form/data/GeometryControllerTest.kt index 8f326c34258..294f3768281 100644 --- a/form/src/test/java/org/dhis2/form/data/GeometryControllerTest.kt +++ b/form/src/test/java/org/dhis2/form/data/GeometryControllerTest.kt @@ -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 @@ -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) - } } diff --git a/form/src/test/java/org/dhis2/form/integration/ProgramRulesTest.kt b/form/src/test/java/org/dhis2/form/integration/ProgramRulesTest.kt index 1c5d88d8d18..259b6376e37 100644 --- a/form/src/test/java/org/dhis2/form/integration/ProgramRulesTest.kt +++ b/form/src/test/java/org/dhis2/form/integration/ProgramRulesTest.kt @@ -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 @@ -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 @@ -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 { @@ -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, @@ -372,13 +375,21 @@ class ProgramRulesTest { val items = formViewModel.items.value ?: emptyList() - val optionsToDisplay: List