Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ktarasenko committed Aug 16, 2023
1 parent a6f47d6 commit b518f01
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal constructor(
for (dependency in dependencies) {
if (knowledgeDao.getImplementationGuide(dependency.packageId, dependency.version) != null)
continue

println(dependency)
val containsPackage = npmFileManager.containsPackage(dependency.packageId, dependency.version)
val npmPackage =
if (containsPackage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
* limitations under the License.
*/

package com.google.android.fhir.knowledge.npm
package com.google.android.fhir.knowledge

import com.google.android.fhir.knowledge.Dependency
import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import com.google.android.fhir.knowledge.db.impl.KnowledgeDatabase
import com.google.android.fhir.knowledge.npm.NpmFileManager
import com.google.android.fhir.knowledge.npm.NpmPackage
import com.google.android.fhir.knowledge.npm.PackageDownloader
import com.google.common.truth.Truth.assertThat
import java.io.File
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -27,50 +33,70 @@ import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(RobolectricTestRunner::class)
class NpmPackageManagerTest { // TODO: fix the test
class KnowledgeManagerNpmTest {

private val downloadedDependencies = mutableSetOf<Dependency>()
private val fakePackageDownloader: PackageDownloader =
PackageDownloader { dependency: Dependency, _ ->
downloadedDependencies.add(dependency)
NPM_CACHE_MAP.getValue(dependency)
}

private val context: Context = ApplicationProvider.getApplicationContext()
private val mockNpmFileManager = mock<NpmFileManager>()
private val knowledgeDb =
Room.inMemoryDatabaseBuilder(context, KnowledgeDatabase::class.java).build()

private val npmPackageManager =
null // NpmPackageManager(mockNpmFileManager, fakePackageDownloader)
private val knowledgeManager =
KnowledgeManager(
knowledgeDb,
context.dataDir,
npmFileManager = mockNpmFileManager,
packageDownloader = fakePackageDownloader
)

@Test
fun install_withDependencies() = runTest {
whenever(mockNpmFileManager.containsPackage(any(), any())).thenReturn(false)

// assertThat(npmPackageManager.getOrDownload(IG1)).containsExactly(NPM1, NPM2, NPM3)
knowledgeManager.install(DEP1)

assertThat(downloadedDependencies).containsExactly(DEP1, DEP2, DEP3)
}

@Test
fun install_alreadyCached() = runTest {
whenever(mockNpmFileManager.containsPackage(any(), any())).thenReturn(true)
whenever(mockNpmFileManager.getPackage(DEP1.packageId, DEP1.version)).thenReturn(NPM1)
whenever(mockNpmFileManager.getPackage(DEP2.packageId, DEP2.version)).thenReturn(NPM2)
whenever(mockNpmFileManager.getPackage(DEP3.packageId, DEP3.version)).thenReturn(NPM3)

// assertThat(npmPackageManager.getOrDownload(IG1)).containsExactly(NPM1)
knowledgeManager.install(DEP1)

assertThat(downloadedDependencies).isEmpty()
}

@Test
fun install_someCached() = runTest {
whenever(mockNpmFileManager.containsPackage(DEP1.packageId, DEP1.version)).thenReturn(false)
whenever(mockNpmFileManager.containsPackage(DEP2.packageId, DEP2.version)).thenReturn(true)
whenever(mockNpmFileManager.containsPackage(DEP3.packageId, DEP3.version)).thenReturn(true)
whenever(mockNpmFileManager.getPackage(DEP2.packageId, DEP2.version)).thenReturn(NPM2)
whenever(mockNpmFileManager.getPackage(DEP3.packageId, DEP3.version)).thenReturn(NPM3)

knowledgeManager.install(DEP1)

// assertThat(npmPackageManager.getOrDownload(IG1)).containsExactly(NPM1, NPM2)
assertThat(downloadedDependencies).containsExactly(DEP1)
}

private companion object {
val DEP1 = Dependency("package1", "version")
val DEP2 = Dependency("package2", "version")
val DEP3 = Dependency("package3", "version")
val NPM1 = NpmPackage(DEP1.packageId, DEP1.version, null, listOf(DEP2), File("fakePath"))
val NPM2 = NpmPackage(DEP2.packageId, DEP2.version, null, listOf(DEP3), File("fakePath"))
val NPM3 = NpmPackage(DEP3.packageId, DEP1.version, null, emptyList(), File("fakePath"))
val NPM1 = NpmPackage(DEP1.packageId, DEP1.version, null, listOf(DEP2), File("/fakePath"))
val NPM2 = NpmPackage(DEP2.packageId, DEP2.version, null, listOf(DEP3), File("/fakePath"))
val NPM3 = NpmPackage(DEP3.packageId, DEP1.version, null, emptyList(), File("/fakePath"))
val NPM_CACHE_MAP = mapOf(DEP1 to NPM1, DEP2 to NPM2, DEP3 to NPM3)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,19 @@ import com.google.android.fhir.knowledge.npm.NpmFileManager
import com.google.android.fhir.knowledge.npm.NpmPackage
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
import org.robolectric.RobolectricTestRunner

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(RobolectricTestRunner::class)
internal class KnowledgeManagerTest {
private val context: Context = ApplicationProvider.getApplicationContext()
private val knowledgeDb =
Room.inMemoryDatabaseBuilder(context, KnowledgeDatabase::class.java).build()
private val knowledgeManager = KnowledgeManager(knowledgeDb, context.cacheDir.absolutePath)
private val implementationGuide = Dependency("anc-cds", "0.3.0", "http://url.com")
private val dependency = Dependency("anc-cds", "0.3.0", "http://url.com")
private val dataFolder = File(javaClass.getResource("/anc-cds")!!.file)

private val jsonParser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()
private val knowledgeDb =
Room.inMemoryDatabaseBuilder(context, KnowledgeDatabase::class.java).build()
private val npmFileManager = NpmFileManager(context.dataDir)
Expand All @@ -53,11 +47,11 @@ internal class KnowledgeManagerTest {
knowledgeDb,
context.dataDir,
npmFileManager = npmFileManager,
packageDownloader = { implementationGuide, _ ->
packageDownloader = { dependency, _ ->
NpmPackage(
implementationGuide.packageId,
implementationGuide.version,
implementationGuide.uri,
dependency.packageId,
dependency.version,
dependency.uri,
emptyList(),
dataFolder
)
Expand Down Expand Up @@ -140,14 +134,6 @@ internal class KnowledgeManagerTest {
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))
}
}

fun `installing from npmPackageManager`() = runTest {
knowledgeManager.install(dependency)

Expand All @@ -166,4 +152,10 @@ internal class KnowledgeManagerTest {
assertThat(knowledgeManager.loadResources(resourceType = "Measure", url = "Measure/ANCIND01"))
.isNotNull()
}

private fun writeToFile(library: Library): File {
return File(context.filesDir, library.name).apply {
writeText(jsonParser.encodeResourceToString(library))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ package com.google.android.fhir.knowledge.npm
import com.google.android.fhir.knowledge.Dependency
import com.google.common.truth.Truth.assertThat
import java.io.File
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(RobolectricTestRunner::class)
class NpmFileManagerTest {
private val testDataFolder = File(javaClass.getResource("/cache_manager")!!.file)
Expand All @@ -51,10 +49,8 @@ class NpmFileManagerTest {
}

@Test
fun getPackage_notFound() = runTest {
val npmPackage = npmFileManager.getPackage(PACKAGE_ID, MISSING_VERSION)

assertThat(npmPackage).isNull()
fun containsPackage_notFound() = runTest {
assertThat(npmFileManager.containsPackage(PACKAGE_ID, MISSING_VERSION)).isFalse()
}

companion object {
Expand All @@ -64,8 +60,6 @@ class NpmFileManagerTest {
val DEPENDENCIES =
listOf(
Dependency("hl7.fhir.r4.core", "4.0.1"),
Dependency("hl7.terminology.r4", "5.0.0"),
Dependency("hl7.fhir.fr.core", "1.1.0")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import androidx.test.core.app.ApplicationProvider
import com.google.android.fhir.knowledge.Dependency
import com.google.common.truth.Truth.assertThat
import java.io.IOException
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
Expand All @@ -31,7 +30,6 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(RobolectricTestRunner::class)
class OkHttpPackageDownloaderTest {

Expand Down

0 comments on commit b518f01

Please sign in to comment.