diff --git a/WordPress/src/androidTest/java/org/wordpress/android/editor/savedinstance/SavedInstanceDatabaseTest.kt b/WordPress/src/androidTest/java/org/wordpress/android/editor/savedinstance/SavedInstanceDatabaseTest.kt index 634c1f1fe070..36529f934774 100644 --- a/WordPress/src/androidTest/java/org/wordpress/android/editor/savedinstance/SavedInstanceDatabaseTest.kt +++ b/WordPress/src/androidTest/java/org/wordpress/android/editor/savedinstance/SavedInstanceDatabaseTest.kt @@ -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 @@ -23,6 +25,7 @@ class SavedInstanceDatabaseTest { fun setUp() { hiltRule.inject() db = SavedInstanceDatabase.getDatabase(context)!! + db.reset(db.writableDatabase) } @Test @@ -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) + } } diff --git a/libs/editor/src/main/java/org/wordpress/android/editor/savedinstance/SavedInstanceDatabase.kt b/libs/editor/src/main/java/org/wordpress/android/editor/savedinstance/SavedInstanceDatabase.kt index 808008e73f94..f772762b7e2a 100644 --- a/libs/editor/src/main/java/org/wordpress/android/editor/savedinstance/SavedInstanceDatabase.kt +++ b/libs/editor/src/main/java/org/wordpress/android/editor/savedinstance/SavedInstanceDatabase.kt @@ -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) @@ -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 diff --git a/libs/editor/src/main/java/org/wordpress/android/editor/savedinstance/SavedParcelTable.kt b/libs/editor/src/main/java/org/wordpress/android/editor/savedinstance/SavedParcelTable.kt index df89e7de4d0f..08c1e82e2cb9 100644 --- a/libs/editor/src/main/java/org/wordpress/android/editor/savedinstance/SavedParcelTable.kt +++ b/libs/editor/src/main/java/org/wordpress/android/editor/savedinstance/SavedParcelTable.kt @@ -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 + } }