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

Fix the version collision in Knowledge Manager #2043

Merged
merged 7 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -88,7 +88,8 @@ internal constructor(
val resType = ResourceType.fromCode(resourceType)
val resourceEntities =
when {
url != null -> listOfNotNull(knowledgeDao.getResourceWithUrl(url))
url != null && version != null ->
listOfNotNull(knowledgeDao.getResourceWithUrlAndVersion(url, version))
id != null -> listOfNotNull(knowledgeDao.getResourceWithUrlLike("%$id"))
name != null && version != null ->
listOfNotNull(knowledgeDao.getResourcesWithNameAndVersion(resType, name, version))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class KnowledgeDao {

val resourceMetadata =
if (resource.url != null) {
getResourceWithUrl(resource.url)
getResourceWithUrlAndVersion(resource.url, resource.version.orEmpty())
} else {
getResourcesWithNameAndVersion(resource.resourceType, resource.name, resource.version)
}
Expand Down Expand Up @@ -83,9 +83,10 @@ abstract class KnowledgeDao {
resourceType: ResourceType,
): List<ResourceMetadataEntity>

@Query("SELECT * from ResourceMetadataEntity WHERE url = :url")
internal abstract suspend fun getResourceWithUrl(
@Query("SELECT * from ResourceMetadataEntity WHERE url = :url AND version = :version")
internal abstract suspend fun getResourceWithUrlAndVersion(
url: String,
version: String
): ResourceMetadataEntity?
// Remove after https://github.com/google/android-fhir/issues/1920
@Query("SELECT * from ResourceMetadataEntity WHERE url LIKE :urlPart")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ package com.google.android.fhir.knowledge
import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import com.google.android.fhir.knowledge.db.impl.KnowledgeDatabase
import com.google.common.truth.Truth.assertThat
import java.io.File
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.hl7.fhir.r4.model.Library
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -91,4 +94,35 @@ internal class KnowledgeManagerTest {
assertThat(knowledgeManager.loadResources(resourceType = "Measure", url = "Measure/ANCIND01"))
.isNotNull()
}

@Test
fun `inserting a library of a different version creates new entry`() = runTest {
val libraryAOld =
Library().apply {
id = "defaultA"
name = "defaultA"
url = "www.exampleA.com"
version = "A.1.0.0"
}
val libraryANew =
Library().apply {
id = "defaultA"
name = "defaultA"
url = "www.exampleA.com"
version = "A.1.0.1"
}

knowledgeManager.install(writeToFile(libraryAOld))
knowledgeManager.install(writeToFile(libraryANew))

val resources = knowledgeDb.knowledgeDao().getResources()
assertThat(resources).hasSize(2)
}

private val jsonParser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()
private fun writeToFile(library: Library): File {
return File(context.filesDir, library.name).apply {
writeText(jsonParser.encodeResourceToString(library))
}
}
}