test db
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Umberto Furno 2020-01-11 22:58:01 +01:00
parent 47fa49aa07
commit 1273e080ac
3 changed files with 315 additions and 0 deletions

View File

@ -71,6 +71,7 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'

View File

@ -0,0 +1,111 @@
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)
}
}
}

View File

@ -0,0 +1,203 @@
package it.unisannio.ding.ids.wedroid.app.data.repository
import android.util.Log
import androidx.lifecycle.LiveData
import io.mockk.*
import it.unisannio.ding.ids.wedroid.app.data.dao.WListDao
import it.unisannio.ding.ids.wedroid.app.data.entity.WList
import it.unisannio.ding.ids.wedroid.app.getPrivateFun
import it.unisannio.ding.ids.wedroid.app.util.PreferenceReader
import it.unisannio.ding.ids.wedroid.wrapper.api.ListService
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import org.junit.Test
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import kotlin.reflect.full.callSuspend
class WListRepositoryTest {
private val reader = mockk<PreferenceReader>()
private val webServer = MockWebServer()
private lateinit var service: ListService
@Before
fun setUp() {
webServer.start()
service = Retrofit.Builder()
.baseUrl(webServer.url("/"))
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ListService::class.java)
mockkStatic(Log::class)
every { reader.boardId } returns "board id"
every { Log.e(any(), any()) } returns 0
}
@After
fun teardown() {
webServer.close()
}
@Test
fun addNewBoardsToDb() {
val dao = mockk<WListDao>()
var count = 0
coEvery {
dao.insert(any())
} answers {
count++
}
val addNewListToDb = getPrivateFun(
"addNewWListToDb", WListRepository::class
)
val repository = WListRepository(
dao, service, reader
)
val list0 = WList("id0", "title0")
val list1 = WList("id1", "title1")
val list2 = WList("id2", "title2")
runBlocking {
addNewListToDb?.callSuspend(
repository,
listOf(list0, list1, list2)
)
}
coVerifyAll {
dao.insert(list0)
dao.insert(list1)
dao.insert(list2)
}
TestCase.assertEquals(3, count)
}
@Test
fun deleteWListCallbackSuccess() {
val dao = mockk<WListDao>()
val response =
mockk<Response<Void>>()
every { response.isSuccessful } returns true
coEvery { dao.delete(any()) } answers {}
val deleteWList = getPrivateFun(
"deleteWListCallBack", WListRepository::class
)
val repository = WListRepository(
dao, service, reader
)
runBlocking {
deleteWList?.callSuspend(
repository,
response,
"id"
)
}
coVerify { dao.delete(WList("id")) }
}
@Test
fun deleteWListCallbackError() {
val dao = mockk<WListDao>()
val response = mockk<Response<Void>>()
every { response.isSuccessful } returns false
every { response.code() } returns 400
every { response.message() } returns "Error"
val repository = WListRepository(dao, service, reader)
val deleteWList = getPrivateFun(
"deleteWListCallBack", WListRepository::class
)
runBlocking {
deleteWList?.callSuspend(
repository,
response,
"id"
)
}
verify { Log.e("RETROFIT", "400 Error") }
}
@Test
fun deleteOldWListFromDb() {
val dao = mockk<WListDao>()
val dbWLists = mockk<LiveData<List<WList>>>()
val wList0 = WList("id0", "title0")
val wList1 = WList("id1", "title1")
val wList2 = WList("id2", "title2")
val wList3 = WList("id2", "title3")
every { dbWLists.value } returns listOf(
wList0, wList1, wList2, wList3
)
coEvery { dao.allWList } returns dbWLists
coEvery { dao.delete(any()) } answers {}
val removeOldWLists = getPrivateFun(
"removeOldWListsFromDb", WListRepository::class
)
val repository = WListRepository(
dao, service, reader
)
runBlocking {
removeOldWLists?.callSuspend(
repository,
listOf(
wList0, wList2, wList3
)
)
}
coVerify { dao.delete(wList1) }
}
@Test
fun synchronizeCallbackError() {
val dao = mockk<WListDao>()
val response =
mockk<Response<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>>>()
every { response.isSuccessful } returns false
every { response.code() } returns 400
every { response.message() } returns "Error"
val synchronize = getPrivateFun(
"synchronizeCallback", WListRepository::class
)
val repository = WListRepository(
dao, service, reader
)
runBlocking {
synchronize?.callSuspend(
repository,
response
)
}
verify { Log.e("RETROFIT", "400 Error") }
}
}