Skip to content

Commit

Permalink
fix filter related resources (#2769)
Browse files Browse the repository at this point in the history
* fix filter related resources when expression is not provided

* spotless ran
  • Loading branch information
aurangzaibumer authored Sep 19, 2023
1 parent aaacdbb commit 40faa3d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -778,11 +778,19 @@ constructor(
fhirEngine.update(resource)
}

/**
* Filtering the Related Resources is achieved by use of the filterFhirPathExpression
* configuration. It specifies which field and values to filter the resources by.
*/
fun filterRelatedResource(resource: Resource, resourceConfig: ResourceConfig): Boolean {
return resourceConfig.filterFhirPathExpressions?.any { filterFhirPathExpression ->
fhirPathDataExtractor.extractValue(resource, filterFhirPathExpression.key) ==
filterFhirPathExpression.value
} == true
return if (resourceConfig.filterFhirPathExpressions?.isEmpty() == true) {
true
} else {
resourceConfig.filterFhirPathExpressions?.any { filterFhirPathExpression ->
fhirPathDataExtractor.extractValue(resource, filterFhirPathExpression.key) ==
filterFhirPathExpression.value
} == true
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.configuration.profile.ManagingEntityConfig
import org.smartregister.fhircore.engine.domain.model.Code
import org.smartregister.fhircore.engine.domain.model.KeyValueConfig
import org.smartregister.fhircore.engine.domain.model.ResourceConfig
import org.smartregister.fhircore.engine.robolectric.RobolectricTest
import org.smartregister.fhircore.engine.rulesengine.ConfigRulesExecutor
Expand Down Expand Up @@ -560,7 +561,7 @@ class DefaultRepositoryTest : RobolectricTest() {
configurationRegistry = mockk(),
configService = mockk(),
configRulesExecutor = mockk(),
fhirPathDataExtractor = mockk(),
fhirPathDataExtractor = fhirPathDataExtractor,
),
)
coEvery { fhirEngine.search<RelatedPerson>(any()) } returns
Expand Down Expand Up @@ -806,6 +807,50 @@ class DefaultRepositoryTest : RobolectricTest() {
Assert.assertEquals(CarePlan.CarePlanStatus.COMPLETED, carePlanSlot.captured.status)
}

@Test
fun testFilterRelatedResourcesShouldReturnTrueIfProvidedExpressionEvaluatesToTrue() {
val resourceConfig =
ResourceConfig(
resource = ResourceType.Task,
filterFhirPathExpressions = listOf(KeyValueConfig("Task.status", "ready")),
)
val task =
Task().apply {
id = "37793d31-def5-40bd-a2e3-fdaf5a0ddc53"
status = Task.TaskStatus.READY
}
val result = defaultRepository.filterRelatedResource(task, resourceConfig)
Assert.assertTrue(result)
}

@Test
fun testFilterRelatedResourcesShouldReturnFalseIfProvidedExpressionEvaluatesToFalse() {
val resourceConfig =
ResourceConfig(
resource = ResourceType.Task,
filterFhirPathExpressions = listOf(KeyValueConfig("Task.status", "cancelled")),
)
val task =
Task().apply {
id = "37793d31-def5-40bd-a2e3-fdaf5a0ddc53"
status = Task.TaskStatus.READY
}
val result = defaultRepository.filterRelatedResource(task, resourceConfig)
Assert.assertFalse(result)
}

@Test
fun testFilterRelatedResourcesShouldReturnTrueIfExpressionIsNotProvided() {
val resourceConfig = ResourceConfig(resource = ResourceType.Task)
val task =
Task().apply {
id = "37793d31-def5-40bd-a2e3-fdaf5a0ddc53"
status = Task.TaskStatus.READY
}
val result = defaultRepository.filterRelatedResource(task, resourceConfig)
Assert.assertTrue(result)
}

@Test
fun testCloseResourceUpdatesCorrectProcedureStatus() {
val procedure =
Expand Down

0 comments on commit 40faa3d

Please sign in to comment.