mirror of
https://github.com/AllanWang/Frost-for-Facebook.git
synced 2024-11-10 04:52:38 +01:00
Add more cookie db tests
This commit is contained in:
parent
8841728780
commit
b5d442ba3c
@ -5,7 +5,6 @@ import androidx.room.Room
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Rule
|
||||
import org.junit.runner.RunWith
|
||||
import kotlin.test.AfterTest
|
||||
import kotlin.test.BeforeTest
|
||||
@ -35,7 +34,7 @@ class FrostDatabaseTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun basic() {
|
||||
fun basicCookie() {
|
||||
val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie")
|
||||
runBlocking {
|
||||
db.cookieDao().insertCookie(cookie)
|
||||
@ -43,4 +42,42 @@ class FrostDatabaseTest {
|
||||
assertEquals(listOf(cookie), cookies, "Cookie mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deleteCookie() {
|
||||
val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie")
|
||||
|
||||
runBlocking {
|
||||
db.cookieDao().insertCookie(cookie)
|
||||
db.cookieDao().deleteById(cookie.id + 1)
|
||||
assertEquals(
|
||||
listOf(cookie),
|
||||
db.cookieDao().selectAll(),
|
||||
"Cookie list should be the same after inexistent deletion"
|
||||
)
|
||||
db.cookieDao().deleteById(cookie.id)
|
||||
assertEquals(emptyList(), db.cookieDao().selectAll(), "Cookie list should be empty after deletion")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun insertCookie() {
|
||||
val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie")
|
||||
runBlocking {
|
||||
db.cookieDao().insertCookie(cookie)
|
||||
assertEquals(listOf(cookie), db.cookieDao().selectAll(), "Cookie insertion failed")
|
||||
db.cookieDao().insertCookie(cookie.copy(name = "testName2"))
|
||||
assertEquals(
|
||||
listOf(cookie.copy(name = "testName2")),
|
||||
db.cookieDao().selectAll(),
|
||||
"Cookie replacement failed"
|
||||
)
|
||||
db.cookieDao().insertCookie(cookie.copy(id = 123L))
|
||||
assertEquals(
|
||||
setOf(cookie.copy(id = 123L), cookie.copy(name = "testName2")),
|
||||
db.cookieDao().selectAll().toSet(),
|
||||
"New cookie insertion failed"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
@ -33,6 +33,8 @@ import com.raizlabs.android.dbflow.kotlinextensions.fastSave
|
||||
import com.raizlabs.android.dbflow.kotlinextensions.from
|
||||
import com.raizlabs.android.dbflow.kotlinextensions.select
|
||||
import com.raizlabs.android.dbflow.structure.BaseModel
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
/**
|
||||
* Created by Allan Wang on 2017-05-30.
|
||||
@ -52,21 +54,26 @@ interface FbTabDao {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun _insertAll(items: List<FbTabEntity>)
|
||||
|
||||
@Transaction
|
||||
suspend fun _save(items: List<FbTabEntity>) {
|
||||
_deleteAll()
|
||||
_insertAll(items)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saving tabs operates by deleting all db items and saving the new list.
|
||||
* Transactions can't be done with suspensions in room as switching threads during the process
|
||||
* may result in a deadlock.
|
||||
* In this case, there may be a chance that the 'transaction' completes partially,
|
||||
* but we'll just fallback to the default anyways.
|
||||
*/
|
||||
suspend fun FbTabDao.save(items: List<FbItem>) {
|
||||
_save((items.takeIf { it.isNotEmpty() } ?: defaultTabs()).mapIndexed { index, fbItem ->
|
||||
FbTabEntity(
|
||||
index,
|
||||
fbItem
|
||||
)
|
||||
})
|
||||
withContext(NonCancellable) {
|
||||
_deleteAll()
|
||||
val entities = (items.takeIf { it.isNotEmpty() } ?: defaultTabs()).mapIndexed { index, fbItem ->
|
||||
FbTabEntity(
|
||||
index,
|
||||
fbItem
|
||||
)
|
||||
}
|
||||
_insertAll(entities)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun FbTabDao.selectAll(): List<FbItem> = _selectAll().map { it.tab }.takeIf { it.isNotEmpty() } ?: defaultTabs()
|
||||
|
Loading…
Reference in New Issue
Block a user