Last active
September 14, 2020 17:30
-
-
Save tinmegali/8396c89b1f0a78f94d98c035a93fc057 to your computer and use it in GitHub Desktop.
Testing Android Room database with Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where does the data come from?