Skip to content

Commit 3b4f94d

Browse files
authored
Fix failing tests (#3785)
* Remove unnecessary logging of IDs * Fix failling executeCqlShouldInvokeRunCqlLibrary test * Revert deleted comment * Run spotless Apply * Run spotless Apply on engine module * Run spotless Apply on engine module * Add Resource Extension test case
1 parent b3bf46d commit 3b4f94d

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/ResourceExtension.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,28 @@ fun String.resourceClassType(): Class<out Resource> =
452452
*/
453453
fun String.extractLogicalIdUuid() = this.substringAfter("/").substringBefore("/")
454454

455+
/**
456+
* A function that extracts only the UUID part of a resource logicalId from a URI.
457+
*
458+
* Examples:
459+
* 1. "http://smartreg.org/Library/3e2bb367-b9bb-4c30-9033-8f42657c5df7/history/3" returns
460+
*
461+
* ```
462+
* "3e2bb367-b9bb-4c30-9033-8f42657c5df7".
463+
* ```
464+
*/
465+
fun String.extractLogicalIdUuidFromURI(resourceType: ResourceType): String {
466+
val pathSegments: List<String> = this.split("/")
467+
val resourceIndex = pathSegments.indexOf(resourceType.name)
468+
469+
// Check if the resource type was found and if there's a segment after it
470+
return if (resourceIndex != -1 && resourceIndex + 1 < pathSegments.size) {
471+
pathSegments[resourceIndex + 1]
472+
} else {
473+
"" // Return empty if the resource type or its ID is not found
474+
}
475+
}
476+
455477
/**
456478
* This suspend function updates the due date of the dependents of the current [Task], based on the
457479
* date of a related [Immunization] [Task]. The function loops through all the tasks that are

android/engine/src/test/java/org/smartregister/fhircore/engine/util/extension/ResourceExtensionTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import org.hl7.fhir.r4.model.Questionnaire
5252
import org.hl7.fhir.r4.model.QuestionnaireResponse
5353
import org.hl7.fhir.r4.model.Reference
5454
import org.hl7.fhir.r4.model.RelatedPerson
55+
import org.hl7.fhir.r4.model.ResourceType
5556
import org.hl7.fhir.r4.model.StringType
5657
import org.hl7.fhir.r4.model.Task
5758
import org.hl7.fhir.r4.model.Timing
@@ -688,6 +689,24 @@ class ResourceExtensionTest : RobolectricTest() {
688689
)
689690
}
690691

692+
@Test
693+
fun testLogicalIdFromFhirPathExtractedURIReturnsCorrectValue() {
694+
val libraryUri = "http://smartreg.org/Library/3e2bb367-b9bb-4c30-9033-8f42657c5df7/history/3"
695+
Assert.assertEquals(
696+
"3e2bb367-b9bb-4c30-9033-8f42657c5df7",
697+
libraryUri.extractLogicalIdUuidFromURI(ResourceType.Library),
698+
)
699+
}
700+
701+
@Test
702+
fun testLogicalIdFromFhirPathExtractedURIReturnsEmptyStringWhenResourceTypeIsMissing() {
703+
val libraryUri = "http://smartreg.org/3e2bb367-b9bb-4c30-9033-8f42657c5df7/history/3"
704+
Assert.assertEquals(
705+
"",
706+
libraryUri.extractLogicalIdUuidFromURI(ResourceType.Library),
707+
)
708+
}
709+
691710
@Test
692711
fun `prepareQuestionsForReadingOrEditing should set readOnly to true when passed`() {
693712
val questionnaire = Questionnaire()

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import org.smartregister.fhircore.engine.util.extension.cqfLibraryUrls
8989
import org.smartregister.fhircore.engine.util.extension.extractByStructureMap
9090
import org.smartregister.fhircore.engine.util.extension.extractId
9191
import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid
92+
import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuidFromURI
9293
import org.smartregister.fhircore.engine.util.extension.find
9394
import org.smartregister.fhircore.engine.util.extension.generateMissingId
9495
import org.smartregister.fhircore.engine.util.extension.isIn
@@ -882,7 +883,9 @@ constructor(
882883

883884
val libraryFilters =
884885
questionnaire.cqfLibraryUrls().map {
885-
val apply: TokenParamFilterCriterion.() -> Unit = { value = of(it.extractLogicalIdUuid()) }
886+
val apply: TokenParamFilterCriterion.() -> Unit = {
887+
value = of(it.extractLogicalIdUuidFromURI(ResourceType.Library))
888+
}
886889
apply
887890
}
888891

android/quest/src/test/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModelTest.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ import org.smartregister.fhircore.quest.ui.questionnaire.QuestionnaireViewModel.
128128
import org.smartregister.fhircore.quest.util.QuestionnaireResponseUtils
129129
import org.smartregister.model.practitioner.FhirPractitionerDetails
130130
import org.smartregister.model.practitioner.PractitionerDetails
131-
import timber.log.Timber
132131

133132
@HiltAndroidTest
134133
class QuestionnaireViewModelTest : RobolectricTest() {
@@ -259,8 +258,7 @@ class QuestionnaireViewModelTest : RobolectricTest() {
259258
{
260259
runBlocking { (firstArg() as suspend () -> Unit).invoke() }
261260
}
262-
val onSuccessfulSubmission =
263-
spyk({ idsTypes: List<IdType>, _: QuestionnaireResponse -> Timber.i(idsTypes.toString()) })
261+
val onSuccessfulSubmission: (List<IdType>, QuestionnaireResponse) -> Unit = spyk()
264262
coEvery {
265263
ResourceMapper.extract(
266264
questionnaire = questionnaire,
@@ -1225,7 +1223,7 @@ class QuestionnaireViewModelTest : RobolectricTest() {
12251223

12261224
val cqlLibrary =
12271225
Library().apply {
1228-
id = "Library/123"
1226+
id = "123"
12291227
url = "http://smartreg.org/Library/123"
12301228
name = "123"
12311229
version = "1.0.0"
@@ -1244,9 +1242,18 @@ class QuestionnaireViewModelTest : RobolectricTest() {
12441242
},
12451243
)
12461244

1247-
fhirEngine.create(patient)
1245+
defaultRepository.fhirEngine.create(cqlLibrary)
12481246

1249-
questionnaireViewModel.executeCql(patient, bundle, questionnaire)
1247+
// Load the CQL input resources from the questionnaireConfig
1248+
val questionnaireConfigCqlInputResources =
1249+
questionnaireConfig.copy(cqlInputResources = listOf("basic-resource-id"))
1250+
1251+
questionnaireViewModel.executeCql(
1252+
patient,
1253+
bundle,
1254+
questionnaire,
1255+
questionnaireConfigCqlInputResources,
1256+
)
12501257

12511258
coVerify {
12521259
fhirOperator.evaluateLibrary(

0 commit comments

Comments
 (0)