Skip to content

Commit

Permalink
Refactor QuestionnaireViewModelTest
Browse files Browse the repository at this point in the history
Signed-off-by: Elly Kitoto <junkmailstoelly@gmail.com>
  • Loading branch information
ellykits committed Jul 31, 2023
1 parent 1f454cf commit 0b607e5
Show file tree
Hide file tree
Showing 8 changed files with 437 additions and 2,960 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fun Questionnaire.extractByStructureMap() =

fun Questionnaire.cqfLibraryIds() =
this.extension
.filter { it.url.contains("cqf-library") }
.filter { it.url.contains("cqf-library", ignoreCase = true) }
.mapNotNull { it.value?.asStringValue()?.replace("Library/", "") }

fun QuestionnaireResponse.findSubject(bundle: Bundle?) =
Expand Down Expand Up @@ -155,9 +155,7 @@ fun List<Questionnaire.QuestionnaireItemComponent>.prePopulateInitialValues(
forEach { item ->
prePopulationParams
.firstOrNull {
it.linkId == item.linkId &&
!it.value.isNullOrEmpty() &&
!it.value.contains(interpolationPrefix)
it.linkId == item.linkId && it.value.isNotEmpty() && !it.value.contains(interpolationPrefix)
}
?.let { actionParam ->
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import org.hl7.fhir.r4.model.ListResource.ListEntryComponent
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.StringType
Expand Down Expand Up @@ -233,7 +232,7 @@ constructor(

defaultRepository.addOrUpdate(resource = this)

addMemberToConfiguredGroup(questionnaireConfig.groupResource)
addMemberToConfiguredGroup(this, questionnaireConfig.groupResource)

// Track ids for resources in ListResource added to the QuestionnaireResponse.contained
val listEntryComponent =
Expand Down Expand Up @@ -392,12 +391,12 @@ constructor(
* has an answer.
*/
fun saveDraftQuestionnaire(questionnaireResponse: QuestionnaireResponse) {
val questionnaireHasAnswer =
questionnaireResponse.item.any {
it.answer.any { answerComponent -> answerComponent.hasValue() }
}
if (questionnaireHasAnswer) {
viewModelScope.launch(dispatcherProvider.io()) {
viewModelScope.launch {
val questionnaireHasAnswer =
questionnaireResponse.item.any {
it.answer.any { answerComponent -> answerComponent.hasValue() }
}
if (questionnaireHasAnswer) {
questionnaireResponse.status = QuestionnaireResponse.QuestionnaireResponseStatus.INPROGRESS
defaultRepository.addOrUpdate(addMandatoryTags = true, resource = questionnaireResponse)
}
Expand All @@ -418,10 +417,24 @@ constructor(

updateOnEditParams?.forEach { param ->
try {
val resource = defaultRepository.loadResource(Reference(param.value))
defaultRepository.addOrUpdate(resource = resource)
defaultRepository.run {
val resourceType = param.resourceType
if (resourceType != null) {
loadResource(resourceType, param.value)?.let { addOrUpdate(resource = it) }
} else {
val valueResourceType = param.value.substringBefore("/")
val valueResourceId = param.value.substringAfter("/")
addOrUpdate(
resource = loadResource(valueResourceId, ResourceType.valueOf(valueResourceType)),
)
}
}
} catch (resourceNotFoundException: ResourceNotFoundException) {
Timber.e("Unable to update resource's _lastUpdated", resourceNotFoundException)
} catch (illegalArgumentException: IllegalArgumentException) {
Timber.e(
"No enum constant org.hl7.fhir.r4.model.ResourceType.${param.value.substringBefore("/")}",
)
}
}
}
Expand Down Expand Up @@ -480,17 +493,17 @@ constructor(
* NOT the same as the retrieved [GroupResourceConfig.groupIdentifier] (Cannot add a [Group] as
* member of itself.
*/
private suspend fun Resource.addMemberToConfiguredGroup(groupConfig: GroupResourceConfig?) {
suspend fun addMemberToConfiguredGroup(resource: Resource, groupConfig: GroupResourceConfig?) {
val group: Group =
groupConfig?.groupIdentifier?.let { loadResource(ResourceType.Group, it) } as Group? ?: return
val reference = asReference()
val member = group.member.find { it.entity == reference }
val reference = resource.asReference()
val member = group.member.find { it.entity.reference.equals(reference.reference, true) }

// Cannot add Group as member of itself; Cannot not duplicate existing members
if (this.logicalId == group.logicalId || member != null) return
if (resource.logicalId == group.logicalId || member != null) return

if (
this.resourceType.isIn(
resource.resourceType.isIn(
ResourceType.CareTeam,
ResourceType.Device,
ResourceType.Group,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.cql.LibraryEvaluator
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.rulesengine.ConfigRulesExecutor
import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider
import org.smartregister.fhircore.quest.app.fakes.Faker
import org.smartregister.fhircore.quest.robolectric.RobolectricTest

Expand All @@ -57,14 +56,28 @@ class CqlContentTest : RobolectricTest() {
@get:Rule(order = 0) var hiltRule = HiltAndroidRule(this)
private val fhirContext: FhirContext = FhirContext.forCached(FhirVersionEnum.R4)
private val parser = fhirContext.newJsonParser()!!
private val evaluator = LibraryEvaluator().apply { initialize() }
private val configurationRegistry = Faker.buildTestConfigurationRegistry()
private val configService: ConfigService = mockk()
private val configRulesExecutor: ConfigRulesExecutor = mockk()
private lateinit var evaluator: LibraryEvaluator
private lateinit var defaultRepository: DefaultRepository
val fhirEngine = mockk<FhirEngine>()

@Before
fun setUp() {
hiltRule.inject()
defaultRepository =
spyk(
DefaultRepository(
fhirEngine = fhirEngine,
dispatcherProvider = coroutineTestRule.testDispatcherProvider,
sharedPreferencesHelper = mockk(),
configurationRegistry = configurationRegistry,
configService = configService,
configRulesExecutor = configRulesExecutor,
),
)
evaluator = LibraryEvaluator(defaultRepository).apply { initialize() }
}

@Test
Expand Down Expand Up @@ -101,17 +114,6 @@ class CqlContentTest : RobolectricTest() {
}

val fhirEngine = mockk<FhirEngine>()
val defaultRepository =
spyk(
DefaultRepository(
fhirEngine,
DefaultDispatcherProvider(),
mockk(),
configurationRegistry,
configService,
configRulesExecutor,
),
)

coEvery { fhirEngine.get(ResourceType.Library, cqlLibrary.logicalId) } returns cqlLibrary
coEvery { fhirEngine.get(ResourceType.Library, fhirHelpersLibrary.logicalId) } returns
Expand All @@ -121,13 +123,13 @@ class CqlContentTest : RobolectricTest() {

val result = runBlocking {
evaluator.runCqlLibrary(
cqlLibrary.logicalId,
patient,
dataBundle.apply {
this.entry.removeIf { it.resource.resourceType == ResourceType.Patient }
},
defaultRepository,
true,
libraryId = cqlLibrary.logicalId,
patient = patient,
data =
dataBundle.apply {
this.entry.removeIf { it.resource.resourceType == ResourceType.Patient }
},
outputLog = true,
)
}

Expand Down Expand Up @@ -167,19 +169,6 @@ class CqlContentTest : RobolectricTest() {
.forEach { addEntry().apply { resource = it } }
}

val fhirEngine = mockk<FhirEngine>()
val defaultRepository =
spyk(
DefaultRepository(
fhirEngine,
DefaultDispatcherProvider(),
mockk(),
configurationRegistry,
configService,
configRulesExecutor,
),
)

coEvery { fhirEngine.get(ResourceType.Library, cqlLibrary.logicalId) } returns cqlLibrary
coEvery { fhirEngine.get(ResourceType.Library, fhirHelpersLibrary.logicalId) } returns
fhirHelpersLibrary
Expand All @@ -188,13 +177,13 @@ class CqlContentTest : RobolectricTest() {

val result = runBlocking {
evaluator.runCqlLibrary(
cqlLibrary.logicalId,
patient,
dataBundle.apply {
this.entry.removeIf { it.resource.resourceType == ResourceType.Patient }
},
defaultRepository,
true,
libraryId = cqlLibrary.logicalId,
patient = patient,
data =
dataBundle.apply {
this.entry.removeIf { it.resource.resourceType == ResourceType.Patient }
},
outputLog = true,
)
}

Expand Down Expand Up @@ -239,27 +228,14 @@ class CqlContentTest : RobolectricTest() {
}
}

val fhirEngine = mockk<FhirEngine>()
val defaultRepository =
spyk(
DefaultRepository(
fhirEngine,
DefaultDispatcherProvider(),
mockk(),
configurationRegistry,
configService,
configRulesExecutor,
),
)

coEvery { fhirEngine.get(ResourceType.Library, cqlLibrary.logicalId) } returns cqlLibrary
coEvery { fhirEngine.get(ResourceType.Library, fhirHelpersLibrary.logicalId) } returns
fhirHelpersLibrary
coEvery { defaultRepository.create(any(), any()) } returns emptyList()
coEvery { configService.provideResourceTags(any()) } returns listOf()

val result = runBlocking {
evaluator.runCqlLibrary(cqlLibrary.logicalId, null, dataBundle, defaultRepository)
evaluator.runCqlLibrary(libraryId = cqlLibrary.logicalId, patient = null, data = dataBundle)
}

println(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ class StructureMapUtilitiesTest : RobolectricTest() {
val questionnaire = iParser.parseResource(Questionnaire::class.java, questionnaireJson)
val questionnaireResponse: QuestionnaireResponse

runBlocking { questionnaireResponse = firstTaskpulate(questionnaire, patient, immunization) }
runBlocking {
questionnaireResponse = ResourceMapper.populate(questionnaire, patient, immunization)
}

structureMapUtilities.transform(contextR4, questionnaireResponse, structureMap, targetResource)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.smartregister.fhircore.quest.app.fakes.Faker
import org.smartregister.fhircore.quest.event.AppEvent
import org.smartregister.fhircore.quest.event.EventBus
import org.smartregister.fhircore.quest.robolectric.ActivityRobolectricTest
import org.smartregister.fhircore.quest.ui.questionnaire.QuestionnaireActivity

@OptIn(ExperimentalMaterialApi::class)
@HiltAndroidTest
Expand Down
Loading

0 comments on commit 0b607e5

Please sign in to comment.