Skip to content

Commit

Permalink
Add tests and remove checks at individual widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
chungwwei committed Feb 7, 2025
1 parent 33232fb commit 38e0b03
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,13 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
item: QuestionnaireItemComponent,
questionnaireItemToParentMap: ItemToParentMap,
) {
CheckMinAndMaxExtensionValues(item.minValue, item.maxValue)
checkMinAndMaxExtensionValues(item.minValue, item.maxValue)
for (child in item.item) {
CheckMinAndMaxExtensionValues(child.minValue, child.maxValue)
checkMinAndMaxExtensionValues(child.minValue, child.maxValue)
questionnaireItemToParentMap[child] = item
buildParentList(child, questionnaireItemToParentMap)
}
}

questionnaireItemParentMap = buildMap {
for (item in questionnaire.item) {
buildParentList(item, this)
Expand Down Expand Up @@ -492,7 +491,6 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
}
}


/** Clears all the answers from the questionnaire response by iterating through each item. */
fun clearAllAnswers() {
questionnaireResponse.allItems.forEach { it.answer = emptyList() }
Expand Down Expand Up @@ -1150,7 +1148,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
}
}

private fun CheckMinAndMaxExtensionValues(minValue: Type?, maxValue: Type?) {
private fun checkMinAndMaxExtensionValues(minValue: Type?, maxValue: Type?) {
if (minValue == null || maxValue == null) {
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Google LLC
* Copyright 2022-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -169,10 +169,6 @@ internal object DatePickerViewHolderFactory :
val min = (questionnaireViewItem.minAnswerValue as? DateType)?.value?.time
val max = (questionnaireViewItem.maxAnswerValue as? DateType)?.value?.time

if (min != null && max != null && min > max) {
throw IllegalArgumentException("minValue cannot be greater than maxValue")
}

val listValidators = ArrayList<DateValidator>()
min?.let { listValidators.add(DateValidatorPointForward.from(it)) }
max?.let { listValidators.add(DateValidatorPointBackward.before(it)) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Google LLC
* Copyright 2022-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,9 +58,6 @@ internal object SliderViewHolderFactory : QuestionnaireItemViewHolderFactory(R.l
val answer = questionnaireViewItem.answers.singleOrNull()
val minValue = getMinValue(questionnaireViewItem.minAnswerValue)
val maxValue = getMaxValue(questionnaireViewItem.maxAnswerValue)
if (minValue >= maxValue) {
throw IllegalStateException("minValue $minValue must be smaller than maxValue $maxValue")
}

with(slider) {
clearOnChangeListeners()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Google LLC
* Copyright 2023-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -86,7 +86,9 @@ import org.hl7.fhir.r4.model.BooleanType
import org.hl7.fhir.r4.model.CodeType
import org.hl7.fhir.r4.model.CodeableConcept
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.DateTimeType
import org.hl7.fhir.r4.model.DateType
import org.hl7.fhir.r4.model.DecimalType
import org.hl7.fhir.r4.model.Enumerations
import org.hl7.fhir.r4.model.Expression
import org.hl7.fhir.r4.model.Extension
Expand Down Expand Up @@ -228,6 +230,94 @@ class QuestionnaireViewModelTest {
}
}

@Test
fun `should throw exception if minValue is greater than maxValue for integer type`() {
val questionnaire =
Questionnaire().apply {
addItem().apply {
type = Questionnaire.QuestionnaireItemType.INTEGER
addExtension().apply {
url = MIN_VALUE_EXTENSION_URL
setValue(IntegerType(10))
}
addExtension().apply {
url = MAX_VALUE_EXTENSION_URL
setValue(IntegerType(1))
}
}
}
val errorMessage =
assertFailsWith<IllegalArgumentException> { createQuestionnaireViewModel(questionnaire) }
.localizedMessage
assertThat(errorMessage).isEqualTo("minValue cannot be greater than maxValue")
}

@Test
fun `should throw exception if minValue is greater than maxValue for decimal type`() {
val questionnaire =
Questionnaire().apply {
addItem().apply {
type = Questionnaire.QuestionnaireItemType.INTEGER
addExtension().apply {
url = MIN_VALUE_EXTENSION_URL
setValue(DecimalType(10.0))
}
addExtension().apply {
url = MAX_VALUE_EXTENSION_URL
setValue(DecimalType(1.5))
}
}
}
val errorMessage =
assertFailsWith<IllegalArgumentException> { createQuestionnaireViewModel(questionnaire) }
.localizedMessage
assertThat(errorMessage).isEqualTo("minValue cannot be greater than maxValue")
}

@Test
fun `should throw exception if minValue is greater than maxValue for datetime type`() {
val questionnaire =
Questionnaire().apply {
addItem().apply {
type = Questionnaire.QuestionnaireItemType.DATETIME
addExtension().apply {
url = MIN_VALUE_EXTENSION_URL
setValue(DateTimeType("2020-01-01T00:00:00Z"))
}
addExtension().apply {
url = MAX_VALUE_EXTENSION_URL
setValue(DateTimeType("2019-01-01T00:00:00Z"))
}
}
}
val errorMessage =
assertFailsWith<IllegalArgumentException> { createQuestionnaireViewModel(questionnaire) }
.localizedMessage
assertThat(errorMessage).isEqualTo("minValue cannot be greater than maxValue")
}

@Test
fun `should throw exception if minValue is greater than maxValue for date type`() {
val questionnaire =
Questionnaire().apply {
addItem().apply {
type = Questionnaire.QuestionnaireItemType.DATE
addExtension().apply {
url = MIN_VALUE_EXTENSION_URL
setValue(DateType("2020-01-01"))
}
addExtension().apply {
url = MAX_VALUE_EXTENSION_URL
setValue(DateType("2019-01-01"))
}
}
}
val errorMessage =
assertFailsWith<IllegalArgumentException> { createQuestionnaireViewModel(questionnaire) }
.localizedMessage
assertThat(errorMessage).isEqualTo("minValue cannot be greater than maxValue")
}

@Test
fun `should copy nested questions if no response is provided`() {
val questionnaire =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Google LLC
* Copyright 2023-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,6 @@ import com.google.android.fhir.datacapture.views.QuestionTextConfiguration
import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
import com.google.android.material.slider.Slider
import com.google.common.truth.Truth.assertThat
import kotlin.test.assertFailsWith
import org.hl7.fhir.r4.model.CodeableConcept
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Extension
Expand Down Expand Up @@ -185,29 +184,6 @@ class SliderViewHolderFactoryTest {
assertThat(viewHolder.itemView.findViewById<Slider>(R.id.slider).valueFrom).isEqualTo(0.0F)
}

@Test
fun `throws exception if minValue is greater than maxvalue`() {
assertFailsWith<IllegalStateException> {
viewHolder.bind(
QuestionnaireViewItem(
Questionnaire.QuestionnaireItemComponent().apply {
addExtension().apply {
url = "http://hl7.org/fhir/StructureDefinition/minValue"
setValue(IntegerType("100"))
}
addExtension().apply {
url = "http://hl7.org/fhir/StructureDefinition/maxValue"
setValue(IntegerType("50"))
}
},
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _, _ -> },
),
)
}
}

@Test
fun shouldSetQuestionnaireResponseSliderAnswer() {
var answerHolder: List<QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent>? = null
Expand Down

0 comments on commit 38e0b03

Please sign in to comment.