111 lines
2.6 KiB
Kotlin
111 lines
2.6 KiB
Kotlin
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.WListDatabase
|
|
import it.unisannio.ding.ids.wedroid.app.data.entity.WList
|
|
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 WListDaoTest {
|
|
private lateinit var dao: WListDao
|
|
private lateinit var db: WListDatabase
|
|
|
|
@get:Rule
|
|
val instantTaskExecutorRule = InstantTaskExecutorRule()
|
|
|
|
@Before
|
|
fun createDb() {
|
|
val context = ApplicationProvider.getApplicationContext<Context>()
|
|
db = Room.inMemoryDatabaseBuilder(
|
|
context, WListDatabase::class.java
|
|
).build()
|
|
dao = db.wListDao()
|
|
}
|
|
|
|
@After
|
|
@Throws(IOException::class)
|
|
fun closeDb() {
|
|
db.close()
|
|
}
|
|
|
|
@Test
|
|
fun emptyDatabaseOnCreation() {
|
|
dao.allWList.observeOnce {
|
|
assertEquals(0, it.size)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
fun insert() {
|
|
val wList = WList("id", "title")
|
|
|
|
runBlocking {
|
|
dao.insert(wList)
|
|
}
|
|
|
|
dao.allWList.observeOnce {
|
|
assertEquals(1, it.size)
|
|
assertEquals(wList, it[0])
|
|
}
|
|
}
|
|
|
|
@Test
|
|
fun replaceOnConflict() {
|
|
val wList0 = WList("id", "title0")
|
|
val wList1 = WList("id", "title1")
|
|
|
|
runBlocking {
|
|
dao.insert(wList0)
|
|
dao.insert(wList1)
|
|
}
|
|
|
|
dao.allWList.observeOnce {
|
|
assertEquals(1, it.size)
|
|
assertEquals("title1", it[0].title)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
fun getInAscendingOrder() {
|
|
val wList0 = WList("id0", "title0")
|
|
val wList1 = WList("id1", "title1")
|
|
|
|
runBlocking {
|
|
dao.insert(wList1)
|
|
dao.insert(wList0)
|
|
}
|
|
|
|
dao.allWList.observeOnce {
|
|
assertEquals(2, it.size)
|
|
assertEquals(wList0, it[0])
|
|
assertEquals(wList1, it[1])
|
|
}
|
|
}
|
|
|
|
@Test
|
|
fun delete() {
|
|
val wlist = WList("id", "title")
|
|
|
|
runBlocking {
|
|
dao.insert(wlist)
|
|
dao.delete(wlist)
|
|
}
|
|
|
|
dao.allWList.observeOnce {
|
|
assertEquals(0, it.size)
|
|
}
|
|
}
|
|
|
|
} |