Skip to content

Commit

Permalink
Pack repeated groups recursively (google#2675)
Browse files Browse the repository at this point in the history
* Fix packing repeated groups

* Add test to pack repeated groups recursively
  • Loading branch information
jingtang10 authored Nov 7, 2024
1 parent e7c8c33 commit 11ed6f0
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,8 @@ private fun List<QuestionnaireResponse.QuestionnaireResponseItemComponent>.packR

val questionnaireItem = questionnaireItems.single()

questionnaireResponseItems.forEach { it ->
if (questionnaireItem.type == Questionnaire.QuestionnaireItemType.GROUP) {
if (questionnaireItem.repeats) {
it.answer.forEach { it.item = it.item.packRepeatedGroups(questionnaireItem.item) }
} else {
it.item = it.item.packRepeatedGroups(questionnaireItem.item)
}
} else {
it.answer.forEach { it.item = it.item.packRepeatedGroups(questionnaireItem.item) }
}
questionnaireResponseItems.forEach {
it.item = it.item.packRepeatedGroups(questionnaireItem.item)
}

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,121 @@ class MoreQuestionnaireResponsesTest {
assertResourceEquals(questionnaireResponse, packedQuestionnaireResponse)
}

@Test
fun `should pack repeated groups recursively`() {
val questionnaire =
Questionnaire().apply {
addItem(
QuestionnaireItemComponent().apply {
linkId = "repeated-group"
type = Questionnaire.QuestionnaireItemType.GROUP
repeats = true
addItem(
QuestionnaireItemComponent().apply {
linkId = "nested-repeated-group"
type = Questionnaire.QuestionnaireItemType.GROUP
repeats = true
addItem(
QuestionnaireItemComponent().apply {
linkId = "nested-nested-question"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
},
)
},
)
},
)
}

val questionnaireResponse =
QuestionnaireResponse().apply {
addItem(
QuestionnaireResponseItemComponent().apply {
linkId = "repeated-group"
addItem(
QuestionnaireResponseItemComponent().apply {
linkId = "nested-repeated-group"
addItem(
QuestionnaireResponseItemComponent().apply {
linkId = "nested-nested-question"
addAnswer(
QuestionnaireResponseItemAnswerComponent().apply {
value = BooleanType(true)
},
)
},
)
},
)
addItem(
QuestionnaireResponseItemComponent().apply {
linkId = "nested-repeated-group"
addItem(
QuestionnaireResponseItemComponent().apply {
linkId = "nested-nested-question"
addAnswer(
QuestionnaireResponseItemAnswerComponent().apply {
value = BooleanType(false)
},
)
},
)
},
)
},
)
}

val packedQuestionnaireResponse =
QuestionnaireResponse().apply {
addItem(
QuestionnaireResponseItemComponent().apply {
linkId = "repeated-group"
addAnswer(
QuestionnaireResponseItemAnswerComponent().apply {
addItem(
QuestionnaireResponseItemComponent().apply {
linkId = "nested-repeated-group"
addAnswer(
QuestionnaireResponseItemAnswerComponent().apply {
addItem(
QuestionnaireResponseItemComponent().apply {
linkId = "nested-nested-question"
addAnswer(
QuestionnaireResponseItemAnswerComponent().apply {
value = BooleanType(true)
},
)
},
)
},
)
addAnswer(
QuestionnaireResponseItemAnswerComponent().apply {
addItem(
QuestionnaireResponseItemComponent().apply {
linkId = "nested-nested-question"
addAnswer(
QuestionnaireResponseItemAnswerComponent().apply {
value = BooleanType(false)
},
)
},
)
},
)
},
)
},
)
},
)
}

questionnaireResponse.packRepeatedGroups(questionnaire)
assertResourceEquals(questionnaireResponse, packedQuestionnaireResponse)
}

@Test
fun `should not modify non-repeated groups while packing repeated groups`() {
val questionnaire =
Expand Down

0 comments on commit 11ed6f0

Please sign in to comment.