From 2964f07c11bea907ccdba7f7473d1b6545418b58 Mon Sep 17 00:00:00 2001 From: Yelin Jeong Date: Mon, 29 Jul 2024 15:38:34 +0900 Subject: [PATCH] Test: Add local database instrumented test This patch adds local database instrumented test. Signed-off-by: Yelin Jeong --- .../offloading/ExampleInstrumentedTest.kt | 24 ------- .../inference/offloading/LocalDatabaseTest.kt | 70 +++++++++++++++++++ 2 files changed, 70 insertions(+), 24 deletions(-) delete mode 100644 ml_inference_offloading/src/androidTest/java/ai/nnstreamer/ml/inference/offloading/ExampleInstrumentedTest.kt create mode 100644 ml_inference_offloading/src/androidTest/java/ai/nnstreamer/ml/inference/offloading/LocalDatabaseTest.kt diff --git a/ml_inference_offloading/src/androidTest/java/ai/nnstreamer/ml/inference/offloading/ExampleInstrumentedTest.kt b/ml_inference_offloading/src/androidTest/java/ai/nnstreamer/ml/inference/offloading/ExampleInstrumentedTest.kt deleted file mode 100644 index 6fa307f..0000000 --- a/ml_inference_offloading/src/androidTest/java/ai/nnstreamer/ml/inference/offloading/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package ai.nnstreamer.ml.inference.offloading - -import android.support.test.InstrumentationRegistry -import android.support.test.runner.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("ai.nnstreamer.ml.inference.offloading", appContext.packageName) - } -} \ No newline at end of file diff --git a/ml_inference_offloading/src/androidTest/java/ai/nnstreamer/ml/inference/offloading/LocalDatabaseTest.kt b/ml_inference_offloading/src/androidTest/java/ai/nnstreamer/ml/inference/offloading/LocalDatabaseTest.kt new file mode 100644 index 0000000..25745db --- /dev/null +++ b/ml_inference_offloading/src/androidTest/java/ai/nnstreamer/ml/inference/offloading/LocalDatabaseTest.kt @@ -0,0 +1,70 @@ +package ai.nnstreamer.ml.inference.offloading + +import ai.nnstreamer.ml.inference.offloading.data.AppDatabase +import ai.nnstreamer.ml.inference.offloading.data.Model +import ai.nnstreamer.ml.inference.offloading.data.ModelDao +import ai.nnstreamer.ml.inference.offloading.data.OffloadingService +import ai.nnstreamer.ml.inference.offloading.data.OffloadingServiceDao +import android.content.Context +import androidx.room.Room +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import kotlinx.coroutines.flow.take +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.runTest +import org.junit.After + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* +import org.junit.Before +import org.nnsuite.nnstreamer.Pipeline +import java.io.IOException +import kotlin.jvm.Throws + +@RunWith(AndroidJUnit4::class) +class LocalDatabaseTest { + private lateinit var database: AppDatabase + private lateinit var modelDao: ModelDao + private lateinit var offloadingServiceDao: OffloadingServiceDao + + @Before + fun createDb() { + val context = ApplicationProvider.getApplicationContext() + database = Room.inMemoryDatabaseBuilder( + context, AppDatabase::class.java + ).build() + modelDao = database.modelDao() + offloadingServiceDao = database.offloadingServiceDao() + } + + @After + @Throws(IOException::class) + fun closeDb() { + database.close() + } + + @Test + @Throws(Exception::class) + fun writeOffloadingServiceAndReadInList() = runTest { + val modelId = 1 + val model = Model(modelId, "model_name") + modelDao.insert(model) + + val serviceId = 1 + val port = 3030 + val offloadingService = OffloadingService(serviceId, modelId, port, Pipeline.State.NULL) + offloadingServiceDao.insert(offloadingService) + + launch { + val byId = offloadingServiceDao.getOffloadingService(1) + byId.take(1).collect { + assertEquals(serviceId, it.serviceId) + assertEquals(modelId, it.modelId) + assertEquals(port, it.port) + assertEquals(Pipeline.State.NULL, it.state) + } + } + } +}