Fix implementation in 'K9BackendStorage' to store extra values

This commit is contained in:
cketti 2019-12-17 15:09:14 +01:00
parent 615cad7414
commit d72b30e82c
2 changed files with 86 additions and 4 deletions

View file

@ -2,6 +2,7 @@ package com.fsck.k9.mailstore
import android.content.ContentValues import android.content.ContentValues
import android.database.Cursor import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import androidx.core.database.getStringOrNull import androidx.core.database.getStringOrNull
import com.fsck.k9.Account import com.fsck.k9.Account
import com.fsck.k9.Preferences import com.fsck.k9.Preferences
@ -68,7 +69,7 @@ class K9BackendStorage(
return database.execute(false) { db -> return database.execute(false) { db ->
val cursor = db.query( val cursor = db.query(
"account_extra_values", "account_extra_values",
arrayOf("value_string"), arrayOf("value_text"),
"name = ?", "name = ?",
arrayOf(name), arrayOf(name),
null, null, null) null, null, null)
@ -85,9 +86,10 @@ class K9BackendStorage(
override fun setExtraString(name: String, value: String) { override fun setExtraString(name: String, value: String) {
database.execute(false) { db -> database.execute(false) { db ->
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put("value_string", value) put("name", name)
put("value_text", value)
} }
db.update("account_extra_values", contentValues, "name = ?", arrayOf(name)) db.insertWithOnConflict("account_extra_values", null, contentValues, SQLiteDatabase.CONFLICT_REPLACE)
} }
} }
@ -112,9 +114,10 @@ class K9BackendStorage(
override fun setExtraNumber(name: String, value: Long) { override fun setExtraNumber(name: String, value: Long) {
database.execute(false) { db -> database.execute(false) { db ->
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put("name", name)
put("value_integer", value) put("value_integer", value)
} }
db.update("account_extra_values", contentValues, "name = ?", arrayOf(name)) db.insertWithOnConflict("account_extra_values", null, contentValues, SQLiteDatabase.CONFLICT_REPLACE)
} }
} }

View file

@ -0,0 +1,79 @@
package com.fsck.k9.mailstore
import android.net.Uri
import com.fsck.k9.Account
import com.fsck.k9.K9RobolectricTest
import com.fsck.k9.Preferences
import com.fsck.k9.backend.api.BackendStorage
import com.fsck.k9.provider.EmailProvider
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.koin.core.inject
class K9BackendStorageTest : K9RobolectricTest() {
val preferences: Preferences by inject()
val localStoreProvider: LocalStoreProvider by inject()
val account: Account = createAccount()
val database: LockableDatabase = localStoreProvider.getInstance(account).database
val backendStorage = createBackendStorage()
@Before
fun setUp() {
// Set EmailProvider.CONTENT_URI so LocalStore.notifyChange() won't crash
EmailProvider.CONTENT_URI = Uri.parse("content://dummy")
}
@After
fun tearDown() {
preferences.deleteAccount(account)
}
@Test
fun writeAndReadExtraString() {
backendStorage.setExtraString("testString", "someValue")
val value = backendStorage.getExtraString("testString")
assertEquals("someValue", value)
}
@Test
fun updateExtraString() {
backendStorage.setExtraString("testString", "oldValue")
backendStorage.setExtraString("testString", "newValue")
val value = backendStorage.getExtraString("testString")
assertEquals("newValue", value)
}
@Test
fun writeAndReadExtraInteger() {
backendStorage.setExtraNumber("testNumber", 42)
val value = backendStorage.getExtraNumber("testNumber")
assertEquals(42L, value)
}
@Test
fun updateExtraInteger() {
backendStorage.setExtraNumber("testNumber", 42)
backendStorage.setExtraNumber("testNumber", 23)
val value = backendStorage.getExtraNumber("testNumber")
assertEquals(23L, value)
}
fun createAccount(): Account {
// FIXME: This is a hack to get Preferences into a state where it's safe to call newAccount()
preferences.clearAccounts()
return preferences.newAccount()
}
private fun createBackendStorage(): BackendStorage {
val localStore: LocalStore = localStoreProvider.getInstance(account)
return K9BackendStorage(preferences, account, localStore, emptyList())
}
}