mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-15 18:36:01 -04:00
Added migration test cases for notes.
This commit is contained in:
parent
8f42e09c45
commit
84e0b742c2
@ -36,11 +36,14 @@ 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.KiwixRoomDatabaseTest.Companion.getHistoryItem
|
||||||
|
import org.kiwix.kiwixmobile.KiwixRoomDatabaseTest.Companion.getNoteListItem
|
||||||
import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity
|
import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity
|
||||||
|
import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity
|
||||||
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
|
||||||
import org.kiwix.kiwixmobile.core.di.modules.DatabaseModule
|
import org.kiwix.kiwixmobile.core.di.modules.DatabaseModule
|
||||||
|
import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem
|
||||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4::class)
|
@RunWith(AndroidJUnit4::class)
|
||||||
@ -162,10 +165,11 @@ class ObjectBoxToRoomMigratorTest {
|
|||||||
assertTrue("Migration took too long: $migrationTime ms", migrationTime < 20000)
|
assertTrue("Migration took too long: $migrationTime ms", migrationTime < 20000)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T> clearRoomAndBoxStoreDatabases(box: Box<T>) {
|
private suspend 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()
|
kiwixRoomDatabase.historyRoomDao().deleteAllHistory()
|
||||||
|
kiwixRoomDatabase.notesRoomDao().deletePages(kiwixRoomDatabase.notesRoomDao().notes().first())
|
||||||
box.removeAll()
|
box.removeAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,4 +274,113 @@ class ObjectBoxToRoomMigratorTest {
|
|||||||
// Assert that the migration completes within a reasonable time frame
|
// Assert that the migration completes within a reasonable time frame
|
||||||
assertTrue("Migration took too long: $migrationTime ms", migrationTime < 20000)
|
assertTrue("Migration took too long: $migrationTime ms", migrationTime < 20000)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun migrateNotes_shouldInsertDataIntoRoomDatabase() = runBlocking {
|
||||||
|
val box = boxStore.boxFor(NotesEntity::class.java)
|
||||||
|
// clear both databases for history to test more edge cases
|
||||||
|
clearRoomAndBoxStoreDatabases(box)
|
||||||
|
|
||||||
|
val noteItem = getNoteListItem(
|
||||||
|
zimUrl = "http://kiwix.app/MainPage",
|
||||||
|
noteFilePath = "/storage/emulated/0/Download/Notes/Alpine linux/MainPage.txt"
|
||||||
|
)
|
||||||
|
|
||||||
|
val noteItem1 = getNoteListItem(
|
||||||
|
databaseId = 1,
|
||||||
|
title = "Installing",
|
||||||
|
zimUrl = "http://kiwix.app/Installing",
|
||||||
|
noteFilePath = "/storage/emulated/0/Download/Notes/Alpine linux/Installing.txt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// insert into object box
|
||||||
|
box.put(NotesEntity(noteItem))
|
||||||
|
// migrate data into room database
|
||||||
|
objectBoxToRoomMigrator.migrateNotes(box)
|
||||||
|
// check if data successfully migrated to room
|
||||||
|
var notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List<NoteListItem>
|
||||||
|
with(notesList.first()) {
|
||||||
|
assertThat(zimId, equalTo(noteItem.zimId))
|
||||||
|
assertThat(zimUrl, equalTo(noteItem.zimUrl))
|
||||||
|
assertThat(title, equalTo(noteItem.title))
|
||||||
|
assertThat(zimFilePath, equalTo(noteItem.zimFilePath))
|
||||||
|
assertThat(noteFilePath, equalTo(noteItem.noteFilePath))
|
||||||
|
assertThat(favicon, equalTo(noteItem.favicon))
|
||||||
|
}
|
||||||
|
assertEquals(notesList.size, 1)
|
||||||
|
|
||||||
|
clearRoomAndBoxStoreDatabases(box)
|
||||||
|
|
||||||
|
// Migrate data from empty ObjectBox database
|
||||||
|
objectBoxToRoomMigrator.migrateNotes(box)
|
||||||
|
notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List<NoteListItem>
|
||||||
|
assertTrue(notesList.isEmpty())
|
||||||
|
|
||||||
|
// Test if data successfully migrated to Room and existing data is preserved
|
||||||
|
kiwixRoomDatabase.notesRoomDao().saveNote(noteItem1)
|
||||||
|
box.put(NotesEntity(noteItem))
|
||||||
|
// Migrate data into Room database
|
||||||
|
objectBoxToRoomMigrator.migrateNotes(box)
|
||||||
|
notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List<NoteListItem>
|
||||||
|
assertEquals(noteItem.title, notesList.first().title)
|
||||||
|
assertEquals(2, notesList.size)
|
||||||
|
val existingItem =
|
||||||
|
notesList.find {
|
||||||
|
it.zimUrl == noteItem.zimUrl && it.title == noteItem.title
|
||||||
|
}
|
||||||
|
assertNotNull(existingItem)
|
||||||
|
val newItem =
|
||||||
|
notesList.find {
|
||||||
|
it.zimUrl == noteItem1.zimUrl && it.title == noteItem1.title
|
||||||
|
}
|
||||||
|
assertNotNull(newItem)
|
||||||
|
|
||||||
|
clearRoomAndBoxStoreDatabases(box)
|
||||||
|
|
||||||
|
// Test room will update the already exiting data in the database while migration.
|
||||||
|
kiwixRoomDatabase.notesRoomDao().saveNote(noteItem1)
|
||||||
|
box.put(NotesEntity(noteItem1))
|
||||||
|
// Migrate data into Room database
|
||||||
|
objectBoxToRoomMigrator.migrateNotes(box)
|
||||||
|
notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List<NoteListItem>
|
||||||
|
assertEquals(1, notesList.size)
|
||||||
|
|
||||||
|
clearRoomAndBoxStoreDatabases(box)
|
||||||
|
|
||||||
|
// Test migration if ObjectBox has null values
|
||||||
|
try {
|
||||||
|
lateinit var invalidNotesEntity: NotesEntity
|
||||||
|
box.put(invalidNotesEntity)
|
||||||
|
// Migrate data into Room database
|
||||||
|
objectBoxToRoomMigrator.migrateNotes(box)
|
||||||
|
} catch (_: Exception) {
|
||||||
|
}
|
||||||
|
// Ensure Room database remains empty or unaffected by the invalid data
|
||||||
|
notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List<NoteListItem>
|
||||||
|
assertTrue(notesList.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(
|
||||||
|
NotesEntity(
|
||||||
|
getNoteListItem(
|
||||||
|
title = "Installation$i",
|
||||||
|
zimUrl = "https://kiwix.app/A/Installation$i"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val startTime = System.currentTimeMillis()
|
||||||
|
// Migrate data into Room database
|
||||||
|
objectBoxToRoomMigrator.migrateNotes(box)
|
||||||
|
val endTime = System.currentTimeMillis()
|
||||||
|
val migrationTime = endTime - startTime
|
||||||
|
// Check if data successfully migrated to Room
|
||||||
|
notesList = kiwixRoomDatabase.notesRoomDao().notes().first() as List<NoteListItem>
|
||||||
|
assertEquals(numEntities, notesList.size)
|
||||||
|
// Assert that the migration completes within a reasonable time frame
|
||||||
|
assertTrue("Migration took too long: $migrationTime ms", migrationTime < 20000)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user