Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -121,6 +121,34 @@ internal fun StringType.toIdType(): IdType {
return IdType(value)
}

/**
* Checks if two Coding objects match.
*
* The matching logic is progressive:
* 1. Always matches on the [code].
* 2. Matches on [system] if the both coding has a system.
* 3. Matches on [version] if the both coding has a version.
*/
internal fun Coding.matches(other: Coding): Boolean {
// Always match on code
if (this.code != other.code) {
return false
}

// If system exists in both, it must match
if (other.hasSystem() && this.hasSystem() && this.system != other.system) {
return false
}

// If version exists in both, it must match
if (other.hasVersion() && this.hasVersion() && this.version != other.version) {
return false
}

// All conditions met
return true
}

/** Converts Coding to CodeType. */
internal fun Coding.toCodeType(): CodeType {
return CodeType(code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.google.android.fhir.datacapture.extensions.filterByCodeInNameExtensio
import com.google.android.fhir.datacapture.extensions.initialExpression
import com.google.android.fhir.datacapture.extensions.initialSelected
import com.google.android.fhir.datacapture.extensions.logicalId
import com.google.android.fhir.datacapture.extensions.matches
import com.google.android.fhir.datacapture.extensions.questionnaireLaunchContexts
import com.google.android.fhir.datacapture.extensions.targetStructureMap
import com.google.android.fhir.datacapture.extensions.toCodeType
Expand Down Expand Up @@ -291,7 +292,13 @@ object ResourceMapper {
if (questionnaireItem.answerOption.isNotEmpty()) {
questionnaireItem.answerOption.forEach { answerOption ->
answerOption.initialSelected =
evaluatedExpressionResult.any { answerOption.value.equalsDeep(it) }
evaluatedExpressionResult.any { evaluatedItem ->
if (answerOption.value is Coding && evaluatedItem is Coding) {
(answerOption.value as Coding).matches(evaluatedItem)
} else {
answerOption.value.equalsDeep(evaluatedItem)
}
}
}
} else {
questionnaireItem.initial =
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 @@ -280,4 +280,92 @@ class MoreTypesTest {
val quantity = Quantity(20L)
assertThat(quantity.getValueAsString(context)).isEqualTo("20")
}

@Test
fun codingMatches_sameSystemAndCode_shouldReturnTrue() {
val left = Coding("system", "code", "display")
val right = Coding("system", "code", "display")

assertThat(left.matches(right)).isTrue()
}

@Test
fun codingMatches_differentDisplays_shouldReturnTrue() {
val left = Coding("system", "code", "display")
val right = Coding("system", "code", "other display")

assertThat(left.matches(right)).isTrue()
}

@Test
fun codingMatches_differentCodes_shouldReturnFalse() {
val left = Coding("system", "code", "display")
val right = Coding("system", "other-code", "display")

assertThat(left.matches(right)).isFalse()
}

@Test
fun codingMatches_differentSystems_shouldReturnFalse() {
val left = Coding("system", "code", "display")
val right = Coding("other-system", "code", "display")

assertThat(left.matches(right)).isFalse()
}

@Test
fun codingMatches_sameVersion_shouldReturnTrue() {
val left = Coding("system", "code", "display").apply { version = "1" }
val right = Coding("system", "code", "display").apply { version = "1" }

assertThat(left.matches(right)).isTrue()
}

@Test
fun codingMatches_differentVersions_shouldReturnFalse() {
val left = Coding("system", "code", "display").apply { version = "1" }
val right = Coding("system", "code", "display").apply { version = "2" }

assertThat(left.matches(right)).isFalse()
}

@Test
fun codingMatches_missingSystemOnOneSide_shouldReturnTrue() {
val left = Coding(null, "code", "display")
val right = Coding("system", "code", "display")

assertThat(left.matches(right)).isTrue()
}

@Test
fun codingMatches_missingVersionOnOneSide_shouldReturnTrue() {
val left = Coding("system", "code", "display").apply { version = "1" }
val right = Coding("system", "code", "display")

assertThat(left.matches(right)).isTrue()
}

@Test
fun codingMatches_missingDisplayOnOneSide_shouldReturnTrue() {
val left = Coding("system", "code", null)
val right = Coding("system", "code", "display")

assertThat(left.matches(right)).isTrue()
}

@Test
fun codingMatches_bothMissingSystem_shouldReturnTrue() {
val left = Coding(null, "code", "display")
val right = Coding(null, "code", "display")

assertThat(left.matches(right)).isTrue()
}

@Test
fun codingMatches_bothMissingDisplay_shouldReturnTrue() {
val left = Coding("system", "code", null)
val right = Coding("system", "code", null)

assertThat(left.matches(right)).isTrue()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3144,6 +3144,175 @@ class ResourceMapperTest {
}
}

@Test
fun `populate() should select coding answerOption when only display differs`() = runBlocking {
val matchingAnswerOption =
Coding().apply {
system = "http://loinc.org"
code = "12345-6"
version = "1.0"
display = "Answer option display"
}
val questionnaire =
createObservationChoiceQuestionnaire(
matchingAnswerOption,
Coding().apply {
system = "http://loinc.org"
code = "65432-1"
display = "Fallback display"
},
)

val observationCoding =
Coding().apply {
system = matchingAnswerOption.system
code = matchingAnswerOption.code
version = matchingAnswerOption.version
display = "Observation display"
}

val questionnaireResponse =
ResourceMapper.populate(
questionnaire,
mapOf("observation" to observationWithCoding(observationCoding)),
)

val question = questionnaire.item.single()
val selectedOption =
question.answerOption.first { (it.value as Coding).code == matchingAnswerOption.code }

assertThat(selectedOption.initialSelected).isTrue()
assertThat(question.answerOption.count { it.initialSelected }).isEqualTo(1)
Comment on lines +3184 to +3185
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just testing the createObservationChoiceQuestionnaire function no? i don't think this is necessary.

the important thing in the test case is below: the generated questionnaire response from the population process includes selected answers.

=====

actually, this reveals a problem in the populate function (thanks laz for pointing out) i dont' think it should change the questionnaire... can you fix that?


val responseItem = questionnaireResponse.item.single()
assertThat(responseItem.hasAnswer()).isTrue()
val answerCoding = responseItem.answer.single().value as Coding
assertThat(answerCoding.code).isEqualTo(matchingAnswerOption.code)
assertThat(answerCoding.system).isEqualTo(matchingAnswerOption.system)
assertThat(answerCoding.version).isEqualTo(matchingAnswerOption.version)
assertThat(answerCoding.display).isEqualTo(matchingAnswerOption.display)
}

@Test
fun `populate() should not select coding answerOption when codes differ`() = runBlocking {
val matchingAnswerOption =
Coding().apply {
system = "http://loinc.org"
code = "12345-6"
version = "1.0"
display = "Answer option display"
}
val questionnaire =
createObservationChoiceQuestionnaire(
matchingAnswerOption,
Coding().apply {
system = "http://loinc.org"
code = "65432-1"
display = "Fallback display"
},
)

val questionnaireResponse =
ResourceMapper.populate(
questionnaire,
mapOf(
"observation" to
observationWithCoding(
Coding().apply {
system = matchingAnswerOption.system
code = "different-code"
version = matchingAnswerOption.version
display = "Observation display"
},
),
),
)

val question = questionnaire.item.single()
assertThat(question.answerOption.none { it.initialSelected }).isTrue()
assertThat(questionnaireResponse.item.single().hasAnswer()).isFalse()
}

@Test
fun `populate() should not select coding answerOption when systems differ`() = runBlocking {
val matchingAnswerOption =
Coding().apply {
system = "http://loinc.org"
code = "12345-6"
version = "1.0"
display = "Answer option display"
}
val questionnaire =
createObservationChoiceQuestionnaire(
matchingAnswerOption,
Coding().apply {
system = "http://loinc.org"
code = "65432-1"
display = "Fallback display"
},
)

val questionnaireResponse =
ResourceMapper.populate(
questionnaire,
mapOf(
"observation" to
observationWithCoding(
Coding().apply {
system = "http://snomed.info/sct"
code = matchingAnswerOption.code
version = matchingAnswerOption.version
display = "Observation display"
},
),
),
)

val question = questionnaire.item.single()
assertThat(question.answerOption.none { it.initialSelected }).isTrue()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line the same

assertThat(questionnaireResponse.item.single().hasAnswer()).isFalse()
}

@Test
fun `populate() should not select coding answerOption when versions differ`() = runBlocking {
val matchingAnswerOption =
Coding().apply {
system = "http://loinc.org"
code = "12345-6"
version = "1.0"
display = "Answer option display"
}
val questionnaire =
createObservationChoiceQuestionnaire(
matchingAnswerOption,
Coding().apply {
system = "http://loinc.org"
code = "65432-1"
display = "Fallback display"
},
)

val questionnaireResponse =
ResourceMapper.populate(
questionnaire,
mapOf(
"observation" to
observationWithCoding(
Coding().apply {
system = matchingAnswerOption.system
code = matchingAnswerOption.code
version = "2.0"
display = "Observation display"
},
),
),
)

val question = questionnaire.item.single()
assertThat(question.answerOption.none { it.initialSelected }).isTrue()
assertThat(questionnaireResponse.item.single().hasAnswer()).isFalse()
}

@Test
fun `populate() should select single answer for non repeating question with answerOption`() =
runBlocking {
Expand Down Expand Up @@ -3596,6 +3765,54 @@ class ResourceMapperTest {
assertThat(patient.name.first().family).isEqualTo("John Doe")
}

private fun createObservationChoiceQuestionnaire(
vararg answerOptions: Coding,
): Questionnaire =
Questionnaire()
.apply {
addExtension().apply {
url = EXTENSION_SDC_QUESTIONNAIRE_LAUNCH_CONTEXT
extension =
listOf(
Extension(
"name",
Coding(
CODE_SYSTEM_LAUNCH_CONTEXT,
"observation",
"Test Observation",
),
),
Extension("type", CodeType("Observation")),
)
}
}
.addItem(
Questionnaire.QuestionnaireItemComponent().apply {
linkId = "observation-choice"
type = Questionnaire.QuestionnaireItemType.CHOICE
extension =
listOf(
Extension(
ITEM_INITIAL_EXPRESSION_URL,
Expression().apply {
language = "text/fhirpath"
expression = "%observation.value.coding"
},
),
)
answerOption =
answerOptions
.map { coding -> Questionnaire.QuestionnaireItemAnswerOptionComponent(coding) }
.toMutableList()
},
)

private fun observationWithCoding(coding: Coding): Observation =
Observation().apply {
status = Observation.ObservationStatus.FINAL
value = CodeableConcept().apply { this.coding = mutableListOf(coding) }
}

private fun readFileFromResourcesAsString(filename: String) =
readFileFromResources(filename).bufferedReader().use { it.readText() }

Expand Down