Skip to content

Commit

Permalink
Search latest QR in PdfLauncherViewModel (#3499)
Browse files Browse the repository at this point in the history
* search qr by latest date

* spotless

* Add test

* spotless
  • Loading branch information
FikriMilano committed Sep 19, 2024
1 parent bd4fedd commit 97a75a0
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ constructor(
): QuestionnaireResponse? {
val searchQuery =
createQuestionnaireResponseSearchQuery(questionnaireId, subjectId, subjectType)
return defaultRepository.search<QuestionnaireResponse>(searchQuery).firstOrNull()
return defaultRepository.search<QuestionnaireResponse>(searchQuery).maxByOrNull {
it.meta.lastUpdated
}
}

/**
Expand All @@ -77,8 +79,6 @@ constructor(
QuestionnaireResponse.QUESTIONNAIRE,
{ value = "${ResourceType.Questionnaire}/$questionnaireId" },
)
count = 1
from = 0
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.quest.pdf

import com.google.android.fhir.FhirEngine
import com.google.android.fhir.SearchResult
import com.google.android.fhir.search.Search
import io.mockk.coEvery
import io.mockk.mockk
import java.util.Date
import kotlinx.coroutines.test.runTest
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.ResourceType
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.util.extension.asReference
import org.smartregister.fhircore.engine.util.extension.yesterday
import org.smartregister.fhircore.quest.robolectric.RobolectricTest
import org.smartregister.fhircore.quest.ui.pdf.PdfLauncherViewModel

class PdfLauncherViewModelTest : RobolectricTest() {

private lateinit var fhirEngine: FhirEngine
private lateinit var defaultRepository: DefaultRepository
private lateinit var viewModel: PdfLauncherViewModel

@Before
fun setUp() {
fhirEngine = mockk()
defaultRepository =
DefaultRepository(
fhirEngine = fhirEngine,
dispatcherProvider = mockk(),
sharedPreferencesHelper = mockk(),
configurationRegistry = mockk(),
configService = mockk(),
configRulesExecutor = mockk(),
fhirPathDataExtractor = mockk(),
parser = mockk(),
context = mockk(),
)
viewModel = PdfLauncherViewModel(defaultRepository)
}

@Test
fun testRetrieveQuestionnaireResponseReturnsLatestResponse() = runTest {
val patient = Patient().apply { id = "p1" }
val questionnaire = Questionnaire().apply { id = "q1" }
val olderQuestionnaireResponse =
QuestionnaireResponse().apply {
id = "qr2"
meta.lastUpdated = yesterday()
subject = patient.asReference()
setQuestionnaire(questionnaire.asReference().reference)
}
val latestQuestionnaireResponse =
QuestionnaireResponse().apply {
id = "qr1"
meta.lastUpdated = Date()
subject = patient.asReference()
setQuestionnaire(questionnaire.asReference().reference)
}
val questionnaireResponses =
listOf(olderQuestionnaireResponse, latestQuestionnaireResponse).map {
SearchResult(it, null, null)
}

coEvery { fhirEngine.search<QuestionnaireResponse>(any<Search>()) } returns
questionnaireResponses
val result =
viewModel.retrieveQuestionnaireResponse(questionnaire.id, patient.id, ResourceType.Patient)

assertEquals(latestQuestionnaireResponse.id, result!!.id)
}
}

0 comments on commit 97a75a0

Please sign in to comment.