Skip to content

Commit

Permalink
Merge branch 'main' into 3489-and-3490-and-refactor-pdf-config
Browse files Browse the repository at this point in the history
  • Loading branch information
ellykits authored Sep 19, 2024
2 parents 28b04d4 + 97a75a0 commit 27fe6b7
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 94 deletions.
2 changes: 1 addition & 1 deletion android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ okhttp = "4.12.0"
okhttp-logging-interceptor = "4.12.0"
orchestrator = "1.5.0"
owasp = "8.2.1"
p2p-lib = "0.6.10-SNAPSHOT"
p2p-lib = "0.6.11-SNAPSHOT"
playServicesLocation = "21.3.0"
playServicesTasks = "18.2.0"
preference-ktx = "1.2.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ constructor(
questionnaireId: String,
subjectReference: String,
): QuestionnaireResponse? {
val searchQuery = createQuestionnaireResponseSearchQuery(questionnaireId, subjectReference)
return defaultRepository.search<QuestionnaireResponse>(searchQuery).firstOrNull()
val searchQuery =
createQuestionnaireResponseSearchQuery(questionnaireId, subjectId, subjectType)
return defaultRepository.search<QuestionnaireResponse>(searchQuery).maxByOrNull {
it.meta.lastUpdated
}
}

/**
Expand All @@ -72,8 +75,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)
}
}
100 changes: 57 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 27fe6b7

Please sign in to comment.