From 614b82c4440dbabbcd06b322549d0790cc26f211 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Fri, 31 May 2024 16:12:45 +0530 Subject: [PATCH] Added migration test cases for History. --- .../kiwixmobile/KiwixRoomDatabaseTest.kt | 2 +- .../ObjectBoxToRoomMigratorTest.kt | 115 +++++++++++++++++- 2 files changed, 112 insertions(+), 5 deletions(-) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/KiwixRoomDatabaseTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/KiwixRoomDatabaseTest.kt index 27f2f568b..2edb84c27 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/KiwixRoomDatabaseTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/KiwixRoomDatabaseTest.kt @@ -137,7 +137,7 @@ class KiwixRoomDatabaseTest { title: String = "Installation", historyUrl: String = "https://kiwix.app/A/Installation", dateString: String = "30 May 2024", - databaseId: Long, + databaseId: Long = 0L, zimId: String = "1f88ab6f-c265-b-3ff-8f49-b7f4429503800", zimName: String = "alpinelinux_en_all", zimFilePath: String = "/storage/emulated/0/Download/alpinelinux_en_all_maxi_2023-01.zim", diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt index 044dd66a7..80300c4b1 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt @@ -26,6 +26,8 @@ import io.objectbox.Box import io.objectbox.BoxStore import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.core.IsEqual.equalTo import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull @@ -33,6 +35,8 @@ import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.kiwix.kiwixmobile.KiwixRoomDatabaseTest.Companion.getHistoryItem +import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity import org.kiwix.kiwixmobile.core.data.KiwixRoomDatabase import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToRoomMigrator @@ -69,7 +73,7 @@ class ObjectBoxToRoomMigratorTest { fun migrateRecentSearch_shouldInsertDataIntoRoomDatabase() = runBlocking { val box = boxStore.boxFor(RecentSearchEntity::class.java) // clear both databases for recent searches to test more edge cases - clearRecentSearchDatabases(box) + clearRoomAndBoxStoreDatabases(box) val expectedSearchTerm = "test search" val expectedZimId = "8812214350305159407L" val expectedUrl = "http://kiwix.app/mainPage" @@ -86,7 +90,7 @@ class ObjectBoxToRoomMigratorTest { assertEquals(actual[0].zimId, expectedZimId) // clear both databases for recent searches to test more edge cases - clearRecentSearchDatabases(box) + clearRoomAndBoxStoreDatabases(box) // Migrate data from empty ObjectBox database objectBoxToRoomMigrator.migrateRecentSearch(box) @@ -114,7 +118,7 @@ class ObjectBoxToRoomMigratorTest { } assertNotNull(newItem) - clearRecentSearchDatabases(box) + clearRoomAndBoxStoreDatabases(box) // Test migration if ObjectBox has null values lateinit var undefinedSearchTerm: String @@ -158,9 +162,112 @@ class ObjectBoxToRoomMigratorTest { assertTrue("Migration took too long: $migrationTime ms", migrationTime < 10000) } - private fun clearRecentSearchDatabases(box: Box) { + private fun clearRoomAndBoxStoreDatabases(box: Box) { // delete history for testing other edge cases kiwixRoomDatabase.recentSearchRoomDao().deleteSearchHistory() + kiwixRoomDatabase.historyRoomDao().deleteAllHistory() box.removeAll() } + + @Test + fun migrateHistory_shouldInsertDataIntoRoomDatabase() = runBlocking { + val box = boxStore.boxFor(HistoryEntity::class.java) + // clear both databases for history to test more edge cases + clearRoomAndBoxStoreDatabases(box) + + val historyItem = getHistoryItem() + val historyItem2 = getHistoryItem( + title = "Main Page", + historyUrl = "https://kiwix.app/A/MainPage" + ) + val historyItem3 = getHistoryItem(databaseId = 1) + // insert into object box + box.put(HistoryEntity(historyItem)) + // migrate data into room database + objectBoxToRoomMigrator.migrateHistory(box) + // check if data successfully migrated to room + val actual = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + with(actual.first()) { + assertThat(historyTitle, equalTo(historyItem.title)) + assertThat(zimId, equalTo(historyItem.zimId)) + assertThat(zimName, equalTo(historyItem.zimName)) + assertThat(historyUrl, equalTo(historyItem.historyUrl)) + assertThat(zimFilePath, equalTo(historyItem.zimFilePath)) + assertThat(favicon, equalTo(historyItem.favicon)) + assertThat(dateString, equalTo(historyItem.dateString)) + assertThat(timeStamp, equalTo(historyItem.timeStamp)) + } + + clearRoomAndBoxStoreDatabases(box) + + // Migrate data from empty ObjectBox database + objectBoxToRoomMigrator.migrateHistory(box) + var actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + assertTrue(actualData.isEmpty()) + + // Test if data successfully migrated to Room and existing data is preserved + kiwixRoomDatabase.historyRoomDao().saveHistory(historyItem3) + box.put(HistoryEntity(historyItem2)) + // Migrate data into Room database + objectBoxToRoomMigrator.migrateHistory(box) + actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + assertEquals(2, actualData.size) + val existingItem = + actualData.find { + it.historyUrl == historyItem.historyUrl && it.historyTitle == historyItem.title + } + assertNotNull(existingItem) + val newItem = + actualData.find { + it.historyUrl == historyItem2.historyUrl && it.historyTitle == historyItem2.title + } + assertNotNull(newItem) + + clearRoomAndBoxStoreDatabases(box) + + // Test room will not migrate the already exiting data in the database. + kiwixRoomDatabase.historyRoomDao().saveHistory(historyItem) + box.put(HistoryEntity(historyItem)) + objectBoxToRoomMigrator.migrateHistory(box) + actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + assertEquals(1, actualData.size) + + clearRoomAndBoxStoreDatabases(box) + + // Test migration if ObjectBox has null values + try { + lateinit var invalidHistoryEntity: HistoryEntity + box.put(invalidHistoryEntity) + // Migrate data into Room database + objectBoxToRoomMigrator.migrateHistory(box) + } catch (_: Exception) { + } + // Ensure Room database remains empty or unaffected by the invalid data + actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + assertTrue(actualData.isEmpty()) + + // Test large data migration for recent searches + val numEntities = 5000 + // Insert a large number of recent search entities into ObjectBox + for (i in 1..numEntities) { + box.put( + HistoryEntity( + getHistoryItem( + title = "Installation$i", + historyUrl = "https://kiwix.app/A/Installation$i" + ) + ) + ) + } + val startTime = System.currentTimeMillis() + // Migrate data into Room database + objectBoxToRoomMigrator.migrateHistory(box) + val endTime = System.currentTimeMillis() + val migrationTime = endTime - startTime + // Check if data successfully migrated to Room + actualData = kiwixRoomDatabase.historyRoomDao().historyRoomEntity().first() + assertEquals(numEntities, actualData.size) + // Assert that the migration completes within a reasonable time frame + assertTrue("Migration took too long: $migrationTime ms", migrationTime < 10000) + } }