mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-10 16:02:05 -04:00
Added migration test cases for History.
This commit is contained in:
parent
68e1386344
commit
614b82c444
@ -137,7 +137,7 @@ class KiwixRoomDatabaseTest {
|
|||||||
title: String = "Installation",
|
title: String = "Installation",
|
||||||
historyUrl: String = "https://kiwix.app/A/Installation",
|
historyUrl: String = "https://kiwix.app/A/Installation",
|
||||||
dateString: String = "30 May 2024",
|
dateString: String = "30 May 2024",
|
||||||
databaseId: Long,
|
databaseId: Long = 0L,
|
||||||
zimId: String = "1f88ab6f-c265-b-3ff-8f49-b7f4429503800",
|
zimId: String = "1f88ab6f-c265-b-3ff-8f49-b7f4429503800",
|
||||||
zimName: String = "alpinelinux_en_all",
|
zimName: String = "alpinelinux_en_all",
|
||||||
zimFilePath: String = "/storage/emulated/0/Download/alpinelinux_en_all_maxi_2023-01.zim",
|
zimFilePath: String = "/storage/emulated/0/Download/alpinelinux_en_all_maxi_2023-01.zim",
|
||||||
|
@ -26,6 +26,8 @@ import io.objectbox.Box
|
|||||||
import io.objectbox.BoxStore
|
import io.objectbox.BoxStore
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import org.hamcrest.MatcherAssert.assertThat
|
||||||
|
import org.hamcrest.core.IsEqual.equalTo
|
||||||
import org.junit.After
|
import org.junit.After
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Assert.assertNotNull
|
import org.junit.Assert.assertNotNull
|
||||||
@ -33,6 +35,8 @@ import org.junit.Assert.assertTrue
|
|||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
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.dao.entities.RecentSearchEntity
|
||||||
import org.kiwix.kiwixmobile.core.data.KiwixRoomDatabase
|
import org.kiwix.kiwixmobile.core.data.KiwixRoomDatabase
|
||||||
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToRoomMigrator
|
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToRoomMigrator
|
||||||
@ -69,7 +73,7 @@ class ObjectBoxToRoomMigratorTest {
|
|||||||
fun migrateRecentSearch_shouldInsertDataIntoRoomDatabase() = runBlocking {
|
fun migrateRecentSearch_shouldInsertDataIntoRoomDatabase() = runBlocking {
|
||||||
val box = boxStore.boxFor(RecentSearchEntity::class.java)
|
val box = boxStore.boxFor(RecentSearchEntity::class.java)
|
||||||
// clear both databases for recent searches to test more edge cases
|
// clear both databases for recent searches to test more edge cases
|
||||||
clearRecentSearchDatabases(box)
|
clearRoomAndBoxStoreDatabases(box)
|
||||||
val expectedSearchTerm = "test search"
|
val expectedSearchTerm = "test search"
|
||||||
val expectedZimId = "8812214350305159407L"
|
val expectedZimId = "8812214350305159407L"
|
||||||
val expectedUrl = "http://kiwix.app/mainPage"
|
val expectedUrl = "http://kiwix.app/mainPage"
|
||||||
@ -86,7 +90,7 @@ class ObjectBoxToRoomMigratorTest {
|
|||||||
assertEquals(actual[0].zimId, expectedZimId)
|
assertEquals(actual[0].zimId, expectedZimId)
|
||||||
|
|
||||||
// clear both databases for recent searches to test more edge cases
|
// clear both databases for recent searches to test more edge cases
|
||||||
clearRecentSearchDatabases(box)
|
clearRoomAndBoxStoreDatabases(box)
|
||||||
|
|
||||||
// Migrate data from empty ObjectBox database
|
// Migrate data from empty ObjectBox database
|
||||||
objectBoxToRoomMigrator.migrateRecentSearch(box)
|
objectBoxToRoomMigrator.migrateRecentSearch(box)
|
||||||
@ -114,7 +118,7 @@ class ObjectBoxToRoomMigratorTest {
|
|||||||
}
|
}
|
||||||
assertNotNull(newItem)
|
assertNotNull(newItem)
|
||||||
|
|
||||||
clearRecentSearchDatabases(box)
|
clearRoomAndBoxStoreDatabases(box)
|
||||||
|
|
||||||
// Test migration if ObjectBox has null values
|
// Test migration if ObjectBox has null values
|
||||||
lateinit var undefinedSearchTerm: String
|
lateinit var undefinedSearchTerm: String
|
||||||
@ -158,9 +162,112 @@ class ObjectBoxToRoomMigratorTest {
|
|||||||
assertTrue("Migration took too long: $migrationTime ms", migrationTime < 10000)
|
assertTrue("Migration took too long: $migrationTime ms", migrationTime < 10000)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun clearRecentSearchDatabases(box: Box<RecentSearchEntity>) {
|
private fun <T> clearRoomAndBoxStoreDatabases(box: Box<T>) {
|
||||||
// delete history for testing other edge cases
|
// delete history for testing other edge cases
|
||||||
kiwixRoomDatabase.recentSearchRoomDao().deleteSearchHistory()
|
kiwixRoomDatabase.recentSearchRoomDao().deleteSearchHistory()
|
||||||
|
kiwixRoomDatabase.historyRoomDao().deleteAllHistory()
|
||||||
box.removeAll()
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user