Skip to content

Commit

Permalink
Pack QuestionnaireResponse repeatedGroup items
Browse files Browse the repository at this point in the history
before validation
borrows from sdk: google/android-fhir#1936
  • Loading branch information
LZRS committed May 15, 2024
1 parent 79b564e commit 6bdd6c6
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,31 @@ private fun List<QuestionnaireResponseItemComponent>.clearText() {
}
}
}

/** Borrows from: https://github.com/google/android-fhir/pull/1936 */
fun QuestionnaireResponse.packRepeatedGroups() {
item = item.packRepeatedGroups()

Check warning on line 39 in android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/QuestionnaireResponseExtension.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/QuestionnaireResponseExtension.kt#L39

Added line #L39 was not covered by tests
}

private fun List<QuestionnaireResponse.QuestionnaireResponseItemComponent>.packRepeatedGroups():
List<QuestionnaireResponse.QuestionnaireResponseItemComponent> {
forEach { it ->
it.item = it.item.packRepeatedGroups()
it.answer.forEach { it.item = it.item.packRepeatedGroups() }

Check warning on line 46 in android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/QuestionnaireResponseExtension.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/QuestionnaireResponseExtension.kt#L44-L46

Added lines #L44 - L46 were not covered by tests
}
val linkIdToPackedResponseItems =
groupBy { it.linkId }
.mapValues { (linkId, questionnaireResponseItems) ->

Check warning on line 50 in android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/QuestionnaireResponseExtension.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/QuestionnaireResponseExtension.kt#L48-L50

Added lines #L48 - L50 were not covered by tests
questionnaireResponseItems.singleOrNull()
?: QuestionnaireResponse.QuestionnaireResponseItemComponent().apply {
this.linkId = linkId
answer =
questionnaireResponseItems.map {
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply {
item = it.item

Check warning on line 57 in android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/QuestionnaireResponseExtension.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/QuestionnaireResponseExtension.kt#L52-L57

Added lines #L52 - L57 were not covered by tests
}
}
}
}
return map { it.linkId }.distinct().map { linkIdToPackedResponseItems[it]!! }

Check warning on line 62 in android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/QuestionnaireResponseExtension.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/QuestionnaireResponseExtension.kt#L62

Added line #L62 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid
import org.smartregister.fhircore.engine.util.extension.find
import org.smartregister.fhircore.engine.util.extension.generateMissingId
import org.smartregister.fhircore.engine.util.extension.isIn
import org.smartregister.fhircore.engine.util.extension.packRepeatedGroups
import org.smartregister.fhircore.engine.util.extension.prePopulateInitialValues
import org.smartregister.fhircore.engine.util.extension.prepareQuestionsForEditing
import org.smartregister.fhircore.engine.util.extension.prepareQuestionsForReadingOrEditing
Expand Down Expand Up @@ -675,7 +676,10 @@ constructor(
return QuestionnaireResponseValidator.validateQuestionnaireResponse(
questionnaire = Questionnaire().apply { item = validQuestionnaireItems },
questionnaireResponse =
QuestionnaireResponse().apply { item = validQuestionnaireResponseItems },
QuestionnaireResponse().apply {
item = validQuestionnaireResponseItems
packRepeatedGroups()
},

Check warning on line 682 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L679-L682

Added lines #L679 - L682 were not covered by tests
context = context,
)
.values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,226 @@ class QuestionnaireViewModelTest : RobolectricTest() {
}
}

@Test
fun testValidateQuestionnaireResponseWithRepeatsGroup() = runTest {
val questionnaireString =
"""
{
"resourceType": "Questionnaire",
"item": [
{
"linkId": "systolic-bp",
"type": "integer",
"required": true
},
{
"linkId": "blood-pressure-repeating-group",
"type": "group",
"required": false,
"repeats": true,
"item": [
{
"linkId": "systolic-bp",
"type": "integer",
"required": true
}
]
}
]
}
"""
.trimIndent()
val questionnaireResponseString =
"""
{
"resourceType": "QuestionnaireResponse",
"item": [
{
"linkId": "systolic-bp",
"answer": [
{
"valueInteger": 123
}
]
},
{
"linkId": "blood-pressure-repeating-group",
"item": [
{
"linkId": "systolic-bp",
"answer": [
{
"valueInteger": 124
}
]
}
]
},
{
"linkId": "blood-pressure-repeating-group",
"item": [
{
"linkId": "systolic-bp",
"answer": [
{
"valueInteger": 125
}
]
}
]
},
{
"linkId": "blood-pressure-repeating-group",
"item": [
{
"linkId": "systolic-bp",
"answer": [
{
"valueInteger": 126
}
]
}
]
}
]
}
"""
.trimIndent()
val questionnaire = parser.parseResource(questionnaireString) as Questionnaire
val questionnaireResponse =
parser.parseResource(questionnaireResponseString) as QuestionnaireResponse
val result =
questionnaireViewModel.validateQuestionnaireResponse(
questionnaire,
questionnaireResponse,
context,
)
Assert.assertTrue(result)
}

@Test
fun testValidateQuestionnaireResponseWithNestedRepeatsGroup() = runTest {
val questionnaireString =
"""
{
"resourceType": "Questionnaire",
"item": [
{
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/questionnaire-itemControl",
"valueCodeableConcept": {
"coding": [
{
"system": "http://hl7.org/fhir/questionnaire-item-control",
"code": "page",
"display": "Page 1"
}
],
"text": "Page 1"
}
}
],
"linkId": "page-1",
"type": "group",
"item": [
{
"linkId": "systolic-bp",
"type": "integer",
"required": true
},
{
"linkId": "blood-pressure-repeating-group",
"type": "group",
"required": false,
"repeats": true,
"item": [
{
"linkId": "systolic-bp",
"type": "integer",
"required": true
}
]
}
]
}
]
}
"""
.trimIndent()
val questionnaireResponseString =
"""
{
"resourceType": "QuestionnaireResponse",
"item": [
{
"linkId": "page-1",
"item": [
{
"linkId": "systolic-bp",
"answer": [
{
"valueInteger": 123
}
]
},
{
"linkId": "blood-pressure-repeating-group",
"item": [
{
"linkId": "systolic-bp",
"answer": [
{
"valueInteger": 124
}
]
}
]
},
{
"linkId": "blood-pressure-repeating-group",
"item": [
{
"linkId": "systolic-bp",
"answer": [
{
"valueInteger": 125
}
]
}
]
},
{
"linkId": "blood-pressure-repeating-group",
"item": [
{
"linkId": "systolic-bp",
"answer": [
{
"valueInteger": 126
}
]
}
]
}
]
}
]
}
"""
.trimIndent()
val questionnaire = parser.parseResource(questionnaireString) as Questionnaire
val questionnaireResponse =
parser.parseResource(questionnaireResponseString) as QuestionnaireResponse
val result =
questionnaireViewModel.validateQuestionnaireResponse(
questionnaire,
questionnaireResponse,
context,
)
Assert.assertTrue(result)
}

@Test
fun testExecuteCqlShouldInvokeRunCqlLibrary() = runTest {
val bundle =
Expand Down

0 comments on commit 6bdd6c6

Please sign in to comment.