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

Search latest QR in PdfLauncherViewModel #3499

Merged
merged 5 commits into from
Sep 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
): QuestionnaireResponse? {
val searchQuery =
createQuestionnaireResponseSearchQuery(questionnaireId, subjectId, subjectType)
return defaultRepository.search<QuestionnaireResponse>(searchQuery).firstOrNull()
return defaultRepository.search<QuestionnaireResponse>(searchQuery).maxByOrNull {
FikriMilano marked this conversation as resolved.
Show resolved Hide resolved
it.meta.lastUpdated

Check warning on line 59 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/pdf/PdfLauncherViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/pdf/PdfLauncherViewModel.kt#L58-L59

Added lines #L58 - L59 were not covered by tests
}
}

/**
Expand All @@ -77,8 +79,6 @@
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)
}
}
Loading