tinmegali
7/14/2017 - 7:24 PM

Testing Android Room database with Kotlin

Testing Android Room database with Kotlin

package com.tinmegali.daggerwithkotlin

import android.arch.persistence.room.Room
import android.content.Context
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4

import com.tinmegali.daggerwithkotlin.room.AppDatabse
import com.tinmegali.daggerwithkotlin.room.blockingObserve
import com.tinmegali.daggerwithkotlin.room.daos.NoteDAO
import com.tinmegali.daggerwithkotlin.room.entities.Note

import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

import java.io.IOException

// Instrumental Tests
@RunWith(AndroidJUnit4::class)
class DB_NoteDAOTests {

    private var noteDAO: NoteDAO? = null
    private var db: AppDatabse? = null

    @Before
    fun onCreateDB() {
        val context = InstrumentationRegistry.getTargetContext()
        db = Room.inMemoryDatabaseBuilder(context, AppDatabse::class.java).build()
        noteDAO = db!!.noteDAO()
    }

    @After
    @Throws(IOException::class)
    fun closeDB() {
        db!!.close()
    }

    @Test
    @Throws(Exception::class)
    fun readAndWriteTests() {
        val note = TestUtil.createNote()

        // insert
        val insertedID = noteDAO!!.insertNote(note)
        assertNotNull(insertedID)

        // find by id
        val inserted = noteDAO!!.findById(insertedID)
        assertNotNull(inserted)
        assertTrue(inserted.text == note.text)

        // update
        inserted.isDone = true
        val updatedQtd = noteDAO!!.updateNotes(inserted)
        assertEquals(updatedQtd.toLong(), 1)

        // delete
        val deletedQtd = noteDAO!!.deleteNotes(inserted)
        assertEquals(deletedQtd.toLong(), 1)
    }

    @Test
    @Throws(Exception::class)
    fun findLists() {
        val notes = TestUtil.createNoteList(20)

        // bulk insert
        val ids = noteDAO!!.insertNotes(*notes)
        assertNotNull(ids)
        assertEquals(ids.size.toLong(), 20)

        // find all resumed notes with done status
        val doneNotes = noteDAO!!.findAllResumedNotesNotDoneFilteredByUpdateDate()
        assertNotNull(doneNotes)
        for (note in doneNotes) {
            assertTrue(String.format("Note [%s] should be marked as done.", note.id), note.isDone)
        }

        // find all notes grouped by done status
        val groupedNotes = noteDAO!!.findAllNotesGroupedByDoneStatus()
        assertNotNull(groupedNotes)
        var secondGroup = false
        for (i in groupedNotes.indices) {
            if (!secondGroup)
                assertTrue(!groupedNotes[i].isDone)
            else
                assertTrue(groupedNotes[i].isDone)
            if (i + 1 < groupedNotes.size) {
                if (groupedNotes[i].isDone !== groupedNotes[i + 1].isDone)
                    secondGroup = true
            }
        }

        // find all notes ordered by date
        val notesByDate = noteDAO!!.findAllResumedNotesFilteredByUpdateDate()
        assertNotNull(notesByDate)
        assertEquals(notesByDate.size.toLong(), notes.size.toLong())

    }

    @Test
    @Throws(Exception::class)
    fun findListObservable() {
        val notesTemp = TestUtil.createNoteList(10)
        noteDAO!!.insertNotes( *notesTemp )

        val notesLive = noteDAO!!.findAllNotedObservable()
        assertNotNull( notesLive )
        val notesLiveValue = notesLive.blockingObserve()
        assertNotNull( notesLiveValue )

    }

}