From 90e31fbf168b3d745be49b68f126b45c46ec1a14 Mon Sep 17 00:00:00 2001 From: norangebit Date: Sun, 1 Dec 2019 16:24:44 +0100 Subject: [PATCH] add database testing --- .../ding/ids/wedroid/app/TestHelper.kt | 22 ++++ .../ids/wedroid/app/data/dao/BoardDaoTest.kt | 103 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/TestHelper.kt create mode 100644 app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/data/dao/BoardDaoTest.kt diff --git a/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/TestHelper.kt b/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/TestHelper.kt new file mode 100644 index 0000000..017874f --- /dev/null +++ b/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/TestHelper.kt @@ -0,0 +1,22 @@ +package it.unisannio.ding.ids.wedroid.app + +import androidx.lifecycle.* + +class OneTimeObserver(private val handler: (T) -> Unit) : Observer, 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 LiveData.observeOnce(onChangeHandler: (T) -> Unit) { + val observer = OneTimeObserver(handler = onChangeHandler) + observe(observer, observer) +} diff --git a/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/data/dao/BoardDaoTest.kt b/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/data/dao/BoardDaoTest.kt new file mode 100644 index 0000000..dae8ff8 --- /dev/null +++ b/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/data/dao/BoardDaoTest.kt @@ -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() + 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) + } + } +} + + + +