Skip to content

Commit

Permalink
fix: [ANDROAPP-5869] duplicated indicators table (#3713)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmateos authored Jul 4, 2024
1 parent 6242cab commit fc42e48
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.dhis2.data.forms.dataentry.tablefields.spinner.SpinnerViewModel
import org.dhis2.form.model.ValueStoreResult.ERROR_UPDATING_VALUE
import org.dhis2.form.model.ValueStoreResult.VALUE_CHANGED
import org.dhis2.form.model.ValueStoreResult.VALUE_HAS_NOT_CHANGED
import org.dhis2.usescases.datasets.dataSetTable.dataSetSection.TableDataToTableModelMapper.Companion.INDICATORS_TABLE_ID
import org.hisp.dhis.android.core.arch.helpers.Result
import org.hisp.dhis.android.core.common.ValueType
import org.hisp.dhis.android.core.dataelement.DataElement
Expand Down Expand Up @@ -109,10 +110,12 @@ class DataValuePresenter(
val updatedTableModel = mapper(tableData)

val updatedTables = screenState.value.tables.map { tableModel ->
if (tableModel.id == catComboUid) {
updatedTableModel.copy(overwrittenValues = tableModel.overwrittenValues)
} else {
indicatorTables() ?: tableModel
when (tableModel.id) {
catComboUid -> updatedTableModel.copy(
overwrittenValues = tableModel.overwrittenValues,
)
INDICATORS_TABLE_ID -> indicatorTables() ?: tableModel
else -> tableModel
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ class TableDataToTableModelMapper(val mapFieldValueToUser: MapFieldValueToUser)
}

return TableModel(
id = "indicators",
id = INDICATORS_TABLE_ID,
title = mapFieldValueToUser.resources.getString(R.string.dashboard_indicators),
tableHeaderModel = tableHeader,
tableRows = tableRows,
)
}

companion object {
const val INDICATORS_TABLE_ID = "indicators"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.dhis2.data.forms.dataentry.tablefields.spinner.SpinnerViewModel
import org.dhis2.data.schedulers.TrampolineSchedulerProvider
import org.dhis2.form.model.StoreResult
import org.dhis2.form.model.ValueStoreResult
import org.dhis2.usescases.datasets.dataSetTable.dataSetSection.TableDataToTableModelMapper.Companion.INDICATORS_TABLE_ID
import org.hisp.dhis.android.core.category.CategoryCombo
import org.hisp.dhis.android.core.common.ValueType
import org.hisp.dhis.android.core.dataelement.DataElement
Expand Down Expand Up @@ -381,7 +382,7 @@ class DataValuePresenterTest {
}

val mockedIndicatorTableModel = mock<TableModel> {
on { id } doReturn null
on { id } doReturn INDICATORS_TABLE_ID
}

val mockedUpdatedTableModel = mock<TableModel> {
Expand Down Expand Up @@ -426,6 +427,72 @@ class DataValuePresenterTest {
verify(view).onValueProcessed()
}

@Test
fun shouldUpdateValueWhenSavedForTwoTablesAndIndicators() {
val mockedTableCell = mock<TableCell> {
on { id } doReturn "mocked_id"
on { column } doReturn 1
on { row } doReturn 0
on { value } doReturn "valueToSave"
}

val mockedTableModelA = mock<TableModel> {
on { id } doReturn "tableIdA"
on { hasCellWithId(any()) } doReturn true
}

val mockedTableModelB = mock<TableModel> {
on { id } doReturn "tableIdB"
on { hasCellWithId(any()) } doReturn true
}

val mockedIndicatorTableModel = mock<TableModel> {
on { id } doReturn INDICATORS_TABLE_ID
}

val mockedUpdatedTableModel = mock<TableModel> {
on { id } doReturn "tableIdA_updated"
on { hasCellWithId(any()) } doReturn true
}

val mockedUpdatedIndicatorTableModel = mock<TableModel> {
on { id } doReturn "updated_indicator"
}

val tableStateValue = presenter.mutableTableData()
tableStateValue.value = TableScreenState(
listOf(mockedTableModelA, mockedTableModelB, mockedIndicatorTableModel),
)

whenever(valueStore.save(any(), any(), any(), any(), any(), any())) doReturn Flowable.just(
StoreResult(
uid = "id",
valueStoreResult = ValueStoreResult.VALUE_CHANGED,
valueStoreResultMessage = null,
),
)

whenever(dataValueRepository.getDataTableModel(any<String>())) doReturn Observable.just(
mockedDataTableModel,
)
whenever(dataValueRepository.setTableData(any(), any())) doReturn mockedTableData
whenever(mapper.invoke(any())) doReturn mockedUpdatedTableModel
whenever(
mockedUpdatedTableModel.copy(overwrittenValues = mockedTableModel.overwrittenValues),
) doReturn mockedUpdatedTableModel

whenever(dataValueRepository.getDataSetIndicators()) doReturn Single.just(mockedIndicators)
whenever(mapper.map(any())) doReturn mockedUpdatedIndicatorTableModel

presenter.onSaveValueChange(mockedTableCell)

assertTrue(presenter.currentState().value.tables.size == 3)
assertTrue(presenter.currentState().value.tables[0].id == "tableIdA_updated")
assertTrue(presenter.currentState().value.tables[1].id == "tableIdB")
assertTrue(presenter.currentState().value.tables.last().id == "updated_indicator")
verify(view).onValueProcessed()
}

@Test
fun shouldSetErrorValue() {
val mockedTableCell = mock<TableCell> {
Expand Down

0 comments on commit fc42e48

Please sign in to comment.