Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'system' in evaluation of unique Coding identifierString #2148

Merged
merged 4 commits into from
Sep 5, 2023
Merged
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
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,19 @@ fun Type.displayString(context: Context): String =
fun Type.getValueAsString(context: Context): String =
getValueString(this) ?: context.getString(R.string.not_answered)

/*
/**
* Returns the unique identifier of a [Type]. Used to differentiate between item answer options that
* may have similar display strings
*/
fun Type.identifierString(context: Context): String =
id ?: (this as? Coding)?.code ?: (this as? Reference)?.reference ?: displayString(context)
id
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved
?: when (this) {
is Coding ->
arrayOf("${this.system.orEmpty()}${this.version.orEmpty()}", this.code.orEmpty())
.joinToString(if (this.hasSystem().or(this.hasVersion()) && this.hasCode()) "|" else "")
is Reference -> this.reference ?: displayString(context)
else -> displayString(context)
}

private fun getDisplayString(type: Type, context: Context): String? =
when (type) {
Expand Down
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Google LLC
* Copyright 2022-2023 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 All @@ -16,7 +16,9 @@

package com.google.android.fhir.datacapture.extensions

import android.app.Application
import android.os.Build
import androidx.test.core.app.ApplicationProvider
import ca.uhn.fhir.model.api.TemporalPrecisionEnum
import com.google.common.truth.Truth.assertThat
import java.time.Instant
Expand All @@ -41,6 +43,7 @@ import org.hl7.fhir.r4.model.MarkdownType
import org.hl7.fhir.r4.model.OidType
import org.hl7.fhir.r4.model.PositiveIntType
import org.hl7.fhir.r4.model.Quantity
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.StringType
import org.hl7.fhir.r4.model.TimeType
import org.hl7.fhir.r4.model.Type
Expand All @@ -57,6 +60,8 @@ import org.robolectric.annotation.Config
@Config(sdk = [Build.VERSION_CODES.P])
class MoreTypesTest {

private val context = ApplicationProvider.getApplicationContext<Application>()

@Test
fun instant_shouldReturnExpectedStringValue() {
val value =
Expand Down Expand Up @@ -199,6 +204,62 @@ class MoreTypesTest {
assertThat(code.equalsDeep(CodeType("fakeCode"))).isTrue()
}

@Test
fun `should return identifier string for coding containing system, version and code`() {
val coding = Coding("fakeSystem", "fakeCode", "fakeDisplay").apply { version = "2.0" }
assertThat(coding.identifierString(context)).isEqualTo("fakeSystem2.0|fakeCode")
}

@Test
fun `should return identifier string for coding containing system and version`() {
val coding =
Coding().apply {
system = "fakeSystem"
version = "2.0"
}
assertThat(coding.identifierString(context)).isEqualTo("fakeSystem2.0")
}

@Test
fun `should return identifier string for coding containing system and code`() {
val coding = Coding("fakeSystem", "fakeCode", "fakeDisplay")
assertThat(coding.identifierString(context)).isEqualTo("fakeSystem|fakeCode")
}

@Test
fun `should return identifier string for coding containing only system`() {
val coding = Coding().apply { system = "fakeSystem" }
assertThat(coding.identifierString(context)).isEqualTo("fakeSystem")
}

@Test
fun `should return identifier string for coding containing version and code`() {
val coding =
Coding().apply {
version = "2.0"
code = "fakeCode"
}
assertThat(coding.identifierString(context)).isEqualTo("2.0|fakeCode")
}

@Test
fun `should return identifier string for coding containing only version`() {
val coding = Coding().apply { version = "2.0" }
assertThat(coding.identifierString(context)).isEqualTo("2.0")
}

@Test
fun `should return identifier string for coding containing only code`() {
val coding = Coding().apply { code = "fakeCode" }
assertThat(coding.identifierString(context)).isEqualTo("fakeCode")
}

@Test
fun `should return identifier string for reference`() {
val reference = Reference().apply { reference = "fakeReference" }
assertThat(reference.identifierString(context)).isEqualTo("fakeReference")
}

@Test
fun `should return calculated value for cqf expression`() {
val today = LocalDate.now().toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,13 @@ class AutoCompleteViewHolderFactoryTest {
Coding().setCode("test1-code").setDisplay("Test Code").setId("test1-code") as Coding
),
Questionnaire.QuestionnaireItemAnswerOptionComponent()
.setValue(Coding().setCode("test2-code").setDisplay("Test Code") as Coding)
.setValue(
Coding()
.setSystem("http://answers/test-codes")
.setVersion("1.0")
.setCode("test2-code")
.setDisplay("Test Code") as Coding
)
)

val fakeAnswerValueSetResolver = { uri: String ->
Expand Down Expand Up @@ -189,7 +195,10 @@ class AutoCompleteViewHolderFactoryTest {
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply {
value =
answers
.first { it.value.identifierString(parent.context) == "test2-code" }
.first {
it.value.identifierString(parent.context) ==
"http://answers/test-codes1.0|test2-code"
}
.valueCoding
}
)
Expand Down