Skip to content

Commit

Permalink
Test: Add local database instrumented test
Browse files Browse the repository at this point in the history
This patch adds local database instrumented test.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Jul 29, 2024
1 parent 3c627c4 commit 2964f07
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 24 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Context>()
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)
}
}
}
}

0 comments on commit 2964f07

Please sign in to comment.