add database testing
This commit is contained in:
parent
8e9e001c4b
commit
90e31fbf16
@ -0,0 +1,22 @@
|
||||
package it.unisannio.ding.ids.wedroid.app
|
||||
|
||||
import androidx.lifecycle.*
|
||||
|
||||
class OneTimeObserver<T>(private val handler: (T) -> Unit) : Observer<T>, LifecycleOwner {
|
||||
private val lifecycle = LifecycleRegistry(this)
|
||||
init {
|
||||
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
|
||||
}
|
||||
|
||||
override fun getLifecycle(): Lifecycle = lifecycle
|
||||
|
||||
override fun onChanged(t: T) {
|
||||
handler(t)
|
||||
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> LiveData<T>.observeOnce(onChangeHandler: (T) -> Unit) {
|
||||
val observer = OneTimeObserver(handler = onChangeHandler)
|
||||
observe(observer, observer)
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package it.unisannio.ding.ids.wedroid.app.data.dao
|
||||
|
||||
import android.content.Context
|
||||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
||||
import androidx.room.Room
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import it.unisannio.ding.ids.wedroid.app.data.database.BoardDatabase
|
||||
import it.unisannio.ding.ids.wedroid.app.data.entity.Board
|
||||
import it.unisannio.ding.ids.wedroid.app.observeOnce
|
||||
import junit.framework.TestCase.assertEquals
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import java.io.IOException
|
||||
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class BoardDaoTest {
|
||||
private lateinit var dao: BoardDao
|
||||
private lateinit var db: BoardDatabase
|
||||
|
||||
@get:Rule
|
||||
val instantTaskExecutorRule = InstantTaskExecutorRule()
|
||||
|
||||
@Before
|
||||
fun createDb() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
db = Room.inMemoryDatabaseBuilder(
|
||||
context, BoardDatabase::class.java
|
||||
).build()
|
||||
dao = db.boardDao()
|
||||
}
|
||||
|
||||
@After
|
||||
@Throws(IOException::class)
|
||||
fun closeDb() {
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun emptyDatabaseOnCreation() {
|
||||
dao.getAllBoard().observeOnce {
|
||||
assertEquals(0, it.size)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun insert() {
|
||||
val board = Board("id", "title")
|
||||
|
||||
runBlocking {
|
||||
dao.insert(board)
|
||||
}
|
||||
|
||||
dao.getAllBoard().observeOnce {
|
||||
assertEquals(1, it.size)
|
||||
assertEquals(board, it[0])
|
||||
}
|
||||
}
|
||||
|
||||
fun getInAscendingOrder() {
|
||||
val board0 = Board("id0", "title0")
|
||||
val board1 = Board("id1", "title1")
|
||||
|
||||
runBlocking {
|
||||
dao.insert(board1)
|
||||
}
|
||||
|
||||
runBlocking {
|
||||
dao.insert(board0)
|
||||
}
|
||||
|
||||
dao.getAllBoard().observeOnce {
|
||||
assertEquals(2, it.size)
|
||||
assertEquals(board0, it[0])
|
||||
assertEquals(board1, it[1])
|
||||
}
|
||||
}
|
||||
|
||||
fun delete() {
|
||||
val board = Board("id", "title")
|
||||
|
||||
runBlocking {
|
||||
dao.insert(board)
|
||||
}
|
||||
|
||||
runBlocking {
|
||||
dao.delete(board)
|
||||
}
|
||||
|
||||
dao.getAllBoard().observeOnce {
|
||||
assertEquals(0, it.size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user