Skip to content

Commit

Permalink
Merge branch 'master' into optimize-db-index
Browse files Browse the repository at this point in the history
  • Loading branch information
dubdabasoduba authored Nov 8, 2024
2 parents 569a24f + 11ed6f0 commit 77fde5c
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 77fde5c

Please sign in to comment.