Skip to content

Commit

Permalink
Adds hasParcel method
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonis Lilis committed Dec 8, 2023
1 parent a7c04e3 commit 8bdef04
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.Context
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -23,6 +25,7 @@ class SavedInstanceDatabaseTest {
fun setUp() {
hiltRule.inject()
db = SavedInstanceDatabase.getDatabase(context)!!
db.reset(db.writableDatabase)
}

@Test
Expand All @@ -33,4 +36,31 @@ class SavedInstanceDatabaseTest {
val result = db.getParcel(parcelId, TestParcelable.CREATOR)
assertEquals(parcelData, result)
}

@Test
fun testHasParcel() {
val parcelId = "testParcelId"
val parcelData = TestParcelable("testData")
db.addParcel(parcelId, parcelData)
val result = db.hasParcel(parcelId)
assertTrue(result)
}

@Test
fun testHasNoParcel() {
val parcelId1 = "testParcelId1"
val parcelId2 = "testParcelId2"
val parcelData = TestParcelable("testData")
db.addParcel(parcelId1, parcelData)
val result = db.hasParcel(parcelId2)
assertFalse(result)
}

@Test
fun testNullParcel() {
val parcelId = "testParcelId"
db.addParcel(parcelId, null)
val result = db.hasParcel(parcelId)
assertFalse(result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SavedInstanceDatabase(context: Context?) : SQLiteOpenHelper(context, DB_NA
dropTable(db)
}

private fun reset(db: SQLiteDatabase) {
fun reset(db: SQLiteDatabase) {
db.beginTransaction()
try {
dropAllTables(db)
Expand All @@ -56,6 +56,10 @@ class SavedInstanceDatabase(context: Context?) : SQLiteOpenHelper(context, DB_NA
return SavedParcelTable.getParcel(readableDatabase, parcelId, creator)
}

fun hasParcel(parcelId: String): Boolean {
return SavedParcelTable.hasParcel(readableDatabase, parcelId)
}

companion object {
private const val DB_NAME = "wpsavedinstance.db"
private const val DB_VERSION = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ object SavedParcelTable {
SqlUtils.closeCursor(c)
}
}

fun hasParcel(readableDb: SQLiteDatabase?, parcelId: String): Boolean {
val db = readableDb ?: return false
val c = SqlUtils.intForQuery(db, "SELECT COUNT(*) FROM $SAVED_PARCEL_TABLE WHERE $PARCEL_ID ='$parcelId'", null)
return c > 0
}
}

0 comments on commit 8bdef04

Please sign in to comment.