diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/data/local/dao/NoteRoomDaoTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/data/local/dao/NoteRoomDaoTest.kt new file mode 100644 index 000000000..ddb4178eb --- /dev/null +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/data/local/dao/NoteRoomDaoTest.kt @@ -0,0 +1,163 @@ +/* + * Kiwix Android + * Copyright (c) 2023 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.data.local.dao + +import android.content.Context +import androidx.room.Room +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import io.mockk.mockk +import io.objectbox.Box +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import org.junit.runner.RunWith +import org.kiwix.kiwixmobile.core.dao.NewNoteDao +import org.kiwix.kiwixmobile.core.dao.NotesRoomDao +import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity +import org.kiwix.kiwixmobile.core.dao.entities.NotesRoomEntity +import org.kiwix.kiwixmobile.core.data.local.KiwixRoomDatabase +import org.kiwix.kiwixmobile.core.page.adapter.Page +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem +import java.io.IOException + +@RunWith(AndroidJUnit4::class) +class NoteRoomDaoTest { + private lateinit var noteRoomDao: NotesRoomDao + private lateinit var db: KiwixRoomDatabase + private val notesBox: Box = mockk(relaxed = true) + private val newNotesDao = NewNoteDao(notesBox) + + @Test + @Throws(IOException::class) + fun deletePages() = runBlocking { + val context = ApplicationProvider.getApplicationContext() + val notesItem: NoteListItem = mockk(relaxed = true) + val notesItemList: List = listOf(notesItem) + val pagesToDelete: List = notesItemList + db = Room.inMemoryDatabaseBuilder( + context, KiwixRoomDatabase::class.java + ).build() + noteRoomDao = db.noteRoomDao() + noteRoomDao.deletePages(pagesToDelete) + noteRoomDao.deleteNotes(notesItemList) + noteRoomDao.pages().collect { + Assertions.assertEquals(0, it.size) + } + } + + @Test + @Throws(IOException::class) + fun deleteNotePage() = runBlocking { + val context = ApplicationProvider.getApplicationContext() + val notesItem: NoteListItem = mockk(relaxed = true) + db = Room.inMemoryDatabaseBuilder( + context, KiwixRoomDatabase::class.java + ).build() + noteRoomDao = db.noteRoomDao() + val noteTitle = "abNotesTitle" + notesItem.title = noteTitle + noteRoomDao.saveNote(NotesRoomEntity(notesItem)) + noteRoomDao.deleteNote(noteTitle) + noteRoomDao.notesAsEntity().collect { + Assertions.assertEquals(0, it.size) + } + } + + @Test + @Throws(IOException::class) + fun saveNote() = runBlocking { + val context = ApplicationProvider.getApplicationContext() + val notesItem: NoteListItem = mockk(relaxed = true) + val notesItemList: List = listOf(notesItem) + db = Room.inMemoryDatabaseBuilder( + context, KiwixRoomDatabase::class.java + ).build() + noteRoomDao = db.noteRoomDao() + val noteTitle = "abNotesTitle" + notesItem.title = noteTitle + noteRoomDao.saveNote(NotesRoomEntity(notesItem)) + noteRoomDao.notesAsEntity().collect { + Assertions.assertEquals(1, it.size) + Assertions.assertEquals(noteTitle, it.first().noteTitle) + } + } + + @Test + @Throws(IOException::class) + fun saveNotes() = runBlocking { + val context = ApplicationProvider.getApplicationContext() + val notesItem: NoteListItem = mockk(relaxed = true) + val notesItem2: NoteListItem = mockk(relaxed = true) + val notesItem3: NoteListItem = mockk(relaxed = true) + val notesItem4: NoteListItem = mockk(relaxed = true) + val notesItem5: NoteListItem = mockk(relaxed = true) + db = Room.inMemoryDatabaseBuilder( + context, KiwixRoomDatabase::class.java + ).build() + noteRoomDao = db.noteRoomDao() + noteRoomDao.saveNote(NotesRoomEntity(notesItem)) + noteRoomDao.saveNote(NotesRoomEntity(notesItem2)) + noteRoomDao.saveNote(NotesRoomEntity(notesItem3)) + noteRoomDao.saveNote(NotesRoomEntity(notesItem4)) + noteRoomDao.saveNote(NotesRoomEntity(notesItem5)) + noteRoomDao.notesAsEntity().collect { + Assertions.assertEquals(5, it.size) + } + } + + @Test + @Throws(IOException::class) + fun migrationTest() = runBlocking { + val context = ApplicationProvider.getApplicationContext() + db = Room.inMemoryDatabaseBuilder( + context, KiwixRoomDatabase::class.java + ).build() + noteRoomDao = db.noteRoomDao() + val newNote: NoteListItem = mockk(relaxed = true) + newNotesDao.saveNote(newNote) + notesBox.put(NotesEntity(newNote)) + noteRoomDao.migrationToRoomInsert(notesBox) + noteRoomDao.pages().collect { + Assertions.assertEquals(1, it.size) + } + } + + @Test + @Throws(IOException::class) + fun migrationTest2() = runBlocking { + val context = ApplicationProvider.getApplicationContext() + db = Room.inMemoryDatabaseBuilder( + context, KiwixRoomDatabase::class.java + ).build() + noteRoomDao = db.noteRoomDao() + val newNote: NoteListItem = mockk(relaxed = true) + newNotesDao.saveNote(newNote) + notesBox.put(NotesEntity(newNote)) + notesBox.put(NotesEntity(newNote)) + notesBox.put(NotesEntity(newNote)) + notesBox.put(NotesEntity(newNote)) + notesBox.put(NotesEntity(newNote)) + notesBox.put(NotesEntity(newNote)) + noteRoomDao.migrationToRoomInsert(notesBox) + noteRoomDao.pages().collect { + Assertions.assertEquals(6, it.size) + } + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt index cc71d890f..885c4b2bf 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt @@ -8,7 +8,7 @@ import org.kiwix.kiwixmobile.core.reader.ZimFileReader data class NoteListItem( val databaseId: Long = 0L, override val zimId: String, - override val title: String, + override var title: String, override val zimFilePath: String?, val zimUrl: String, val noteFilePath: String,