From 84e0b742c23a92c3abe804550bff6591769a3111 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Wed, 5 Jun 2024 16:46:30 +0530 Subject: [PATCH] Added migration test cases for notes. --- .../ObjectBoxToRoomMigratorTest.kt | 115 +++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt index d66e3a2d1..4adcc270b 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/ObjectBoxToRoomMigratorTest.kt @@ -36,11 +36,14 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith 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.NotesEntity import org.kiwix.kiwixmobile.core.dao.entities.RecentSearchEntity import org.kiwix.kiwixmobile.core.data.KiwixRoomDatabase import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToRoomMigrator import org.kiwix.kiwixmobile.core.di.modules.DatabaseModule +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil @RunWith(AndroidJUnit4::class) @@ -162,10 +165,11 @@ class ObjectBoxToRoomMigratorTest { assertTrue("Migration took too long: $migrationTime ms", migrationTime < 20000) } - private fun clearRoomAndBoxStoreDatabases(box: Box) { + private suspend fun clearRoomAndBoxStoreDatabases(box: Box) { // delete history for testing other edge cases kiwixRoomDatabase.recentSearchRoomDao().deleteSearchHistory() kiwixRoomDatabase.historyRoomDao().deleteAllHistory() + kiwixRoomDatabase.notesRoomDao().deletePages(kiwixRoomDatabase.notesRoomDao().notes().first()) box.removeAll() } @@ -270,4 +274,113 @@ class ObjectBoxToRoomMigratorTest { // Assert that the migration completes within a reasonable time frame 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 + 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 + 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 + 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 + 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 + 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 + assertEquals(numEntities, notesList.size) + // Assert that the migration completes within a reasonable time frame + assertTrue("Migration took too long: $migrationTime ms", migrationTime < 20000) + } }